# Dual Virtual Joystick for rotate ( left ) and move ( right ) in a First-Person Perspective 3D Room

I built this first-person perspective 3D room with a dual VirtualJoystick (left to rotate and right to move) (https://playground.babylonjs.com/#R7KZRR#1). And it works fine in the player’s initial position, but as soon as you rotate the player, the movement is incorrect. Basically these lines make them rotate and move:

``````        // Virtual Joystick UP DOWN LEFT RIGHT *** HELP HERE ***
if(joysticks.rightPuck.isDown){
}

// Virtual Joystick movement rotation  *** HELP HERE ***
if(joysticks.leftPuck.isDown){
}
``````

If I take the vector J ( Joystick ) and the P ( Player ) to calculate the M ( Move ) is not a simple Addition of Vectors but is like to prolongate the coordinates axis J into the direction of P and then calculate the Addition of Vectors. ( See graph )

And I’m not sure how to calculate the correct formula and values to make this work on babylonjs.

Rotate and move also works well with keyboard keys. Arrows to move. The “Z” and “X” to rotate. I appreciate any help. Thank you!

After many formulas and calculations I got the answer for the move:

``````        // Virtual Joystick UP DOWN LEFT RIGHT
if(joysticks.rightPuck.isDown){
player.nextspeed.x = v*(camera.getForwardRay().direction.x*Math.cos(joystick_angle)-camera.getForwardRay().direction.z*Math.sin(joystick_angle));
player.nextspeed.z = v*(camera.getForwardRay().direction.z*Math.cos(joystick_angle)+camera.getForwardRay().direction.x*Math.sin(joystick_angle));
}
``````

Here could see working well the joystick: https://playground.babylonjs.com/#R7KZRR#2

1 Like

And for the rotation is quite simple:

``````        // Virtual Joystick movement rotation
if(joysticks.leftPuck.isDown){