Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Collision control between mBall and another big ball
- private boolean updateBallCollision(float x, float y) {
- //Get actual distance (without square root - remember?) between the mBall and the ball being checked
- float distanceBetweenBallAndPaddle = (x - mBallX) * (x - mBallX) + (y - mBallY) *(y - mBallY);
- //Check if the actual distance is lower than the allowed => collision
- if(mMinDistanceBetweenBallAndPaddle >= distanceBetweenBallAndPaddle) {
- //Get the present speed (this should also be the speed going away after the collision)
- float speedOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //Change the direction of the ball
- mBallSpeedX = mBallX - x;
- mBallSpeedY = mBallY - y;
- //Get the speed after the collision
- float newSpeedOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //using the fraction between the original speed and present speed to calculate the needed
- //velocities in X and Y to get the original speed but with the new angle.
- mBallSpeedX = mBallSpeedX * speedOfBall / newSpeedOfBall;
- mBallSpeedY = mBallSpeedY * speedOfBall / newSpeedOfBall;
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement