Hi All,
I’m pretty new to babylon.js and learning all the features. I’m in the process of building a space scene with Planets, a ship, asteroid fields etc. All is going well apart from an issue with the camera now I’ve hooked it up to my gamepad.
The problem I am having is, once I have pressed button 5 or 6 and rotated the ship 90 degrees to the left or right using camera.rotation.z, the issue happens where camera.rotation.x and camera.rotation.y are now not relative to the camera rotation, they are still relative to the world.
So when I then use my left stick to rotate left for example, the ship rotates up instead of left and vice versa if I rotate right the ship rotates down.
I solved this on strafing Left, Right, Up, Down using addInPlace:
camera.cameraDirection.addInPlace(camera.getDirection(BABYLON.Vector3.Right()).scale(Math.abs(gamepad.rightStick.x / 100)));
However there doesn’t seem to be an equivalent to use once the camera has rotated around the Z axis. camera.rotation.x is always relative to the world and not the camera. What do i need to add to this to make X relative to the camera or at least when i push left to rotate left it goes left and not up?
I’m sure i’m missing something simple here, but i’m lost and going round in circles and nothing works.
Any help is much appreciated.
I don’t have a playground at the moment as I’ve just signed up, but ill create one if needed.
Thanks in advance.
gamepadManager.onGamepadConnectedObservable.add((gamepad, state) => {
gamepad.onButtonDownObservable.add((button, state) => {
//BUTTON PRESSED
if (button == 5) {
camera.rotation.z += -0.01;
}
if (button == 4) {
camera.rotation.z += 0.01;
}
})
scene.registerBeforeRender(function () {
if (gamepad instanceof BABYLON.DualShockPad) {
if (gamepad.rightStick.y > 0.1 || gamepad.rightStick.y < -0.1) {
setVal("control_thrusterXstate", (parseFloat(checkVal("control_thrusterXstate")) + gamepad.rightStick.y / (5000 / (parseFloat(checkVal("control_afterburner"))))));
}
if (gamepad.rightStick.x > 0.1) {
camera.cameraDirection.addInPlace(camera.getDirection(BABYLON.Vector3.Right()).scale(Math.abs(gamepad.rightStick.x / 100)));
}
if (gamepad.rightStick.x < -0.1) {
camera.cameraDirection.addInPlace(camera.getDirection(BABYLON.Vector3.Left()).scale(Math.abs(gamepad.rightStick.x / 100)));
}
****THIS IS WHERE I HAVE THE ISSUE
if (gamepad.leftStick.y > 0.2 || gamepad.leftStick.y < -0.2) {
camera.rotation.x += (gamepad.leftStick.y / 500) * -1;
}
if (gamepad.leftStick.x > 0.1 || gamepad.leftStick.x < -0.1) {
camera.rotation.y += (gamepad.leftStick.x / 500);
}
}
});