Sign in to follow this  
kulan8

bouncing balls

Recommended Posts

I've made a simple 2dgame using tiles in C++ and SDL. At the moment all you can do is run around on the tiles, jump etc. I would like to add balls that bounce around the map and on the player. But don't really know how to make balls bounce against the walls and how to consider the angles etc. Anyone that can help me get started?

Share this post


Link to post
Share on other sites
The easiest solution would be to use an existing physics engine, such as Box2D or Chipmunk. (Note that implementing physics for platform-style or tile-based games using a general physics engine can be a bit tricky and involve some workarounds, but it can be done.)
Quote:
But don't really know how to make balls bounce against the walls and how to consider the angles etc.
For this sort of problem, you don't think in terms of angles, but rather in terms of vectors.

If you want to implement it yourself, you probably need (at least) a discrete circle-vs.-primitive intersection test. I don't know how your world is represented exactly, but the 'primitives' in question here could be (for example) boxes or line segments, depending.

Discrete tests are generally easier to code, but to avoid tunneling you'll have to limit the magnitude of the objects' displacements, or use an iterative approach. Or, you could use continuous tests, which are a little more involved to code.

The other part of the problem is collision response. Generally speaking, when a collision is detected, you would reflect the velocity vector across the collision normal, causing the ball to 'bounce'. In practice though, it can be somewhat more complicated than that (there are various things that have to be dealt with, such as numerical issues, objects getting 'stuck' or otherwise not behaving correctly, objects coming to rest of landing on one another, etc.). It is because of these difficulties that using an existing, proven physics engine can often be an easier route to take.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this