bouncing balls

Started by
1 comment, last by bzroom 13 years, 12 months ago
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?
Advertisement
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.
//rectify positionrb.Position += c->Normal * c->Penetration;float vDotN = Dot(rb.Velocity, c->Normal);if (vDotN < 0){	//moving towards contact, apply impulse	Vector deltaV = c->Normal * vDotN * (-1 - rb.CoefRestitution);	rb.Velocity += deltaV;					}


From: http://www.gamedev.net/community/forums/viewreply.asp?ID=3549796

This topic is closed to new replies.

Advertisement