Академический Документы
Профессиональный Документы
Культура Документы
Detecting a collision
A detailed guide on how to detect collisions in the game.
The game wouldnt be a game if there were no collisions between the objects in it, so you will need to
learn how to detect collisions and react to them appropriately. For this you will be using some maths.
Dont worry we will show you step-by-step how to do it.
Lets observe two balls that have collided (here R1 and R2 are the radius of balls)
You see that there should be a collision if c (the distance between the centres of the balls) is smaller than
the combined length of the two radiuses (R1 and R2). That is when c < R1 + R2. Likewise there should not
be a collision if c > R1 + R2.
Equation 1: Collision condition
c = R1 + R2
Page 1
Collision
When the collision happens you can cheat a bit and create this physical rule:
The small ball will go in a new direction, in a straight line away from the the object it has collided with, as
if the ball hit the object without an angle. This is easier than real physics, because the directions can be set
by taking the centrum of the ball, and subtracting the centrum of the paddle. After that you should adjust
the speed so that it is the same as the original speed.
Math.sqrt can be used to find the square root. It has a library of many different and useful mathematical
functions.
(You can explore the different functions it has here:
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html)
You need to find the new speeds for the X and Y direction to get the new direction of the ball. First you
find the distance between the centrum of the two balls; this will give you the new direction that the ball
needs to go after the impact.
Page 2
Remember that mCanvasHeight is always the Y position of the paddle. At this stage mBallSpeedX and
mBallSpeedY are not speeds/velocities, but values of the new direction. Now you have the new directions,
you need to adjust them so that they have the same speed as they did before the collision. To do this you
need to find the new velocity that the ball would have if it was using the given mBallSpeedY and
mBallSpeedX:
float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
Then that can be used to adjust mBallSpeedX and mBallSpeedY so that the speed is exactly the same as
the old velocity before the collision.
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
Page 3
c2 <= (R1 + R2)2 should then be checked in the beginning of updateGame, but only if mBallSpeedY is
positive.
That can be done in this code:
if(mBallSpeedY > 0) {
//TODO find c
Page 4
}
}
Lets call c2 the distanceBetweenBallAndBall, because it describes what c is better than just c_squared.
It is found using Pythagoras theorem:
a is: mPaddleX mBallX
b is: mCanvasHeight mBallY
distanceBetweenBallAndBall = a2 + b2
so
if(mBallSpeedY > 0) {
distanceBetweenBallAndBall =
(mPaddleX - mBallX) * (mPaddleX - mBallX) +
(mCanvasHeight - mBallY) *(mCanvasHeight - mBallY);
if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndBall) {
//TODO change mBallSpeedX and mBallSpeedY
}
}
Page 5