Hello all,
I am not sure if I am misunderstanding how to use JBullet, but I am not getting the collision events that I am expecting. I have constructed a simple example that illustrates the type of thing I want to do and I will post the code below. In the example a have a floor and two balls. I apply an impulse to one ball so that they will collide. My listener gets the events for the collisions between each of the balls and the floor, but no event for the collision between the balls. It is clear that the collision is occurring since it can be viewed, so I do not understand why the listener does not get an event for it.
Also, I tried running TestCollisionListener from JmeTests and I seem to not get the events there either, so I am wondering if it is a problem with the version of JME I am using. The version of the SDK I am using is v3.2.1-stable-sdk3.
Thanks in advance.
Here is the code :
public class Main extends SimpleApplication
{
private PhysicsCollisionListener myCollisionListener = new PhysicsCollisionListener()
{
@Override
public void collision(PhysicsCollisionEvent event)
{
// if (!(event.getObjectB().getCollisionShape() instanceof PlaneCollisionShape))
{
System.err.println(event.getObjectA().getCollisionShape().getObjectId()
+ " :: " + event.getObjectB().getCollisionShape().getObjectId());
}
}
};
public static void main(String[] args)
{
Main app = new Main();
app.setShowSettings(false);
AppSettings settings = new AppSettings(true);
settings.setWidth(1768);
settings.setHeight(992);
settings.setFrameRate(60);
app.setSettings(settings);
app.start();
}
private static Sphere mySphere;
@Override
public void simpleInitApp()
{
getFlyByCamera().setEnabled(false);
createMaterial();
createBulletAppState();
createFloor();
Vector3f camLoc = new Vector3f(0f, 80f, 220f);
cam.setLocation(camLoc);
Quaternion quat = new Quaternion();
quat.lookAt(camLoc.mult(-1f), Vector3f.UNIT_Y);
cam.setRotation(quat);
float radius = 3f;
mySphere = new Sphere(32, 32, radius, true, false);
mySphere.setTextureMode(Sphere.TextureMode.Projected);
RigidBodyControl ballBodyA = createBall(3);
ballBodyA.setPhysicsLocation(new Vector3f(0f, 3f, 0f));
RigidBodyControl ballBodyB = createBall(3);
ballBodyB.setPhysicsLocation(new Vector3f(40f, 3f, 0f));
ballBodyA.applyImpulse(new Vector3f(20f, 0, 0), Vector3f.ZERO);
}
BulletAppState myBulletAppState;
public void createBulletAppState()
{
myBulletAppState = new BulletAppState();
myBulletAppState.setEnabled(true);
myBulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
stateManager.attach(myBulletAppState);
myBulletAppState.getPhysicsSpace().addCollisionListener(myCollisionListener);
}
public void createFloor()
{
Box floorBox = new Box(140f, 0.25f, 140f);
Geometry floorGeometry = new Geometry("Floor", floorBox);
floorGeometry.setMaterial(myMaterial);
floorGeometry.setLocalTranslation(0f, -0.25f, 0f);
Plane plane = new Plane();
plane.setOriginNormal(Vector3f.ZERO, Vector3f.UNIT_Y);
RigidBodyControl floorBody = new RigidBodyControl(new PlaneCollisionShape(plane), 0);
floorGeometry.addControl(floorBody);
rootNode.attachChild(floorGeometry);
myBulletAppState.getPhysicsSpace().add(floorGeometry);
}
Material myMaterial;
public void createMaterial()
{
myMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture tex = assetManager.loadTexture("Textures/fabric1.jpg");
tex.setWrap(Texture.WrapMode.Repeat);
myMaterial.setTexture("ColorMap", tex);
}
public RigidBodyControl createBall(float radius)
{
Geometry ball = new Geometry("ball", mySphere);
ball.setMaterial(myMaterial);
SphereCollisionShape collShape = new SphereCollisionShape(radius);
RigidBodyControl ballBody = new RigidBodyControl(collShape);
ball.addControl(ballBody);
rootNode.attachChild(ball);
myBulletAppState.getPhysicsSpace().add(ballBody);
return ballBody;
}
}