views:

785

answers:

5

How do games with gravity handle the relationship between moving things like players, monsters, or objects and the floor? Is the player constantly "falling into" the floor and being bounced back up?

Two ways to react to collisions that I have found are moving the player back to his previous location before the collision, and testing the new position before moving to see if it would result in a collision, but I don't see how either of these could deal with a platform that is rising upwards and needs to be able to lift the player. I'm looking at this from a 2D game design perspective, but I imagine that the same problem occurs in 3D game design. Any hints? Any references that I should check out? Thanks.

+1  A: 

One of the most complete textbooks on this subject is Realtime Collision Detection by Christer Ericson. He has a companion blog too. Eric Lengyel's Mathematics for 3D Game Programming and Computer Graphics is useful too.

Crashworks
+1  A: 

I'm not specifically a games programmer, but this is my understanding:

  • in an ideal world with an "infinite frame rate", you'd detect the collision at precisely the moment it occurred, and use a bit of standard physics to model the new velocities and accelerations of the bodies following the collision (see a standard high school mechanics textbook, or various books entitled things like "Physics for Games Programmers")
  • in reality, because you have a fixed frame rate and hence bodies only move with a certain granularity, you usually need to add an extra trick, such as calculating in advance the relative path that bodies will travel on the next frame and seeing if any of the paths intersect
  • if they do intersect, then the point of intersection will actually be an estimate, but slightly inaccurate, of the point at which the bodies really would have collided; you then have the choice of not caring and taking that estimate as the point of intersection and linearly interpolating to get the velocities at the point of collision, or doing a more precise calculation now you've found out that they will intersect, to get the actual point/time/velocities of collision
Neil Coffey
+1  A: 

In addition to modeling collisions, a good approach is to also model continuing transfers of energy (or momentum, or just velocity, depending on the complexity of your simulation). When your player is standing on a platform, store that information in the object representing the platform, and any time the velocity of the object is adjusted you can directly apply that change to the thusly linked player or other objects.

Sparr
+9  A: 
Simucal
I had to type this answer twice, lol. There has got to be some firefox addon that prevents the forward/back button from clearing fields. Sooo frustrating.
Simucal
+2  A: 

One approach used in some games is to cheat: have a separate state for walking vs in the air. While walking, the game engine can determine the slope of the surface being walked over and, if not too steep, move the character in the direction of the surface as well as giving the character the proper vertical placement relative to the surface.

As for physics, I'm becoming a fan of verlet integration as described in Gamasutra: Advanced Character Physics. It simplifies physics update equations (don't have to keep track of velocity!) and it simplifies collisions (don't have to adjust velocity!). That said, it does have a few nuances if you need accuracy.

Paul
Very interesting articles.
MizardX