Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bouncing ball

So i'm just trying to make a ball bounce around the screen which should slow down due to gravity and reflect (bounce) from the wall like a normal ball would. Can someone give some basics and VERY simple implementation of this? Other examples seem a bit "overdone" and seem to go beyond what I want to do. I've 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 I got by myself. By the way the x and y values are from the accelerometer. Any help would be much appreciated, thanks!

like image 731
semajhan Avatar asked Dec 28 '22 03:12

semajhan


2 Answers

I think you'll need 3 things for this, forces (x and y, which you have), velocities (call them xVel and yVel) and positions (xPos and yPos which you also have). The position of the ball is updated (in the simplest way) by:

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

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

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

Then, to get the ball to reflect from the walls, just flip the velocity if it hits a 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. The 'xPos = ...' is just to stop the ball going off the edge of the screen. If you'd like the ball to bounce a little less every time it hits a wall, change the '-1.0' to '-0.9' or something (this is the coefficient of restitution).

Hopefully that should be all. Good luck!

like image 67
conjugatedirection Avatar answered Jan 08 '23 00:01

conjugatedirection


Some comments on your actual code:

  • Both of these lines do exactly the same thing:

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

    likewise, both of these lines do the same thing:

    xPos += (-x * gravity);
    xPos -= (x * gravity);
    
  • You are not handling the 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 velocities (both either positive or negative), one in the x-direction and one in the y-direction. Every frame, add those velocities to the ball's position.
  • Then add collision detection. Check each frame if the center of the ball is outside the screen. If it is, make it bounce (the easiest way to do this is to make the ball's y-velocity positive when it goes off the most-negative y-value displayed on the screen; and do similarly for the other screen-edges).
  • Improve the collision detection: see if you can figure out how to check if any part of the ball it outside the screen (hint: to check if the ball is off the left-side of the screen, you only need to check the left-most coordinate of the ball)
  • Then take gravity into consideration - gravity will be a constant number subtracted from the y-velocity every frame, until the y-velocity hits 0.
like image 32
BlueRaja - Danny Pflughoeft Avatar answered Jan 08 '23 01:01

BlueRaja - Danny Pflughoeft