Note that there are some explanatory texts on larger screens.

plurals
  1. POBall to Ball Collision - Gains significant velocity upon collision
    text
    copied!<p>I implemented the code from the question "<a href="https://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling">Ball to Ball Collision - Detection and Handling</a>" in Objective-C. However, whenever the balls collide at an angle their velocity increases dramatically. All of the vector math is done using <a href="http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/Support/CGPointExtension.h?spec=svn806&amp;r=806" rel="nofollow noreferrer">cocos2d-iphone</a>, with the header CGPointExtension.h. What is the cause of this undesired acceleration?</p> <p>The following is an example of increase in speed: </p> <p><strong>Input:</strong><br> mass == 12.56637<br> velocity.x == 1.73199439<br> velocity.y == -10.5695238 </p> <p>ball.mass == 12.56637<br> ball.velocity.x == 6.04341078<br> ball.velocity.y == 14.2686739</p> <p><strong>Output:</strong><br> mass == 12.56637<br> velocity.x == 110.004326<br> velocity.y == -10.5695238 </p> <p>ball.mass == 12.56637<br> ball.velocity.x == -102.22892<br> ball.velocity.y == -72.4030228</p> <pre><code>#import "CGPointExtension.h" #define RESTITUTION_CONSTANT (0.75) //elasticity of the system - (void) resolveCollision:(Ball*) ball { // get the mtd (minimum translation distance) CGPoint delta = ccpSub(position, ball.position); float d = ccpLength(delta); // minimum translation distance to push balls apart after intersecting CGPoint mtd = ccpMult(delta, (((radius + ball.radius)-d)/d)); // resolve intersection -- // inverse mass quantities float im1 = 1 / [self mass]; float im2 = 1 / [ball mass]; // push-pull them apart based off their mass position = ccpAdd(position, ccpMult(mtd, (im1 / (im1 + im2)))); ball.position = ccpSub(ball.position, ccpMult(mtd, (im2 / (im1 + im2)))); // impact speed CGPoint v = ccpSub(velocity, ball.velocity); float vn = ccpDot(v,ccpNormalize(mtd)); // sphere intersecting but moving away from each other already if (vn &gt; 0.0f) return; // collision impulse float i = (-(1.0f + RESTITUTION_CONSTANT) * vn) / ([self mass] + [ball mass]); CGPoint impulse = ccpMult(mtd, i); // change in momentum velocity = ccpAdd(velocity, ccpMult(impulse, im1)); ball.velocity = ccpSub(ball.velocity, ccpMult(impulse, im2)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload