Implementing physics character node with a orbit camera

Hello guys, it's me, again.

I have some questions about, Orbit camera and PhysicsCharacterNode. I already have my orbir camera, which works preatty well.


import java.util.concurrent.Callable;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.image.Texture.WrapMode;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.input.MouseInputListener;
import com.jme.light.PointLight;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.jme.util.Timer;
import com.jmex.jbullet.PhysicsSpace;
import com.jmex.jbullet.collision.shapes.CollisionShape;
import com.jmex.jbullet.nodes.PhysicsNode;

public class SimpleGameState extends BasicGameState {

    protected Camera cam;
    float camX = 0;
    float camY = 0;

    protected float tpf;
    protected Timer timer;

    private Node pivotNode;

    final PhysicsSpace pSpace = PhysicsSpace.getPhysicsSpace(PhysicsSpace.BroadphaseTypes.AXIS_SWEEP_3);
    protected LightState lightState;

    public SimpleGameState(Camera camera) {
        super("Test"); = camera;

    public void init() {
        PointLight light = new PointLight();
        light.setDiffuse(new ColorRGBA(0.75f, 0.75f, 0.75f, 0.75f));
        light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
        light.setLocation(new Vector3f(100, 100, 100));

        lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();

        timer = Timer.getTimer();

        GameTaskQueueManager.getManager().update(new Callable<Object>() {
        public Object call() throws Exception {
            return null;

        KeyBindingManager.getKeyBindingManager().set("exit", KeyInput.KEY_ESCAPE);

    public void update(float tpf) {

        tpf = timer.getTimePerFrame();

   if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit", false)) {

    public void setupScene() {
        Box floorNode = new Box("Floor", Vector3f.ZERO, new Vector3f(20, .2f, 40));

        Texture text = TextureManager.loadTexture(Main.class.getClassLoader()

        text.setScale(new Vector3f(9, 5, 5));

        PhysicsNode floor = new PhysicsNode(floorNode, CollisionShape.ShapeTypes.MESH, 0);

        floor.setModelBound(new BoundingBox());

        TextureState tsCarpett = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();


        Box roomNode = new Box("Room Wall's and Roof", Vector3f.ZERO, new Vector3f(20, 30, 40));

        Texture tex = TextureManager.loadTexture(Main.class.getClassLoader()

        tex.setScale(new Vector3f(9, 9, 9));

        PhysicsNode room = new PhysicsNode(roomNode, CollisionShape.ShapeTypes.MESH, 0);

        room.setModelBound(new BoundingBox());

        TextureState tsCarpet = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();


        MouseInput.get().addListener(new MouseInputListener() {

            public void onWheel(int wheelDelta, int x, int y) {


            public void onMove(int xDelta, int yDelta, int newX, int newY) {
                float speed = 35f;
      camX -= xDelta * timer.getTimePerFrame() * speed;
      camY -= yDelta * timer.getTimePerFrame() * speed;

      pivotNode.getLocalRotation().fromAngles(camY*FastMath.DEG_TO_RAD, camX*FastMath.DEG_TO_RAD, 0);

            public void onButton(int arg0, boolean arg1, int arg2, int arg3) {


   pivotNode = new Node("pivot");
        pivotNode.setLocalTranslation(10, 1.8f, 20);

   CameraNode camNode = new CameraNode("camNode", cam);
   camNode.setLocalTranslation(0, 0, 0);

But, I don't know how implement PhysicsCharacterNode on that Node. I already looked into TestPhysicsCharacterNode, but I'm like, kicking the air, cause I really don't know how. Can someone show me, using my code or something better? I would really appreciate it.

Regards, Gustavo Borba.
*Hope I don't need to bump this topic (:*

Help, please :confused: maybe confusing because it’s based of the vehicle test I believe, but all you really need to do is attach the pivet node to the character physics node

        character=new PhysicsCharacterNode(pivetNode, CollisionShape.ShapeTypes.SPHERE, .1f);

Then all you need after that is controls to update the position of the character node with the keyboard, similar to how you did it with the mouse.  Just look at the for reference.

Yeah sure, already tried that. But it gives me this:

        at com.jmex.jbullet.collision.shapes.SphereCollisionShape.createCollisionSphere(
        at com.jmex.jbullet.collision.shapes.SphereCollisionShape.createCollisionSphere(
        at com.jmex.jbullet.collision.shapes.SphereCollisionShape.<init>(
        at com.jmex.jbullet.nodes.PhysicsGhostNode.buildCollisionShape(
        at com.jmex.jbullet.nodes.PhysicsGhostNode.<init>(
        at com.jmex.jbullet.nodes.PhysicsCharacterNode.<init>(
        at com.jme.util.GameTask.invoke(
        at com.jme.util.GameTaskQueue.execute(

Cause, it's like, it thinks it is a sphere, but it's a node. Then it messes with the camera. :/ Help?

Try setting the model bounds of the pivet node with a BoundingSphere before creating the character node.  And call updateModelBound(), etc.

Stills the same problem, here's my code:

pivotNode = new Node("pivot");
        pivotNode.setLocalTranslation(10, 1.8f, 20);

        pivotNode.setModelBound(new BoundingSphere());

        character = new PhysicsCharacterNode(pivotNode, CollisionShape.ShapeTypes.SPHERE, .1f);

   CameraNode camNode = new CameraNode("camNode", cam);
   camNode.setLocalTranslation(0, 0, 0);

Try also:

pivotNode.updateGeometricState(0, true);

after the updateModelBound();

You know how to debug, right?

kinda, can you tell how?

Oh, btw tried that you said, nothing changed :confused:

Try to create the CollisionSphere before instead of using CollisionShape.ShapeTypes.SPHERE

float radius=1;
SphereCollisionShape collShape=new SphereCollisionShape(radius);
character = new PhysicsCharacterNode(pivotNode, collShape, .1f);

I think there are not method PhysicsCharacterNode with that parameters, normen. Compilersays there isn't :confused:

GustavoBorba said:

I think there are not method PhysicsCharacterNode with that parameters, normen. Compilersays there isn't :/

Yes there is, have a look at the javadoc.

So, must be a compiling problem from Netbeans. Take a look at this screenshot:


Even if you dont use the svn version, this is definitely in the latest download version as well:

Why do you take the time making a screenshot and uploading it to imageshack to prove me wrong about software that I wrote instead of using that time to check if you use the latest version :?

Over and out until I see my video of your nuke :stuck_out_tongue:

Now it worked, sorry for all. (:

One more question, I don't know if I should make a new topic for that or something like that, so I am posting here.

I want to know, how to make the player walk in the direction his looking at. I tried to print camX, but it's not 360 degrees, it's infinite. So if someone knows how to do it, please tell me.


Gustavo Borba.

GustavoBorba wrote:
I want to know, how to make the player walk in the direction his looking at.
Have you tried asking him nicely?


If you want to get the direction you are looking at (with the camera) try:

Vector3f lookingDirection = camera.getDirection()

Otherwise if you want the direction the character Spatial is rotated towards, use the worldrotation of the Spatial:

// Assuming model starts off looking up the z axis
Vector3f lookingDirection = new Vector3f(0f,0f,1f);
character.getWorldRotation().mult(direction,direction );

Okay, thank you for that ansqwer, but getting that Vector, how am I supposed to move the player to there?

GustavoBorba wrote:
how am I supposed to move the player to there?
That vector is a direction of movement, not a location - you cannot move the player there.

When using a physics character you give it a movement direction and let the physics take care of the rest.

Suggested reading:

Okay, so you mean like this?:

Vector3f lookingDirection = new Vector3f(0f,0f,1f);

walkDirection.addLocal(character.getWorldRotation().mult(direction, direction));

alright then, more like this:

Vector3f lookingDirection = new Vector3f(0f,0f,1f);
character.getWorldRotation().mult(lookingDirection , lookingDirection )
walkDirection.addLocal(lookingDirection );

The typo in my previous was obviously there to test your powers of observation!  ;)