### 球与球碰撞 - 碰撞时获得显着的速度

velocity.x == 1.73199439
velocity.y == -10.5695238

ball.mass == 12.56637
ball.velocity.x == 6.04341078
ball.velocity.y == 14.2686739

velocity.x == 110.004326
velocity.y == -10.5695238

ball.mass == 12.56637
ball.velocity.x == -102.22892
ball.velocity.y == -72.4030228

``````#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

// 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 > 0.0f) return;

// collision impulse
float i = (-(1.0f + RESTITUTION_CONSTANT) * vn) / ([self mass] + [ball mass]);
CGPoint impulse = ccpMult(mtd, i);

// change in momentum
ball.velocity = ccpSub(ball.velocity, ccpMult(impulse, im2));

}
``````

#### 3 个答案:

COR通常是[0,1]范围内的数字。定性地，1表示完全弹性碰撞，而0表示完全非弹性碰撞。大于1的COR在理论上是可能的，表示产生动能的碰撞，例如地雷被抛在一起并爆炸。

``````/ (im1 + im2)
``````

``````// collision impulse
float i = (-(1.0f + RESTITUTION_CONSTANT) * vn) / (im1 + im2);
CGPoint impulse = ccpMult(mtd, i);
``````

``````J = -Vr(1+e) / {1/m1 + 1/m2}
``````

``````CGPoint impulse = ccpMult(mtd, i);
``````

mtd需要规范化。发生错误是因为在原始代码中，mtd在前一行中进行了规范化，但未在代码中进行规范化。您可以通过执行以下操作来解决此问题：

``````CGPoint impulse = ccpMult(ccpNormalize(mtd), i);
``````