0
\$\begingroup\$

So I have a spatial hashing system set up and it works. It puts players in buckets based on their x/y/width/height

So I was going to check collisions on each frame when I loop through the entity for updating their physics. But how would I go about duplicate collisions? Say I'm checking for player a, which is in the same bucket as bullet a.. I check if there's a collision and there is, should I do the collision logic right there? Or should I add the collision to a list and iterate through that list at the end of the frame to handle the logic for each collision? This way I can also apply logic to prevent duplicate collisions. Or should I just do all the logic right there and then when the initial collision is made?

\$\endgroup\$
1
\$\begingroup\$

Check each entity against every entity that comes after it in the list, to prevent duplicate collisions. So if you are on entity n, check it against n+1, n+2 etc. to the end of the list.

As to when you resolve your collision: that's up to you. I would recommend doing all detections first and then doing all your resolutions, in a way similar to what you recommend (add collisions that need to be resolved, to a list).

The problem is that if you resolve the collisions right then and there before you've finished checking for all collisions, you're modifying state which may (by some means or another) cause certain things to get missed. So best do this in two discrete phases.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.