Hello! I am creating a simulation of stars and planets using jMonkeyEngine 3.1.0. I would like to detect when two objects collide. I have looked all over and I feel like my code is right but the collision() method is never being called - even when I clearly see the two objects collide.
I have my planets/stars behaving correctly and moving correctly. I see the Sun and the user created object collide on screen, and again, I simply don’t see the collision() method being called.
If anyone could look at my code and let me know what I am missing, I would really appreciate it! Hopefully I have overlooked something obvious.
Here are the relevant code blocks:
public class Main extends SimpleApplication implements PhysicsCollisionListener, PhysicsTickListener
{
// Game related variables
private Boolean isRunning=true;
private NBodySystem bodies;
private Geometry sunGeo, saturnGeo, uranusGeo, neptuneGeo, userGeo = null;
private Sphere sunMesh, saturnMesh, uranusMesh, neptuneMesh, userMesh;
private Spatial jupiterMesh;
private RigidBodyControl sunPhysControl, userPhysControl;
private SphereCollisionShape userCollisionShape, sunCollisionShape;
private ParticleEmitter jupiterTrailEmitter, saturnTrailEmitter, uranusTrailEmitter, neptuneTrailEmitter, sunTrailEmitter, userTrailEmitter;
private float zoom = 1.05f;
private float camZ = 10.0f;
private Boolean userCreated = false;
/** Prepare the Physics Application State (jBullet) */
private BulletAppState bulletAppState;
public static void main(String[] args)
{
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp()
{
// Set up Physics
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0, 0, 0));
bulletAppState.getPhysicsSpace().addTickListener(this);
bulletAppState.getPhysicsSpace().addCollisionListener(this);
bulletAppState.setDebugEnabled(true);
// Setup the Sun
sunMesh = new Sphere(32, 32, 0.10f);
sunGeo = new Geometry("Sun", sunMesh);
Material sunMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
sunMat.setBoolean("UseMaterialColors",true);
sunMat.setColor("Diffuse", ColorRGBA.Yellow);
sunMat.setColor("Ambient", ColorRGBA.Yellow);
sunMat.setColor("GlowColor", ColorRGBA.Yellow);
sunGeo.setMaterial(sunMat);
sunGeo.setLocalTranslation(0.0f, 0.0f, 0.0f);
rootNode.attachChild(sunGeo);
sunCollisionShape = new SphereCollisionShape(0.10f);
sunPhysControl = new RigidBodyControl(sunCollisionShape, 1f);
sunGeo.addControl(sunPhysControl);
bulletAppState.getPhysicsSpace().add(sunPhysControl);
userMesh = new Sphere(24, 24, 0.07f);
userGeo = new Geometry("user_planet", userMesh);
Material userMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
userMat.setBoolean("UseMaterialColors",true);
userMat.setColor("Diffuse", ColorRGBA.Green);
userMat.setColor("Ambient", ColorRGBA.Green);
userGeo.setMaterial(userMat);
userGeo.setLocalTranslation((float)p.x, (float)p.y, (float)p.z);
rootNode.attachChild(userGeo);
userTrailEmitter = CreateEmitter();
rootNode.attachChild(userTrailEmitter);
userCollisionShape = new SphereCollisionShape(0.10f);
userPhysControl = new RigidBodyControl(userCollisionShape, 1f);
userGeo.addControl(userPhysControl);
bulletAppState.getPhysicsSpace().add(userPhysControl);
}
public void simpleUpdate(float tpf)
{
int num = bodies.getNumBodies();
// num = 5;
bodies.advance(0.01);
NBody nb;
for(int i=0; i<num; ++i)
{
nb = bodies.bodies.get(i);
float pX = (float)nb.x;
float pY = (float)nb.y;
zoom = .125f;
pX = pX * zoom;
pY = pY * zoom;
if(i == 0 && sunGeo != null)
{
sunPhysControl.setEnabled(false);
sunGeo.setLocalTranslation(pX, pY, 0f);
sunPhysControl.setPhysicsLocation(new Vector3f(pX, pY, 0f));
sunPhysControl.setEnabled(true);
sunTrailEmitter.setLocalTranslation(pX, pY, 0f);
}
if(i == 1)
{
jupiterMesh.setLocalTranslation(pX, pY, 0f);
jupiterTrailEmitter.setLocalTranslation(pX, pY, 0f);
}
if(i == 5 && userGeo != null)
{
userPhysControl.setEnabled(false);
userGeo.setLocalTranslation(pX, pY, 0f);
userPhysControl.setPhysicsLocation(new Vector3f(pX, pY, 0f));
userPhysControl.setEnabled(true);
userTrailEmitter.setLocalTranslation(pX, pY, 0f);
}
}
}
@Override
public void prePhysicsTick(PhysicsSpace space, float tpf) {
//System.out.println("PreTick!");
}
@Override
public void physicsTick(PhysicsSpace space, float tpf) {
//System.out.println("Tick!");
}
@Override
public void collision(PhysicsCollisionEvent event) {
System.out.println("Colission detected!");
}
}