Objective-Cの質問「Ball to Ball Collision - Detection and Handling」のコードを実装しました。しかしながら、ボールがある角度で衝突するときはいつも、それらの速度は劇的に増加する。すべてのベクトル演算は、cocos2d-iphoneを使用して、ヘッダCGPointExtension.hとともに行われます。この望ましくない加速の原因は何ですか?Ball to Ball Collision - 衝突時に大きな速度を得ます。
入力:
質量== 12.56637
velocity.x == 1.73199439
velocity.yの== -10.5695238
ボール
は、次の高速化の一例です.mass == 12.56637
ball.velocity.x == 6.04341078
ball.velocity.y == 14.2686739
出力:
質量== 12.56637
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
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 > 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));
}
あなたの物理ここで問題です:あなたは、反発係数は、(1以上)1.75であることから、運動エネルギーを節約しない衝突を設定しています。 これはプログラミングに関するエラーではありません。 – jpinto3912
正常に機能するコードの最終版を投稿できますか? –
@ jpinto3912 - 勢いの保全を意味しないのですか? –