Bounding Sphere Overlap Testing

Figure 11-13. Making the bounding sphere smaller to better fit an object

Listing 11-13., 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) {,y,z); this.radius = radius;

That's the same code as in the Circle class. 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-14 shows the code.

Listing 11-14. Excerpt from, Adding Sphere-Testing Methods public static boolean overlapSpheres(Sphere s1, Sphere s2) { float distance =; float radiusSum = sl.radius + s2.radius; return distance <= radiusSum * radiusSum;

public static boolean pointInSphere(Sphere c, Vector3 p) { return < c.radius * c.radius;

public static boolean pointInSphere(Sphere c, float x, float y, float z) { return, y, z) < c.radius * c.radius;

That's again exactly the same code as in the case of Circle overlap testing. We just use the center of the spheres, which is a Vector3 instead of a Vector2 as in the case of a Circle.

NOTE Entire books have been filled on the topic of 3D collision detection. If you want to dive deep into that rather interesting world, I suggest the book Real-time Collision Detection by Christer Ericson (Morgan Kaufmann, 2005). It should be on the shelf of any self-respecting game developer!

Was this article helpful?

0 0

Post a comment