1
好的,我有这种算法应该检测用户在iPad的屏幕上绘制的球和线之间的碰撞。代码很简单:移动的球和线之间的碰撞
for (int i = 1; i < currentPlatform; i++) {
float x1 = xStart[i] -xBall;
float y1 = yStart[i] - yBall;
float x2 = xEnd[i] - xBall;
float y2 = yEnd[i] - yBall;
float dx = x2 - x1;
float dy = y2 - y1;
float dr = sqrtf(powf(dx, 2) + powf(dy, 2));
float D = x1*y2 - x2*y1;
float delta = powf((ball.frame.size.height/2), 2)*powf(dr, 2) - powf(D, 2);
NSLog(@"%f", delta);
if (delta >= 0) {
iDir = (((ballVelocity*(sin(angle[i])))*-1)*kRestitution);//*sin(angle[i]));
jDir = (((ballVelocity*(cos(angle[i]))))*kRestitution*cos(angle[i]));
}
}
唯一的问题是,球穿过线未被发现!在打印三角洲的控制台中,它会给出-4万亿美元的疯狂答案!有没有人看到我在做这个碰撞检测的错误?
这可能不是最好的方法,但如果没有别的工作,那么为什么不尝试。 – Jordan 2011-12-23 04:03:43