Hello everybody!
I found a lot of good stuff on how to rotate a mesh with camera direction, but Im having a big problem.
I made a tank, that has a turret that can be rotated. And I want to rotate this turret to look wherever the camera looks at… now… here is the catch: the turret rotates slower.
im rotating the turret like this:
turret.rotation.y += speed
but if the camera rotation.y reaches 3.14 it goes back down to -3.14 or vice versa.
and my turret will see that the camera rotation is not bigger anymore, so it will go back with turret.rotation -= speed, which is very annoying when there is clearly a few inches left from the turret to turn fully over, but it just starts to rotate the other way to reach -3.14
I hope you guys can help me.
This is really hard to see what could be wrong without the code Could you create a small playground highlighting the issue ?
1 Like
As was mentioned, a playground example can be very helpful in helping us help you
That said, a problem I think you’re experiencing is what’s known as Gimbal Lock, and it comes about when the Euler angle rotation property is used. Try instead using the functionmesh.rotate(Axis.Up, amount)
and see if that fixes anything
HTH
1 Like
Sorry for not providing a playground example, it was 3 am and I was tired as hell, here is the playground: https://www.babylonjs-playground.com/#1RHSYU#19
(i edited someone else’s playground, then made another save for myself to make it easier for me, i hope it’s not a problem . _.)
i tried your solution, not really worked, btw here is the pg
https://www.babylonjs-playground.com/#1RHSYU#19
@JohnK is the king of maths and I feel bad to ping him that often but on the other hand I know I will learn smthg.
1 Like
Alright, i’ve made myself the logic behind this.
Basically, if it reaches 3.14 then resets to -3.14.
I added Math.PI to that, so it will go up to 6.28 and will reset to 0, and what I’m doing is:
if mesh.rotation.y = 5 and camera.rotation.y = 1 then check if 5 is closer to 6.28 or to 1
if it’s closer to 6.28, then it will overwrite the default direction, and it will turn the other way (aka go forward to 6.28 instead of going back to 1, one by one.)
and if the mesh.rotation.y = 1 and camera.rotation.y = 5 then check the same thing but the otherway.
Here is the playground for my fix: https://www.babylonjs-playground.com/#1RHSYU#22
(maybe there is a better solution hahaha)
1 Like
w00t that was quite a ride
1 Like