Rotate mesh while animating its position PT 2

Hi everyone,

My question is about the solution of this post Rotate mesh while animating its position

What I want to achieve is very similar to the OP of that forum post, but the solution there doesn’t seem to be a fully correct solution. I used their PG to showcase, made it more visible and drew a path in the form of a square to clearly see what’s wrong: https://playground.babylonjs.com/#2I8Y3S#15

In the third turn of the square, it does a way wider turn than it needs to. If you add more points you can see this happen more often too. Can anyone help me find a solution for this? Thank you!

1 Like

I added Vector3.PitchYawRollToMoveBetweenPoints. You should be able to use the return value of the function as the rotation. See the example PG.

Thanks so much for reaching back! I’m still new to this and stuff like Vector3.PitchYawRollToMoveBetweenPoints really fascinate me. That works well for that example, but what about the example I’ve linked here → PG:https://playground.babylonjs.com/#R1F8YU#749

  1. It still does that weird turn where it would rather go 270 degrees for example rather than the short turn
  2. There isnt a “front” face like in your PG.

Appreciate anything. Thanks so much!

If you want it to take the shorter turn, you could check to see if abs(angle - 2 * pi) < angle, in which case you could subtract 2 * pi from the angle. Written out as JS:

function correctAngle(angle) {
	const angle2 = angle - 2 * Math.PI
	return Math.abs(angle2) < angle ? angle2 : angle;
}

rotation.x = correctAngle(rotation.x);
// ...

In case you are wondering about the 2 * pi and how subtracting it works, the angle is in radians and any angle +/- 2 * pi is the same direction (going around the circle again)

1 Like

Im sorry Im trying to understand this but it’s a little confusing relating this to the code you have in your PG. Would angle be the value you get from orientation(p), how did you get rotation.x?

Try https://playground.babylonjs.com/#R1F8YU#750

Interesting. Animation-wise I don’t see a difference between mine and yours, but I can see how you passed the values which is pretty neat. Would you have any advice on how to fix those two points I mentioned earlier? Leaving correctRotation empty seems to not change anything on the animation either.