0
\$\begingroup\$

I am starting work on a new Android game and I haven't had any experience implementing physics before. I am trying to steer clear of external libraries such as box2d, not just because I think it is overkill for my project, but I also want to code it myself for the experience. I am more just interested in getting 1 ball to bounce around the screen and bounce off of objects in my world. The game is in 2D and I was just looking for some advice on where to start with ball physics? I know I will need gravity, x/y velocities... etc. I know I need to calculate how high the ball needs to bounce based on how fast it hit the object, I also know I will need to calculate the correct angle that the ball needs to bounce at. Just hoping to get some ideas on a good place to start and things I will need to take into account.

\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

As you stated, you need to start with forces like gravity and velocity. Define some constants for each of the forces, for example gravity = 0.1; airResistance = 0.05; etc.

Next step is to set a position on the center of the screen and a velocity vector on the ball, with x as a random number between 1 and 2 and y = 0.

In the update loop for the game update the ball's velocity by adding the gravity and subtracting the airResistance, use Pythagorian Theorem to determine what amount of resistance the ball has on each axis. You can imagine if the ball falls straight down, you need to subract the resistance value from the balls y velocity. If the ball were to fly straight to the left, you need to subtract it from the x velocity etc.

When this works, next step is to determine collision with other objects and the screens borders. Lot's of articles can be found about this. (you'll need to check for collision on the current position + the velocity vector)

When a collision is detected, change the direction of the ball accordingly (assuming there's no elasticity and no force lost on impact) If the point of impact is on the left or right, invert x axis, etc

when no collision is found, set the new position for the ball as old position + velocity vector (both x and y).

These are the very basic steps on how to do this, values need to be modified based on the number of updates / sec and need to be balanced for the right effect.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks! I appreciate your thoughts, this sounds pretty close to what I was thinking so that's good. I'll give it a try and start playing around with the values and see if I can get it to look right. All of my collisions are against vertical and horizontal walls. I just wanted to make sure I was thinking about everything correctly. I'm thinking it shouldn't be too hard. I just need a realistic looking bounce on one object, which is the only object moving on the screen. \$\endgroup\$
    – DRiFTy
    Feb 14, 2012 at 13:29
  • \$\begingroup\$ As long as your collisions are this basic, and only one object is moving it should be pretty straightforward. Nice things to add are elasticity (on collision lose % of velocity), weight and air resistance according to size \$\endgroup\$
    – Thomas
    Feb 14, 2012 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .