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). :confused:



Here’s what the output is telling me:



Merging spheres.



Sphere’s (ORANGY) radius = 5.0 Bounding sphere’s radius = 5.00001

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 (PINKY) radius = 5.0 Bounding sphere’s radius = 5.00001

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()));

float radius = ((BoundingSphere) s1.getModelBound()).getRadius();

float spRad = ((Sphere) s1.getMesh()).radius;

//


System.out.println("nSphere's (" + s1.getName() + ") radius = " + spRad + " Bounding sphere's radius = " + radius);
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.



(sphere1.getWorldLocation-sphere2.getWorldLocation).lenght() > (sphere1.radius+sphere2.radius)

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.

@madjack said:
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. :confused:

[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.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);

mat0.getAdditionalRenderState().setColorWrite(true);

mat0.getAdditionalRenderState().setDepthTest(true);

mat0.getAdditionalRenderState().setAlphaTest(true);

mat0.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);

// mat0.getAdditionalRenderState().setAlphaFallOff(.5f);

mat0.getAdditionalRenderState().setWireframe(true);

mat0.setTransparent(true);



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

sphere1.setQueueBucket(RenderQueue.Bucket.Transparent);

sphere1.setMaterial(mat0);

sphere1.setModelBound(new BoundingSphere(sphere.getRadius(), sphere1.getLocalTranslation()));



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);

sphere2.setModelBound(new BoundingSphere(sphere.getRadius(), sphere2.getLocalTranslation()));



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);

mat.getAdditionalRenderState().setColorWrite(true);

geom.setMaterial(mat);

rootNode.attachChild(geom);



rootNode.attachChild(sphere1);

rootNode.attachChild(sphere2);



flyCam.setMoveSpeed(50f);



viewPort.setBackgroundColor(ColorRGBA.Brown);

sphList.add(sphere1);

sphList.add(sphere2);



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

WireSphere ws1 = new WireSphere(bS.getRadius());

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());

WireSphere ws2 = new WireSphere(bS2.getRadius());

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. :confused:



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