CameraNode, gravity and axis: Unpleasant up/down mouselook in fps

Hello!



The question is based on the following code:


import com.jme.input.NodeHandler;
import com.jme.math.Vector3f;
import com.jme.scene.CameraNode;
import com.jme.scene.shape.Box;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.geometry.PhysicsBox;
import com.jmex.physics.util.SimplePhysicsGame;

public class TestPlayerBody extends SimplePhysicsGame {

   @Override
   protected void simpleInitGame() {

      // Ground
      StaticPhysicsNode ground = getPhysicsSpace().createStaticNode();

      Box groundBlock = new Box("Sol", new Vector3f(), 5f, 0.5f, 5f);
      groundBlock.setLocalTranslation(new Vector3f(0, -1, 0));

      ground.attachChild(groundBlock);
      ground.generatePhysicsGeometry();

      // Player
      DynamicPhysicsNode playerNode = getPhysicsSpace().createDynamicNode();

      // DynamicPhysicsNode playerHead =
      // getPhysicsSpace().createDynamicNode();
      // DynamicPhysicsNode playerBody =
      // getPhysicsSpace().createDynamicNode();

      // playerNode.attachChild(playerHead);
      // playerNode.attachChild(playerBody);

      PhysicsBox playerVolume = playerNode.createBox("Player volume");
      playerVolume.setLocalScale(new Vector3f(0.5f, 1f, 0.5f));
      playerNode.setLocalTranslation(0, 2, 0);
      playerNode.setCenterOfMass(new Vector3f(0, -0.25f, 0));

      // Player mouse control
      input = new NodeHandler(playerNode, 5f, 1f);

      // Place the Camera so that it watches the scene n_n
      CameraNode camNode = new CameraNode("Camera Node", cam);
      camNode.setLocalTranslation(0, 1, -4);
      // playerNode.attachChild(camNode);

      // Root
      rootNode.attachChild(ground);
      rootNode.attachChild(playerNode);
      rootNode.attachChild(camNode);

      // Misc
      showPhysics = true;

   }

   public static void main(String[] args) {
      new TestPlayerBody().start();

   }

}



You may notice that playerHead and playerBody creation are commented, as well as playerNode.attachChild(camNode).

Running this code shows:
A physics box falling on a floor.
The mouse and WASD make the "player" move, fps style. The camNode has not been attached on purpose, I want to see how the PhysicsBox behaves.

->
Turn left right, WASD: Works nicely.

Look up/down: Unlike an fps game would do, your head bobs back and forth until it stabilizes.
So, smart me, I wanted to create to "parts" for my player:
- head
- body

both are attached to playerNode.
playerNode is controlled using WASD and left/right rotation. However I'd "lock" up/down mouselook.
The "head" would be the only one affected by that up/down mouselook.

But, a DynamicPhysicsNode cannot be attached another DynamicPhysicsNode. So I can only understand my genius idea wasn't that right, in the end.

How would you cope with this mouselook issue?
Thanks for your time!!!

You can attach two Dynamic Nodes with Joint and do really fun stuff with them :slight_smile:

But i am afraid i never used them, i don't know how to use them correctly.

But i goes something like this:



        DynamicPhysicsNode playerHead = getPhysicsSpace().createDynamicNode();
        PhysicsSphere headVolume = playerHead.createSphere("Head Volume");
        headVolume.setLocalScale(0.3f);
        playerHead.setLocalTranslation(0, 0.9f, 0);
        playerHead.setMass(0.1f);

        DynamicPhysicsNode playerBody = getPhysicsSpace().createDynamicNode();
        PhysicsBox playerVolume = playerBody.createBox("Player volume");
        playerVolume.setLocalScale(new Vector3f(0.5f, 1f, 0.5f));
        playerBody.setCenterOfMass(new Vector3f(0, -0.4f, 0));
        playerBody.setMass(10);
        Joint joint = getPhysicsSpace().createJoint();
        RotationalJointAxis rot = joint.createRotationalAxis();
        rot.setDirection(Vector3f.UNIT_X);
        rot.setPositionMinimum(FastMath.DEG_TO_RAD * -20);
        rot.setPositionMaximum(FastMath.DEG_TO_RAD * 20);
        joint.attach(playerBody, playerHead);



This attaches the Head to the Body and limits the Movement of the Head to the X-Axis within 40 degrees.

I couldn't get it to work really tho, so just take this as a suggestion.  :)

Hmm that joint idea is nice :slight_smile:

I've linked them with a TranslationalJointAxis to ensure the head is at a certain height of the body.



Hmmm (still investigating).

Ah, I've thought about something very, very simple.



two controllers:

  • head
  • body



    (nothing new up to here)


  • disable keyboard for head
  • head is not gravity sensitive
  • head is moved with the body
  • body gets the y and w components of the head quaternion localRotation
  • mouselook for the head, keyboard for the body



    :smiley: wohooo lots of idea!



    I'll post the results of these attempts when I got decent results to share.

Anything come of this?

There's a flaw with that model, I used a node controller like handler to move a physics node… bad. Collisions between player and world were awful.

The next attempt applies forces on the "body physics node", but I've not yet had time to make it look decent, or try to at least. Since the body block is "floating", there's need for friction, etc. I hope it's gonna be the good model this time :slight_smile: