Hi, I'm working on this little 3D demo game, and I have come across a problem.
I have implemented the rotation of the camera correctly, but now I want to make it so that when the virtual joystick is moved left or right, the camera would translate appropriately (think A and D buttons in FPS games on PC).
The way I did it was by rotating the camera's directional vector 90 degrees and then translating the camera's position by that directional vector multiplied by joystick's value and movement speed.
It works as expected when the player is facing forward, but if it's facing up or down, the movement becomes slowed down.
Here's the bit of code I'm talking about
// this is the part that doesn't work correctly
cameraDirection.rotate(0,90,0);
cameraPosition.x += cameraDirection.x * game.joystickSystem.getX1() * CAMERA_SPEED*deltaTime;
cameraPosition.z += cameraDirection.z * game.joystickSystem.getX1() * CAMERA_SPEED*deltaTime;
// rotate the camera
cameraDirection.set(0,0,-1);
cameraDirection.rotate(cameraRotation.x, cameraRotation.y, 0);
cameraRotation.x += game.touchDelta.y;
cameraRotation.y += game.touchDelta.x;
// keep camera up/down rotation between -85 and 85 degrees
if(cameraRotation.x > 85)
cameraRotation.x = 85;
if(cameraRotation.x < -85)
cameraRotation.x = -85;
// move camera in its direction with joystick
cameraPosition.x += cameraDirection.x * -game.joystickSystem.getY1()*CAMERA_SPEED*deltaTime;
cameraPosition.y += cameraDirection.y * -game.joystickSystem.getY1()*CAMERA_SPEED*deltaTime;
cameraPosition.z += cameraDirection.z * -game.joystickSystem.getY1()*CAMERA_SPEED*deltaTime;
camera.setCenter(
cameraPosition.x,
cameraPosition.y,
cameraPosition.z
);
// make camera look at the direction it's facing
camera.lookAt(
cameraPosition.x + cameraDirection.x,
cameraPosition.y + cameraDirection.y,
cameraPosition.z + cameraDirection.z
);
Any ideas on how to fix this?
Thanks in advance