Android bouncing ball

So, I'm just trying to make the ball bounce around the screen, which should slow down due to gravity and reflect (bounce) off the wall like a regular ball. Can someone give some basics and VERY simple implementation of this? Other examples seem a bit "overdoing" and seem to go beyond what I want to do. I tried this:

public void updateLogic() { if (x < -1) { xPos += (-x * gravity); } else if (x > 1) { xPos -= (x * gravity); } if (y > 1) { yPos += (y * gravity); } else if (y < -1) { yPos -= (-y * gravity); } } 

This is the closest that I received from myself. By the way, the values โ€‹โ€‹of x and y are taken from the accelerometer. Any help would be greatly appreciated, thanks!

+4
source share
3 answers

I think for this you will need three things: strength (x and y, which you have), speed (name them xVel and yVel) and position (xPos and yPos, which you also have). The ball position is updated (in the easiest way):

 xPos += dt*xVel; yPos += dt*yVel; xVel += dt*x; yVel += dt*y; 

The 'dt' variable is a timestep that determines how fast the ball will move. If this is too large, the program will be unstable! Try dt = 0.001 or so to start and gradually set it higher.

Then, to make the ball bounce off the walls, simply flip the speed if it hits the wall:

 if (xPos > xMax) { xPos = xMax; xVel *= -1.0; } else if (xPos < 0.0) { xPos = 0.0; xVel *= -1.0; } 

and the same for y. "XPos = ..." is just stopping the ball going from the edge of the screen. If you want the ball to bounce a little less each time it hits the wall, change the value of โ€œ-1.0โ€ to โ€œ-0.9โ€ or something like that (this is a restitution coefficient ).

Hope this should be all. Good luck

+6
source

Youtube video

Source code is available on github .

  • Your x coordinate should not depend on gravity.
  • I would advise against using accelerometers until you understand how physics works for a simple case.

Break it into stages. First, make sure you can make the ball fall at a constant speed.

Once you have this kind of work, worry about gravity causing an increase in speed.

Once you have this kind of work, worry about crossing walls and speed changes. A simple algorithm for bouncing off walls would be to multiply the speed x by -1 when you hit the vertical wall, and multiply the speed y by -1 when you hit the horizontal wall.

+2
source

Some comments on your actual code:

  • Both of these lines do the same:

     yPos -= (-y * gravity); yPos += (y * gravity); 

    similarly, both of these lines do the same thing:

     xPos += (-x * gravity); xPos -= (x * gravity); 
  • You do not handle cases -1 <= x <= 1 or -1 <= y <= 1

Some comments on the concepts:

  • Start with one ball.
  • You want the ball to have two separate speeds (both positive and negative), one in the x direction and one in the y direction. Each frame adds these speeds to the position of the ball.
  • Then add collision detection. Check each frame if the center of the ball is off the screen. If so, make it a rebound (the easiest way to do this is to make a positive momentum of the ball y when it turns off the most negative value of y displayed on the screen and do the same for the other edges of the screen).
  • Improve collision detection: see if you can figure out how to check if any part of the ball is off the screen (hint: to check if the ball is on the left side of the screen, you only need to check the leftmost coordinate of the ball)
  • Then consider gravity - gravity will be a constant number subtracted from the y-speed of each frame until y-speed reaches 0.
+2
source

Source: https://habr.com/ru/post/1333102/


All Articles