Hi Team!
Sorry for asking this as I have already looked at a few questions here regarding rotation and adjusting coordinates after rotation. I still couldn’t figure it out.
I have an example on the playground that demonstrates the issue that I am having (it uses wasd
to move and rotate): https://playground.babylonjs.com/#BTF9E4#1
After rotating the mesh, I would like to reset the mesh’s new forward and backward direction. The mesh in the playground rotates, but continues to move forward and backward in the same direction. How can I update the new forward and backward direction after rotating the cube?
My example on my local machine is slightly different (I couldn’t get it to work on the playground). I set bool flags when any of wasd
is pressed or released:
setInput() {
this.scene.onKeyboardObservable.add((kbEvent) => {
console.log(kbEvent);
if (kbEvent.type === KeyboardEventTypes.KEYDOWN) {
switch (kbEvent.event.key) {
case 'a':
this.moved.left = true;
break;
case 'd':
this.moved.right = true;
break;
case 'w':
this.moved.up = true;
break;
case 's':
this.moved.down = true;
break;
default:
break;
}
}
if (kbEvent.type === KeyboardEventTypes.KEYUP) {
switch (kbEvent.event.key) {
case 'a':
this.moved.left = false;
break;
case 'd':
this.moved.right = false;
break;
case 'w':
this.moved.up = false;
break;
case 's':
this.moved.down = false;
break;
default:
break;
}
}
});
}
And then movement/rotation is performed using those bool flags in the moveBox()
method (called every frame by runRenderLoop()
:
moveBox() {
if (this.box !== null) {
if (this.moved.left) {
this.box.rotate(Axis.Y, 0.05, Space.LOCAL);
}
if (this.moved.right) {
this.box.rotate(Axis.Y, -0.05, Space.LOCAL);
}
if (this.moved.up) {
this.box.position.z -= 0.05;
}
if (this.moved.down) {
this.box.position.z += 0.05;
}
}
}
One other difference is that in the playground example I am using box.rotation.x
and box.rotation.y
… But in my local example I am using this.box.rotate(Axis.Y, -0.05, Space.LOCAL);
I do not know what the difference between the two are, but in both cases I seem to have the same problem.
Thank you for reading!