Game Development Reference

In-Depth Information

To figure this out, the code compares the `t and `u variables against zero to find out whether the

overlap is happening on the top, bottom, left, or right. Figure 7-39 is a line-by-line explanation.

eb$kt:,%

w

r]n`u6Jqi^an9k^fa_p=*u)k^fa_p>*u7

r]nku6Jqi^an9k^fa_p>[D]hbdaecdp'k^fa_p=[D]hbdaecdp)I]pd*]^o$`u%7

eb$ku:,%

w

eb$kt8ku%

w

eb$`t8,%

w

++?khheoekjkjnecdp

ku9,7

y

ahoa

w

++?khheoekjkjhabp

kt&9)-7

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

If the overlap on the x axis (ox) is more than zero, a collision might be occurring,

so it would be a good idea to check for an overlap on the y axis, too.

Figure out the distance between the objects on the y axis (dy).

Figure out the overlap on the y axis (oy).

Check whether the y overlap is greater than zero.

If it is, a collision is definitely occurring, and you need to figure out on which side.

If the x overlap is less than the y overlap, then the collision

is happening on the x axis.

If the distance between the objects on the x axis

is less than zero, then the collision is happening on the right.

Because this is an x axis collision,

you can ignore the y overlap by setting it to zero.

(The x overlap will remain positive to push the object to the right.)

If the distance between the objects on the x axis

is greater than zero, then the collision is happening on the left.

You need to multiply the x overlap by -1 to push the object to the left.

You can again ignore the y overlap by setting it to zero.

If the x overlap is greater than the y overlap, then the collision

is happening on the y axis.

If the distance between the objects on the y axis

is less than zero, then the collision is happening on the top.

Because this is a y axis collision, you can ignore the x overlap by setting it to zero.

You need to multiply the y overlap by -1 to push the object up.

If the distance between the objects on the y axis

is greater than zero, then the collision is happening on the bottom.

We can again ignore the x overlap by setting it to zero.

(The y overlap will remain positive to push the object down.)

Right

A collision is occuring

on the x axis.

Left

Top

A collision is occuring

on the y axis.

Bottom

Assign the newly calculated x overlap to the object's x position.

Assign the newly calculated y overlap to the object's y position.

Although this code may seem complex at first glance, all it does is figure out which side the collision

is happening on. It then assigns the correct overlap value to the object's x and y position to move it out

of the collision. The overlap values, “ox” and “oy,” can have only one of four possible combinations.

Figure 7-39.
Axis-based collision detection

This code repositions the object with deadly accuracy and it also works for 3D objects. All you need

to do is one further check on the z axis, and apply the same logic. I don't discuss 3D in this topic, but

if you do go on to build any 3D games in AS3.0 in future, you have all the makings of a 3D collision

system right here. Axis-based collision is the keystone to understanding advanced collision detection

in games.