Game Development Reference
In-Depth Information
Figure 11-13. Making the bounding sphere smaller to better fit an object
That's, of course, a very cheap trick, but it turns out that, in many situations, it is more than
sufficient to keep up the illusion of mostly correct collision detection.
So how do we collide two spheres with each other? Or rather, how do we test for overlap? It
works exactly the same as in the case of circles! All we need to do is measure the distance from
the center of one sphere to the center of the other sphere. If that distance is smaller than the two
radii of the spheres added together, then we have a collision. Let's create a simple Sphere class.
Listing 11-14 shows the code.
Listing 11-14. Sphere.java, a Simple Bounding Sphere
package com.badlogic.androidgames.framework.math;
public class Sphere {
public final Vector3 center = new Vector3();
public float radius;
public Sphere( float x, float y, float z, float radius) {
this .center.set(x,y,z);
this .radius = radius;
}
}
That's the same code that we used in the Circle class in Chapter 8. All we changed is the vector
holding the center, which is now a Vector3 instead of a Vector2 .
Let's also extend our OverlapTester class with methods to check for overlap of two spheres and
to test whether a point is inside a sphere. Listing 11-15 shows the code.
 
Search WWH ::




Custom Search