Thursday, January 23, 2020

architecture - 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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...