# Handling collisions with ground

How do you usually solve collisions between entities and the ground? Sending collision events hardly seems appropriate since almost everything touches the ground at almost all points in time. Calling collision handling functions doesn't sound any better.

How is this normally achieved? Is handling of slopes significantly harder?

-

When you detect a collision with the ground and the speed Y is low, you disable the gravity for that object, set the Y speed to 0 and skip the collision reactions against that floor (store a temporal reference to it) until its Y axis acceleration gets disturbed somehow (because of an external force such as a collision reaction or the character jumping or the floor getting out of range).

You can have many floors then you know that when you are over one floor you don't have to check collision against others, you only check for collision with the floor you know you were last frame, and if you don't collide with it then you wait until a floor object reports its collision and you can start the process again.

EDIT: I forgot to mention that if you're dealing with more realistic physics such as rigid bodies, checking the Y speed may be not enough for non-spherical objects (non-circular in 2d) as an angular velocity may affect your vertical force depending on the shape and momentum of the object.

Note that by identifying the ground objects you can exploit their mutual exclusivity properties in many ways, such as for collision with other objects (an object over a floor doesn't collide with objects that rest over another floor) similar to the way a quadtree works. You may also find place for some prediction there, depending on the rules of your game (more realistic physics will always be harder) you may be able to predict the floor where your object will (most likely) be next, then set that floor as a priority for ground collision checking.

Ah, and for if handling slopes is harder, it depends on the variety of angles for all slopes (in some games the slopes are always 45 degrees or so) and on the way you make your objects slide over a ground, ie you may want that if the Y velocity is high, you slide when falling on a slope, but if your Y velocity is low enough you could stop it. You may also want to add an effect to have different floor materials, so you would slide less over grass than over polished metal.

-

I'm not sure if any one else used same algorithm or not but for an adventure game we were developing we used a hidden polygon shape which specified ground level (and also some forbidden places). for each object we used a raycast directly toward a ground from that shapes center of mass and then picked ground polygon. in the last step we were only adjusting object altitude so that lowest object part be over ground level. in that algorithm we also checked if the first polygon picked was from forbidden area, we just reversed last movement, and it worked so well. you can also easily implement stairs and multiple ground levels with this algorithm.

the main positive point is you can ignore most of complex 3D Object collision and ground collision computations (by creating a moving forbidden area under every entity) but it almost doubles the work your level designers need to do.

-
Thanks, my game is 2D, but your advice still applies. – Paul Manta Sep 3 '11 at 17:28