Game Development Reference
In-Depth Information
Figure 7-35. The wall pushes the player back to inconsis-
tent positions.
Here's why it doesn't work well. Let's say that the right edge of the lh]uan object is at an t position
of 97, and the left edge of the wall is at an t position of 100. You know that the lh]uan object moves
5 pixels each frame. If you press the right arrow, 5 pixels will be added to the lh]uan object's position and
it will attempt to move to an t position of 102. However, it won't get that far because depPaopK^fa_p will
detect a collision and push the lh]uan object back 5 pixels. That leaves the lh]uan object back where
it started, with its right edge at an t position of 97 and a gap of 3 pixels between the lh]uan object and
the wall.
You can see then from this example that you don't really want to push the player back 5 pixels; you
want to push it back only 2 pixels, which is the depth with which it is penetrating the wall. You could
easily do that, except that you never know what the depth of the penetration will be. If the lh]uan
object started from an t position of 96, the depth would be 1 pixel; if it started from an t position of
99, the depth would be 4 pixels.
The problem is that you have no way of knowing by how many pixels the objects will overlap, and
depPaopK^fa_p has no way of telling you that. There is actually a very precise, bulletproof way to
figure it out, but to do this you need to ditch depPaopK^fa_p and say hello to axis-based collision
detection.
 
Search Nedrilad ::




Custom Search