Collision Detection problem

Hello all,



I’m currently working on a simpe Asteroids clone as a study project. While it is progressing nicely, I’ve run into a small problem.



I’ve got a problem with my collision detection. I have collision detection setup between the ship and the asteroids. This works nicely:



[java]

private void testColliding() {

for (Node roid : asteroids) {

if (roid.getWorldBound().intersects(ship.getWorldBound())) {

System.out.println(“BOOM!!”);

}

}

}

[/java]



Howeve, I’m using more or less the same code to detect if my bulletNode hits an asteroid node:

[java]

private void testHit() {

if (bulletNode != null) {

for (Node roid : asteroids) {

if (roid.getWorldBound().intersects(bulletNode.getWorldBound())) {

System.out.println("bullet hit roid " + roid.getName());

}

}

}

}

[/java]



This however doesn’t do anything. The only difference I can see is that the testHist() checks for an available bulletNode first. I can see it entering that if loop though if I add more System.out.prinln() statements there.



If anyone can tell me why the first method works, but not the second I would be grateful. Oh and if you need more code, I’m more then happy to post it here. This is only study project, so no secret code here.

Any number of things could be going wrong here. If it were my code, I’d print (or look at in a debugger) the values of roid.getWorldBound() and bulletNode.getWorldBound() before the if statement.



For example, it could be that they are not in the same “plane” relative to one another. Or that the bullet bounds is somehow not what you expect.

Ok, I can follow that. And I did as you said. There’s a big difference between the two worldbounds. So now I’m thinking it might have something to with how I shoot the bullet:



[java]

private void shoot() {

Geometry b = bullet.clone();

bulletNode = new Node(“bullet_node”);

bulletNode.attachChild(b);

bulletNode.setLocalRotation(ship.getLocalRotation());

bulletNode.setLocalTranslation(ship.getLocalTranslation());

rootNode.attachChild(bulletNode);

}

[/java]



Most likely the way I set the rotation of the bullet. Since my game is actually 2D, just with 3D objects. Everything is in the X-Z plane. Nut I’m thinking this: bulletNode.setLocalRotation(ship.getLocalRotation()); is where things go wrong. Can’t figure out another way to copy the rotation of the ship though.

What were the bounds when you printed them?

testing Roid bounds: BoundingSphere [Radius: 1.001358E-5 Center: (-0.5837944, 0.0, 2.7902367)]

to Bullet bounds: BoundingSphere [Radius: 1.001358E-5 Center: (0.0, 0.0, 6.435819)]

And what about the ship version?



This bounding spheres are both super teeny tiny for some reason. 0.0001 meters yet they are several meters apart.



How are you creating the bullet and the roids? Is anything scaled? Are these all children of the root node?

Nothing is scaled, everything is a child of the root node:



creation of the ship node:

[java]

Cylinder n = new Cylinder(30, 30, 0.25f, 1f);

Geometry nose = new Geometry("Nose", n);

Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");

mat.setBoolean("m_UseMaterialColors", true);

mat.setColor("m_Ambient", ColorRGBA.Gray);

mat.setColor("m_Diffuse", ColorRGBA.Blue);

mat.setColor("m_Specular", ColorRGBA.White);

mat.setFloat("m_Shininess", 1);

nose.setMaterial(mat);



Sphere b = new Sphere(30, 30, 1f, true, false);

Geometry body = new Geometry("Body", b);

body.setMaterial(mat);

body.setLocalTranslation(0, -0.5f, 0);

ship = new Node("Ship");

ship.attachChild(nose);

ship.attachChild(body);



ship.getChild("Nose").setLocalTranslation(0, 0, 1);

ship.getChild("Body").setLocalTranslation(0, 0, 0);



ship.setModelBound(new BoundingSphere());

rootNode.attachChild(ship);

[/java]



Creation of the asteroids:

[java]

Sphere b = new Sphere(30, 30, 1f, true, false);

asteroid = new Geometry("Asteroid", b);

Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");

mat.setBoolean("m_UseMaterialColors", true);

mat.setColor("m_Ambient", ColorRGBA.Gray);

mat.setColor("m_Diffuse", ColorRGBA.Brown);

mat.setColor("m_Specular", ColorRGBA.LightGray);

mat.setFloat("m_Shininess", 1);

asteroid.setMaterial(mat);

asteroid.setModelBound(new BoundingSphere());



for (int i = 0; i < level; i++) {

Geometry r = asteroid.clone();

r.setName("roir_geo_" + i);

Node roid = ship.clone(false);// new Node("roid_node_" + i);

roid.setName("roid_node_" + i);

roid.detachAllChildren();

roid.attachChild®;

FastMath.rand.setSeed(System.currentTimeMillis());

roid.rotate(0, FastMath.rand.nextFloat() * i, 0);

asteroids.add(roid);

rootNode.attachChild(roid);

}

[/java]



creation of bullet:

[java]

Sphere b = new Sphere(30, 30, 0.2f, true, false);

bullet = new Geometry("Bullet", b);

Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");

mat.setBoolean("m_UseMaterialColors", true);

mat.setColor("m_Ambient", ColorRGBA.LightGray);

mat.setColor("m_Diffuse", ColorRGBA.Cyan);

mat.setColor("m_Specular", ColorRGBA.White);

mat.setFloat("m_Shininess", 1);

bullet.setMaterial(mat);

bullet.setModelBound(new BoundingSphere());



Geometry b = bullet.clone();

bulletNode = ship.clone(false);// new Node("bullet_node");

bulletNode.detachAllChildren();

bulletNode.attachChild(b);

//bulletNode.rotate(0, ship.getLocalRotation().getRotationColumn(2).y, 0);

//bulletNode.setLocalTranslation(bulletNode.worldToLocal(ship.getWorldTranslation(), new Vector3f()));

rootNode.attachChild(bulletNode);

[/java]



Output of both the ship bounds and the bullet bounds:


testing Roid bounds: BoundingSphere [Radius: 1.001358E-5 Center: (0.0, 0.0, 11.557621)]
to Bullet bounds: BoundingSphere [Radius: 1.001358E-5 Center: (0.0, 0.0, 1.0656466)]
testing Roid bounds: BoundingSphere [Radius: 1.001358E-5 Center: (0.0, 0.0, 11.557621)]
to Ship bounds: BoundingSphere [Radius: 0.50001 Center: (0.0, 0.0, 1.0)]


Looks like, for some reason, the ship has a big radius and the rest a very tiny one...

Ok, the radius issue was the hint that set me on the right track. I set a radius to the bullet and asteroid boundedspheres and now it works…