Opinions on collision detection objects with a moving scene
Posted
by Evan Teran
on Stack Overflow
See other posts from Stack Overflow
or by Evan Teran
Published on 2010-03-27T15:24:21Z
Indexed on
2010/03/27
15:33 UTC
Read the original article
Hit count: 266
collision-detection
|language-agnostic
So my question is simple, and I guess it boils down to how anal you want to be about collision detection. To keep things simple, lets assume we're talking about 2D sprites defined by a bounding box. In addition, let's assume that my sprite object has a function to detect collisions like this: S.collidesWith(other);
Finally the scene is moving and "walls" in the scene can move, an object may not touch a wall.
So a simple implementation might look like this (psuedo code):
moveWalls();
moveSprite();
foreach(wall as w) {
if(s.collidesWith(w)) {
gameover();
}
}
The problem with this is that if the sprite and wall move towards each other, depending on the circumstances (such as diagonal moment). They may pass though each other (unlikely but could happen).
So I may do this instead.
moveWalls();
foreach(wall as w) {
if(s.collidesWith(w)) {
gameover();
}
}
moveSprite();
foreach(wall as w) {
if(s.collidesWith(w)) {
gameover();
}
}
This takes care of the passing through each other issue, but another rare issue comes up. If they are adjacent to each other (literally the next pixel) and both the wall and the sprite are moving left, then I will get an invalid collision since the wall moves, checks for collision (hit) then the sprite is moved. Which seems unfair. In addition, to that, the redundant collision detection feels very inefficient. I could give the player movement priority alleviating the first issue but it is still checking twice.
moveSprite();
foreach(wall as w) {
if(s.collidesWith(w)) {
gameover();
}
}
moveWalls();
foreach(wall as w) {
if(s.collidesWith(w)) {
gameover();
}
}
Am I simply over thinking this issue, should this just be chalked up to "it'll happen rare enough that no one will care"? Certainly looking at old sprite based games, I often find situations where the collision detection has subtle flaws, but I figure by now we can do better :-P. What are people's thoughts?
© Stack Overflow or respective owner