I’m currently busy making a little game with some spheres in it that are moving randomly.
I added them to the physicsspace so they will bounce against each other and set the gravity to zero because they’re floating in space.
In the update method I make them move randomly.
Now i want to change the color of the spheres when a collision occurs.
I tried a lot of solutions but nothing works, i guess I’m doing something terribly wrong…
This is my code for making the spheres:
[java]
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
material();
Sphere a = new Sphere(15, 15, 1);
random = new Random();
for(int i = 0; i < geom.length; i++)
{
mController = new RigidBodyControl(0.01f);
geom[i] = new Geometry("Box", a);
geom[i].setMaterial(mat);
geom[i].setLocalTranslation(random.nextInt(20),random.nextInt(20),random.nextInt(20));
geom[i].addControl(mController);
mController.setKinematic(true);
rootNode.attachChild(geom[i]);
bulletAppState.getPhysicsSpace().add(geom[i]);
}
bulletAppState.getPhysicsSpace().setGravity(Vector3f.ZERO);
[/java]
in the update method i want to check if objects collide and i want to change the color of these geometries
I hope you can help me out, thnx in advance!
Use a PhysicsCollisionListener
Thnx, I will have a look at that!
All of the physicsCollisionListener tutorials and examples are about a controlled character that collides with some object.
I can’t get a hold on something that checks if 2 objects collide with each other
Can I get some more detailed advice? I’ve been searching the web all day again
Thnx in advance
I found out that I need a modelbound, so I created for every object (a sphere) a boundingSphere.
I don’t get an error anymore but I never get a collision neither.
I checked if the boundingSphere and the object were on the same spot and realized that the boundingSphere never moves…
How can I set the location of the boundingSphere to the same location as the object it is attached to?
[java]for(int i = 0; i < geom.length; i++)
{
mController = new RigidBodyControl(0.01f);
geom[i] = new Geometry("Box"+i, a);
geom[i].setMaterial(mat);
geom[i].setLocalTranslation(random.nextInt(50),random.nextInt(50),random.nextInt(50));
geom[i].addControl(mController);
geom[i].setModelBound(new BoundingSphere());
geom[i].updateModelBound();
mController.setKinematic(true);
rootNode.attachChild(geom[i]);
bulletAppState.getPhysicsSpace().add(geom[i]);
}[/java]
In the update method:
[java]for(int j = 0; j < geom.length; j++)
{
// Calculate detection results
CollisionResults results = new CollisionResults();
geom[0].collideWith(geom[j].getModelBound(), results);
//System.out.println(“Number of Collisions between” +
//geom[0].getName()+ " and " + geom[j].getName() + ": " + results.size());
// Use the results
if (results.size() > 0) {
// how to react when a collision was detected
CollisionResult closest = results.getClosestCollision();
System.out.println("What was hit? " + closest.getGeometry().getName() );
System.out.println("Where was it hit? " + closest.getContactPoint() );
System.out.println("Distance? " + closest.getDistance() );
} else {
// how to react when no collision occured
}
}[/java]
Actually that is not using the physics, but recalcualting stuff in jme.
You really want to add a listener to the bulletappstate / physicsspace.
Could you be a little more specific, I’m searching my ass off
bulletAppState.getPhysicsSpace().addCollisionListener(…)
I don’t know how this is done in netbeans, but I will bet there is a similar possible search workflow.
OpenType *collision -> open class physicscollisionlistener
show references -> see where this class is used, see the method signature to add it there.