Forward walk direction

Hello, it’s me again :smiley:
I’m trying set the walkDirection to the forward and negate that to backward…
But, how I can get normal data of Vector3f from camDir?
If the camera directed to the horizont character walk normal, but if it directed vertical, character staying…
And it’s with the camUp, but vertical-walking and horizont-staying…
With left and right walking everything alright)
horizontal
Walking normal.
Vertical
Staying, but doing it normal, yes I pressed the W key)
My code
I trying calculate the forward and backward vectors from camLeft data, but it’s stayed in plan)

Well… I got it, if anybody has this problem, just use walkDirection.set(camLeft.z*(-1),0,camLeft.x); for forward walking and negate this for backward.
I don’t know, may be it’s not perfect, but it’s working!)
You can look around and your character will walking everywhen)

And my calculation, if anybody interesting)
calculation of the walking direction

Here is a useful doc so that you do not have to reinvent the wheel:
jme3:beginner:hellochasecam

So it’s a new cam in the character’s node for getting normal direction?

public AvatarBodyMoveControl(PlayerInputActionListener pial, PhysicsCharacter physicBody, Camera cam)
    {
        this.pial = pial;
        this.physicBody = physicBody;
        this.cam = cam;
    }

I would not say it is new. Just a different way to use the Camera object. The Camera object has the following methods, (ones that you are using):
cam.getDirection()
cam.getLeft()

But a different way that is simpler would be:

 Vector3f dir = new Vector3f();
  Vector3f cd = cam.getDirection().clone();
  Vector3f cdl = cam.getLeft().clone();
  cd.y = 0;
  cdl.y = 0;
  if (forward) {
       dir.addLocal(cd);
       forward = false;
  }
  if (backwards) {
      dir.addLocal(cd.negate());
      backwards = false;
  }
  if (right) {
       dir.addLocal(cdl.negate());
       right = false;
  }
  if (left) {
       dir.addLocal(cdl);
       left = false;
  }
  cc.setWalkDirection(dir.multLocal(moveSpeed).normalize());

‘cc’ is the Better Character Control and move speed is a float, I have mine set to 30f, but that is just for my scale.

Yes, I got it, but if I set the cd and look at the character from top, so following screen, it’s not walking…
But if calculate analog forward direction (from camLeft) it’s walking normal.
look at the character from the top

That is odd, what cam are you using? I took that piece of code directly out of a program that works fine.
Can you post the current code you have in that section. You will need 4 if statements that are NOT else-if chains.

Chasecam
With getDirection if look from top or bottom, than more top the more speed of walking… On the top absolutely stopped.

public void simpleUpdate(float tpf) {
        Vector3f camDir = cam.getDirection().mult(2.0f).clone();
        Vector3f camLeft = cam.getLeft().mult(2.0f);
        Vector3f forwardDir = new Vector3f(0, 0, 0);
        camDir.y = 0;
        camLeft.y = 0;
        forwardDir.set(camLeft.z*(-1),0,camLeft.x);
        walkDirection.set(0, 0, 0);
        if (forward) {
            walkDirection.set(forwardDir);
            viewDirection.set(camDir);
        } else if (backward) {
            walkDirection.set(forwardDir.negate());
            viewDirection.set(camDir.negate());
        } else if (left) {
            walkDirection.addLocal(camLeft);
            viewDirection.set(camLeft);
        } else if (right) {
            walkDirection.addLocal(camLeft.negate());
            viewDirection.set(camLeft.negate());
        }
        rangerControl.setViewDirection(viewDirection);
        rangerControl.setWalkDirection(walkDirection);
    }

But, if set the getDirection vector it’s not wolking if look from the top, and if set a getUp it’s not walking if look from the horizontal/down… So screen

You do not want these in an else-if chain.

Try this:


public void simpleUpdate(float tpf) {
        Vector3f camDir = cam.getDirection().clone();
        Vector3f camLeft = cam.getLeft().clone();
        Vector3f dir = new Vector3f();
        camDir.y = 0;
        camLeft.y = 0;
        if (forward) {
            dir.addLocal(camDir);
        }
        if (backward) {
            dir.addLocal(camDir.negate());
        }
        if (left) {
            dir.addLocal(camLeft);
        }
        if (right) {
            dir.addLocal(camLeft.negate());
        }
        dir = dir.multLocal(2.0f);
        rangerControl.setViewDirection(dir);
        rangerControl.setWalkDirection(dir);
    }

Also: where do you set the direction Bools back to false?

private ActionListener actionListener = new ActionListener() {
        @SuppressWarnings("empty-statement")
        public void onAction(String action, boolean pressed, float tpf) {
            rangerControl.setWalkDirection(Vector3f.ZERO);
            if (action.equals("forward")) {
                if (!pressed) {
                    forward=false;
                } else {
                    forward=true;
                }
            } else if (action.equals("backward")) {
                if (!pressed) {
                    backward=false;
                } else {
                    backward=true;
                }
            } else if (action.equals("left")) {
                if (!pressed) {
                    left=false;
                } else {
                    left=true;
                }
            } else if (action.equals("right")) {
                if (!pressed) {
                    right=false;
                } else {
                    right=true;
                }
            } else if (action.equals("jump")) {
                if (pressed) {
                    rangerControl.jump();
                }
            }
        } 
    };
@Override
    public void simpleUpdate(float tpf) {
        Vector3f camDir = cam.getDirection().mult(2.0f).clone();
        Vector3f camLeft = cam.getLeft().mult(2.0f);
        Vector3f forwardDir = new Vector3f(0, 0, 0);
        camDir.y = 0;
        camLeft.y = 0;
        forwardDir.set(camLeft.z*(-1),0,camLeft.x);
        walkDirection.set(0, 0, 0);
        if (forward) {
            walkDirection.set(camDir);
            viewDirection.set(camDir);
        }
        if (backward) {
            walkDirection.set(forwardDir.negate());
            viewDirection.set(camDir.negate());
        }
        if (left) {
            walkDirection.addLocal(camLeft);
            viewDirection.set(camLeft);
        }
        if (right) {
            walkDirection.addLocal(camLeft.negate());
            viewDirection.set(camLeft.negate());
        }
        rangerControl.setViewDirection(viewDirection);
        rangerControl.setWalkDirection(walkDirection);
    }

Nothing, but now it’s going diagonally with pressed 2 key and more faster.
I think it’s problem vector… And forward with if-else if must be working, becouse stated at the head.