Jump to content
  • Advertisement
Sign in to follow this  
Sabo

When to run the collision detection code?

This topic is 1862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently developing a 2D puzzle game. Moving entities are: the player, the enemies, projectiles. I am a bit unsure if I should run a collision check after I move each entity, or move all entities and then check for collisions. The latter makes more sense for a few reasons, one being the case where two entities would collide only after both have moved. If we run the collision detection after each entity moves the collision (and bounce, if that's what they do), would not look correct. Granted, it would probably not be noticable depending on how far they move and the framerate and what not, but still.

 

I just wanted to see if there is ONE answer to the question, or if not, thoughts on when to run use either of the options.

Share this post


Link to post
Share on other sites
Advertisement

Personally, I would run collision and then move entities. That would give you a frame where they are actually touching. Otherwise, I would check collision after moving all the entities as it makes more sense.

Share this post


Link to post
Share on other sites

Personally, I would run collision and then move entities. That would give you a frame where they are actually touching. Otherwise, I would check collision after moving all the entities as it makes more sense.

The problem with that is you usually move entities twice. One for their normal movement, either before or after the collision check, and one to shift them out of each other's collision boxes if they are colliding. I tend to use the second method personally.

 

I would run the collision after you move them, if you don't they'll spend a draw frame possibly inside of each other when in reality they should have moved back with collision resolution before they were drawn. If you just move them after they might collide, move back, then move right into each other again and be drawn that way.

Share this post


Link to post
Share on other sites

Don't do collision detection every single time you move an object (that is, don't move an object, do collision, move the next object, do collision again, etc.). That messes up time of impact ordering (that is, one object is at the new updated position while everything else is at the old positions... if you run collision detection code now it could collide with things that should have moved). Additionally, this is at least twice as expensive as it needs to be (in terms of computation).

 

Move all objects. Then do collision detection. It'll give correct time of impact ordering and it's more efficient.

 

If you're doing swept collision detection, you can solve for velocities, do collision detection, then integrate for positions. But it's still a "update them all at the same time en masse" concept.

Share this post


Link to post
Share on other sites

If you check collision each time the object is moved, you will have (virtually) something like

 

for(i=0; i<N;i++)

   for(j=0; j<N;j++)

 

But, if you check after the first movement part, you may do

 

for(i=0; i<N;i++)

   for(j=i; j<N;j++)

 

This apply to "a priori" (first setup velocities, then swept tests),

as well to "a posteriori" approaches (first setup new positions, then move after penetration tests).

Of course you may want to make this several times (time step, stability reasons, etc..)

 

The other thing is when do you react. One approach is

 

for(i=0; i<N;i++)

   for(j=i; j<N;j++)

   {

        check(i,j);

        react(i,j);

    }

 

and the another is

 

for(i=0; i<N;i++)

   for(j=i; j<N;j++)

       check(i,j);

for(i=0; i<N;i++)

   for(j=i; j<N;j++)

        react(i,j);

 

The first should converge faster (it is "semi-implicit" scheme),

but may be less accurate (it is order dependent).

Edited by mawigator

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!