Game Development Reference
In-Depth Information
ahoa
w
++?khheoekjkjhabp
ku9,7
y
y
ahoa
w
eb\$`u8,%
w
++?khheoekjkjPkl
kt9,7
ku&9)-7
y
ahoa
w
++?khheoekjkj>kppki
kt9,7
y
y
++Qoapda_]h_qh]pa`t]j`ukranh]lopk
++Ikrak^fa_p=kqpkbpda_khheoekj
k^fa_p=*t)9kt7
k^fa_p=*u'9ku7
y
Briefly, this is what's happening:
1. The code first checks to see whether there's an overlap on the x axis.
2. If there is, it checks for an overlap on the y axis.
3. If there's an overlap on both axes, a collision must be occurring.
That's the key to how this system works.
If you know a collision is occurring, the next step is to find out where the collision is happening. If, for
example, the player is colliding with the wall, you need to know whether the collision is happening on
the top, bottom, left, or right side of the wall. If the overlap is happening on the left side of the wall,
you need to subtract the amount of overlap from the player's t position to push it back toward the
left. If the overlap is happening on the right side of the wall, you need to add the amount of overlap to
the player's t position to push it to the right. If a collision is occurring on the top or bottom, you need
to do the same kind of repositioning. All this depends on knowing the direction of the collision.