RigidBodyControl not working properly - possible conflict with a custom control?

Hello, wonderful people of jmonkeyengine.org.

I am still learning how to use all the features of jme3, and as a practice project, I’m creating a simple first-person obstacle course game. In this obstacle course, there will be platforms that move between two locations, and I have created a custom control, MovingPlatformControl, to dictate the motion of these platforms. My problem is that when I attach both an instance of my custom control and a RigidBodyControl to the platform geometry, RigidBodyControl does something weird. As the platform moves back and forth between (0,0,-20) and (0,0,-40), the RigidBodyControl only collides with the player when the platform is between (0,0,-20) and (0,0,-28ish), even though I’ve made sure that the control is, in fact, moving along with the platform. I am completely clueless as to why the RigidBodyControl is behaving this way, and hope that someone might be able to explain it and offer a solution.

Below are download links to both of the .java files for my project, in case I haven’t made my problem clear enough. Neither of these is a virus, I promise. Use WASD to move, Space to jump, and hold LShift to walk slowly. You will see a black platform in front of you when the game is first started, and it will slowly move away from the player; run over to it and jump on. The player will not move with the platform, so you will have to walk slowly along with the platform to stay on. Attempting to stay on the platform will reveal that at about (0,0,-28), the platform will cease to support the player, and the player will simply fall through it. Parts of the platform that still have z values between -20 and -28ish, however, have normal collision behavior. You’ll see what I mean.

Dropbox - Error
Dropbox - Error

EDIT: I forgot that I used some custom textures, so I’m gonna give you the whole project instead of just the .java files: Dropbox - Error

Thanks.

RigidBodyControl manage the position of your your object in physics space then apply this position to the geometry. If you interfere by moving this geometry, the rigid body and the geometry are not in sync.
You can display the debug shapes of rigid bodies and my guess is that you’ll see that your rigid body doesn’t move.
If your platform is as a RigidBody, you need to move it with physics (applyForce, applyImpulse, etc…).This will move the RigidBody in physic space and the position will be applied to the geometry.
I guess you can also use RigidBody.setPhysicsLocation() in your custom control to handle the sync of your geometry and your rigid body. But be aware that you have to set the world transforms of the geometry and not the localTransforms to have it work.
Though, I’m not completely sure the latter is the right way to do it, so I’ll let more experienced people in physics comment on this.
@Normen?

Using setPhysicsLocation isn’t the right way, no. By using that you “beam” your physics objects around and end up with wrong physics by beaming into walls etc. Look at the source of BetterCharacterControl on how you can move physics objects directly while keeping physics in sync. Another way is using kinemtic mode where your object can be moved directly and influences physics but doesn’t get influenced by the physics itself (like the train in GTA V). Using forces is the most complicated but also the most realistic way to do it.

I looked up kinematic mode and that is precisely what I need to use. However, I try to set the RigidBodyControl to kinematic mode and it throws me a NullPointerException. This is the code fragment:

RigidBodyControl c=new RigidBodyControl();
c.setMass(1f);
c.setKinematic(true);
c.setCollisionShape(CollisionShapeFactory.createMeshShape((Spatial)g));
g.addControl(c);

I looked at the output and the error did occur at the line containing c.setKinematic(true).

Any explanation?

I looked up kinematic mode and that is precisely what I need to use. However, I try to set the RigidBodyControl to kinematic mode and it throws me a NullPointerException. This is the code fragment:

RigidBodyControl c=new RigidBodyControl();
c.setMass(1f);
c.setKinematic(true);
c.setCollisionShape(CollisionShapeFactory.createMeshShape((Spatial)g));
g.addControl(c);

I looked at the output and the error did occur at line 3.

Any explanation?

Nevermind. I’ve solved the problem. The control needed to have a collision shape before I set it to kinematic, and it didn’t like the generic MeshCollisionShape for some reason. Thank you both for your help.

RigidBodyControl c=new RigidBodyControl(CollisionShapeFactory.createBoxShape((Spatial)g),1f);
c.setKinematic(true);
g.addControl(c);