android开源项目DivideAndConquer中的小球碰撞问题
1.
对于以上问题,尤其是后两个,不是很明白,有明白的客官,路过后可以留言,灰常感谢!
/*看小球是否重叠*/
public boolean isCircleOverlapping(Ball otherBall) {
final float dy = otherBall.mY - mY;
final float dx = otherBall.mX - mX;
final float distance = dy * dy + dx * dx;//两小球间的距离
return (distance < ((2 * mRadiusPixels) * (2 *mRadiusPixels)))//如果距离小于半径的两倍并且不是碰撞后离开的状态,即重叠
// avoid jittery collisions
&& !movingAwayFromEachother(this, otherBall);
}
2.
/*如何判定连个球朝相反方向运动??*/
private boolean movingAwayFromEachother(Ball ballA, Ball ballB) {
double collA = Math.atan2(ballB.mY - ballA.mY, ballB.mX - ballA.mX);
double collB = Math.atan2(ballA.mY - ballB.mY, ballA.mX - ballB.mX);
double ax = Math.cos(ballA.mAngle - collA);
double bx = Math.cos(ballB.mAngle - collB);
return ax + bx < 0;
}
3.
/**
* Given that ball a and b have collided, adjust their angles to reflect their state
* after the collision.
* 弹性碰撞中的能量守恒和动量守恒定律
* This method works based on the conservation of energy and momentum in an elastic
* collision. Because the balls have equal mass and speed, it ends up being that they
* simply swap velocities along the axis of the collision, keeping the velocities tangent
* to the collision constant.
*
* @param ballA The first ball in a collision
* @param ballB The second ball in a collision
*/
/*碰撞后两个小球的方向??*/
public static void adjustForCollision(Ball ballA, Ball ballB) {
final double collA = Math.atan2(ballB.mY - ballA.mY, ballB.mX - ballA.mX);
final double collB = Math.atan2(ballA.mY - ballB.mY, ballA.mX - ballB.mX);
final double ax = Math.cos(ballA.mAngle - collA);
final double ay = Math.sin(ballA.mAngle - collA);
final double bx = Math.cos(ballB.mAngle - collB);
final double by = Math.cos(ballB.mAngle - collB);
final double diffA = Math.atan2(ay, -bx);
final double diffB = Math.atan2(by, -ax);
ballA.mAngle = collA + diffA;
ballB.mAngle = collB + diffB;
}