# BoundingSphere collision detection. I'm doing it wrong. It seems

Continuing the experimentation I’ve started with spheres merging (some transparency issues there), I’m trying to detect if two spheres are intersecting so I can (hopefully) merge them.

For testing I make 2 spheres, add bounding spheres to them and check if they intersect. Problem is, they seem to always intersect (at least partially).

Here’s what the output is telling me:

Merging spheres.

Sphere’s center = (0.0, 8.0, 0.0) Bounding sphere’s center = (0.0, -8.1340875E-7, -2.3841858E-7)

Bounding Sphere intersecting with other sphere’s (PINKY) bounding volume? true

Getting collision between ORANGY and PINKY

We have 152 collisions.

Collisions list:

Collision at null

[ snipped 150x ]

Collision at null

Sphere’s center = (0.0, -8.0, 0.0) Bounding sphere’s center = (0.0, -8.1340875E-7, -2.3841858E-7)

Bounding Sphere intersecting with other sphere’s (ORANGY) bounding volume? true

Getting collision between PINKY and ORANGY

We have 152 collisions.

Collisions list:

Collision at null

[ snipped 150x ]

Collision at null

The merging code that spews the above:

[java]

private void mergeSpheres() {

System.out.println(“Merging spheres.”);

for (int i = 0; i < sphList.size(); i++) {

Geometry s1 = sphList.get(i);

for (Geometry s : sphList) {

if (s1 != s) {

boolean intersect = s1.getModelBound().intersectsSphere(((BoundingSphere) s.getModelBound()));

//

System.out.println("Sphere's center = " + s1.getWorldTranslation() + " Bounding sphere's center = " + s1.getModelBound().getCenter());
System.out.println("Bounding Sphere intersecting with other sphere's (" + s.getName() + ") bounding volume? " + intersect);
System.out.println("Getting collision between " + s1.getName() + " and " + s.getName());
//
CollisionResults colRes = new CollisionResults();
int coll = s1.collideWith(s.getModelBound(), colRes);
if (coll > 0) {
System.out.println("We have " + coll + " collisions.");
mergeSpheres(s1, s, colRes);
} else {
System.out.println("We have no collision.");
}
}
}
}
}

private void mergeSpheres(Geometry g1, Geometry g2, CollisionResults colRes) {
System.out.println("Collisions list: n");
for (int i = 0; i < colRes.size() ; i++) {
CollisionResult cr = colRes.getCollision(i);
System.out.println("Collision at " + cr.getContactPoint());
}
}
[/java]
---
Finally the scene with the spheres + bounding spheres.

http://www.disenthral.com/files/collisionspheres.png
---

Ultimately the goal is to merge spheres into one mesh, if possible, to delineate the frontiers of an alien race in the game world where each star system (point sprite) has a sphere of X radius around it.

You could do a simple sphere collision yourself.

1 Like

Indeed, but for the purpose of merging spheres, using collision detection I can get the vertices intersecting. Of course I still could do that manually, but I’d prefer if I could use the above.

Thanks for the suggestion though.

Indeed, but for the purpose of merging spheres, using collision detection I can get the vertices intersecting. Of course I still could do that manually, but I'd prefer if I could use the above.

Thanks for the suggestion though.

Can you show the code where you create the spheres and the boundingspheres? The output looks pretty strange

Here it is.

Sorry for the messiness.

[java]

public void simpleInitApp() {

setKeys();

Sphere sphere = new Sphere(30, 30, 5f);

sphere1 = new Geometry("ORANGY", sphere);

Material mat0 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

mat0.setColor("Color", new ColorRGBA(0, 0, 1f, .5f));

mat0.setTransparent(true);

sphere1.setLocalTranslation(0, 8f, 0);

sphere1.setQueueBucket(RenderQueue.Bucket.Transparent);

sphere1.setMaterial(mat0);

sphere2 = new Geometry("PINKY", sphere.clone());

Material mat1 = mat0.clone();

mat1.setColor("Color", new ColorRGBA(0, 1f, 0, .5f));

sphere2.setMaterial(mat1);

sphere2.setLocalTranslation(0, -8f, 0);

Box b = new Box(new Vector3f(0, 0, -10), 2, 2, 2);

Geometry geom = new Geometry("Box", b);

Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

mat.setColor("Color", ColorRGBA.Red);

geom.setMaterial(mat);

rootNode.attachChild(geom);

rootNode.attachChild(sphere1);

rootNode.attachChild(sphere2);

flyCam.setMoveSpeed(50f);

viewPort.setBackgroundColor(ColorRGBA.Brown);

BoundingSphere bS = ((BoundingSphere) sphere1.getModelBound());

ws1.fromBoundingSphere(bS);

Geometry debug1 = new Geometry("Orangy", ws1);

Material matDeb = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

matDeb.setColor("Color", ColorRGBA.Orange);

debug1.setMaterial(matDeb);

debug1.setLocalTranslation(bS.getCenter());

BoundingSphere bS2 = ((BoundingSphere) sphere2.getModelBound());

ws2.fromBoundingSphere(bS2);

Geometry debug2 = new Geometry("Pinky", ws2);

Material matDeb2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

matDeb2.setColor("Color", ColorRGBA.Pink);

debug2.setMaterial(matDeb2);

debug2.setLocalTranslation(bS2.getCenter());

sphere1.updateModelBound();

sphere2.updateModelBound();

rootNode.attachChild(debug1);

rootNode.attachChild(debug2);

rootNode.updateGeometricState();

}

[/java]

Over a week and no reply.

I might just brute-force it the way @zzuegg suggested.