Game Development Reference
In-Depth Information
Adjusting for position changes is easy: we simply move the bounding shape accordingly. In the
case of the triangle mesh, move each vertex; in the case of the bounding rectangle, move the
lower-left corner; and in the case of the bounding circle, move the center.
Scaling a bound shape is a little harder. We need to define the point around which we scale. This
is usually the object's position, which is often given as the center of the object. If we use this
convention, then scaling is easy. For the triangle mesh, we scale the coordinates of each vertex;
for the bounding rectangle, we scale its width, height, and lower-left corner position; and for the
bounding circle, we scale its radius (the circle center is equal to the object's center).
Rotating a bounding shape is also dependent on the definition of a point around which to rotate.
Using the convention just mentioned (where the object center is the rotation point), rotation also
becomes easy. In the case of the triangle mesh, we simply rotate all vertices around the object's
center. In the case of the bounding circle, we do not have to do anything, as the radius will stay the
same no matter how we rotate our object. The bounding rectangle is a little more involved. We need
to construct all four corner points, rotate them, and then find the axis-aligned bounding rectangle
that encloses those four points. Figure 8-9 shows the three bounding shapes after rotation.
Figure 8-9. Rotated bounding shapes, with the center of the object as the rotation point
While rotating a triangle mesh or a bounding circle is rather easy, the results for the axis-aligned
bounding box are not all that satisfying. Notice that the bounding box of the original object fits
tighter than its rotated version. There is a bounding box variant, called oriented bounding shape ,
that works better for rotation, but its disadvantage is that it's harder to calculate. The bounding
shapes covered so far are more than enough for our needs (and most games out there). If you
want to know more about oriented bounding shapes and really dive deep into collision detection,
we recommend the topic Real-Time Collision Detection , by Christer Ericson.
Another question is: how do we create our bounding shapes for Bob in the first place?
Constructing Bounding Shapes
In the example shown in Figure 8-8 , we simply constructed the bounding shapes by hand, based
on Bob's image. But what if Bob's image is given in pixels, and your world operates in, say,
meters? The solution to this problem involves normalization and model space. Imagine the two
triangles we use for Bob in model space when we render him with OpenGL ES. The rectangle
is centered at the origin in model space and has the same aspect ratio (width/height) as Bob's
texture image (that is, 32×32 pixels in the texture map, as compared to 2×2 m in model space).
Now we can apply Bob's texture and figure out the locations of the points of the bounding shape
in model space. Figure 8-10 shows how we can construct the bounding shapes around Bob in
model space.
 
Search WWH ::




Custom Search