When to run the collision detection code?

Started by
4 comments, last by Sabo 10 years, 11 months ago

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.

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.

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.

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.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

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

Yah, as I thought then. Thanks for all the input!

This topic is closed to new replies.

Advertisement