Friday, February 14, 2020

Collision Resolution


I know quite well how to check for collisions, but I don't know how to handle the collision in a good way.


Simplified, if two objects collide I use some calculations to change the velocity direction. If I don't move the two objects they will still overlap and if the velocity is not big enough they will still collide after next update. This can cause objects to get stuck in each other.


But what if I try to move the two objects so they do not overlap. This sounds like a good idea but I have realised that if there is more than two objects this becomes very complicated. What if I move the two objects and one of them collides with other objects so I have to move them too and they may collide with walls etc.


I have a top down 2D game in mind but I don't think that has much to do with it. How are collisions usually handled?


This question is asked on behalf of Wooh




Answer



Daniel Kodicek covers this topic in great detail in his book, Mathematics & Physics for Programmers.


Kodicek does two things to achieve natural-looking collision resolution:



  • His collision detection function calculates the exact time two objects will collide.

  • He recalculates new velocities at the time of collision, so objects never overlap.


I uploaded a demo based on Kodicek's collision detection and resolution.


update: Here's a collision detection & resolution algorithm that is very similar to Kodicek's method. With source code. I still recommend Kodicek's book, as his algorithm is implemented slightly differently and much more thoroughly explained.


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...