Quantcast
Channel: Ball to Ball Collision - Detection and Handling - Stack Overflow
Viewing all articles
Browse latest Browse all 16

Answer by zezba9000 for Ball to Ball Collision - Detection and Handling

$
0
0

Here is a simple example that supports mass.

private void CollideBalls(Transform ball1, Transform ball2, ref Vector3 vel1, ref Vector3 vel2, float radius1, float radius2){    var vec = ball1.position - ball2.position;    float dis = vec.magnitude;    if (dis < radius1 + radius2)    {        var n = vec.normalized;        ReflectVelocity(ref vel1, ref vel2, ballMass1, ballMass2, n);        var c = Vector3.Lerp(ball1.position, ball2.position, radius1 / (radius1 + radius2));        ball1.position = c + (n * radius1);        ball2.position = c - (n * radius2);    }}public static void ReflectVelocity(ref Vector3 vel1, ref Vector3 vel2, float mass1, float mass2, Vector3 intersectionNormal){    float velImpact1 = Vector3.Dot(vel1, intersectionNormal);    float velImpact2 = Vector3.Dot(vel2, intersectionNormal);    float totalMass = mass1 + mass2;    float massTransfure1 = mass1 / totalMass;    float massTransfure2 = mass2 / totalMass;    vel1 += ((velImpact2 * massTransfure2) - (velImpact1 * massTransfure2)) * intersectionNormal;    vel2 += ((velImpact1 * massTransfure1) - (velImpact2 * massTransfure1)) * intersectionNormal;}

Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>