Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1.  
  2.  
  3. //Collision control between mBall and another big ball
  4. private boolean updateBallCollision(float x, float y) {
  5. //Get actual distance (without square root - remember?) between the mBall and the ball being checked
  6. float distanceBetweenBallAndPaddle = (x - mBallX) * (x - mBallX) + (y - mBallY) *(y - mBallY);
  7.  
  8. //Check if the actual distance is lower than the allowed => collision
  9. if(mMinDistanceBetweenBallAndPaddle >= distanceBetweenBallAndPaddle) {
  10. //Get the present speed (this should also be the speed going away after the collision)
  11. float speedOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  12.  
  13. //Change the direction of the ball
  14. mBallSpeedX = mBallX - x;
  15. mBallSpeedY = mBallY - y;
  16.  
  17. //Get the speed after the collision
  18. float newSpeedOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  19.  
  20. //using the fraction between the original speed and present speed to calculate the needed
  21. //velocities in X and Y to get the original speed but with the new angle.
  22. mBallSpeedX = mBallSpeedX * speedOfBall / newSpeedOfBall;
  23. mBallSpeedY = mBallSpeedY * speedOfBall / newSpeedOfBall;
  24.  
  25. return true;
  26. }
  27.  
  28. return false;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement