Collision detection with GhostControl

I’m pretty new to JME and I’m trying to understand how the collision detection works when using a GhostControl object.



Here is the physics related initialization I’m doing:

[java]

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);



CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(sceneModel);

landscape = new RigidBodyControl(sceneShape, 0);

sceneModel.addControl(landscape);



CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1f, 2f, 1);

player = new GhostControl(capsuleShape);



bulletAppState.getPhysicsSpace().add(landscape);

bulletAppState.getPhysicsSpace().add(player);

[/java]



And here is the physics related part of my update loop:

[java]

Vector3f lastLocation = player.getPhysicsLocation().clone();



if (down){

delta.y = -playerSpeedtpf;

} else if (up){

delta.y = playerSpeed
tpf;

} else {

delta.y = 0;

}

player.setPhysicsLocation(player.getPhysicsLocation().clone().addLocal(delta));



if (player.getOverlappingObjects().contains(landscape)){

System.out.println(“Colliding”);

player.setPhysicsLocation(lastLocation);

}

[/java]



The problem I’m having is that the player object is colliding with the landscape object, but instead of getting pushed back to a valid location, it gets stuck inside the landscape object. I feel like I’m missing something really stupid, but I can’t seem to figure out what it is. Can anyone help?

Nevermind, in my research I came across the PhysicsTickListener which solved the problem. I was not aware that you could not check collisions in the main update loop.

Maybe the tutorials will explain https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics



“A GhostControl itself is non-solid and invisible. GhostControl moves with the Spatial it is attached to. Use GhostControls to implement custom game interactions by adding it to a visible Geometry.”

few things:

  • never use setPhysicsLocation on something which should be moved with forces, otherwise set it to kinematic
  • you can use the ghost control for collisions in a PhysicsCollisionListener
  • As jmassing says, just attach it to the spatial, and move the spatial around
  • getOverLappingObjects only works, right after the prePhysicsTick, so in the following PhysicsTick (as you’ve found out). But i think it should also work in simpleUpdate? as normen has said a few times, although can’t remember if it actually does
1 Like

The “overlapping objects ticking”-stuff that wezrule talks about is actually explained in another tutorial:



https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners

Thanks for the replies guys, I have another question now:


@wezrule said:
1. As jmassing says, just attach it to the spatial, and move the spatial around
2. getOverLappingObjects only works, right after the prePhysicsTick, so in the following PhysicsTick (as you've found out). But i think it should also work in simpleUpdate? as normen has said a few times, although can't remember if it actually does


1. When I have the ghost attached to the spatial, do I move the spatial in the main update loop or elsewhere?
2. Will getOverLappingObjects work in simpleUpdate if I move the ghost by moving the spatial?

I essentially want to be able to move an object in one direction, check for a collision, move it again if there isn't a collision, then check again for a collision.