[Solved] 3D Rotation Slerp towards Target

Solved.

I have a turret that slowly rotates towards a target. The game is 2.5D and is oriented along XY axis with Z as “up”.

This was the original code which worked great, but was for XZ axis with Y up.

I’ve changed the up vector in the CreateWorld to every Vector3 direction and it still doesn’t orient correctly. The turret lays in random directions. The model is oriented correctly as it looks great before applying the new Slerp rotation. The closest I can get it to work in a desired direction is changing the ‘up’ argument to my targetRotation and my ‘forward’ argument to Vector3.Forward.

Vector3 targetRotation = Vector3.Normalize(target.position - myPosition);

myRotation = Quaternion.Slerp(myRotation, Quaternion.CreateFromRotationMatrix(Matrix.CreateWorld(myPosition, targetRotation, Vector3.Up)), delta * 3f);

Unfortunately I currently don’t have a fantastic understanding of matrices and quaternion. Thanks

This?

Or this?

oooh:

http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/

Have you tried Vector3(0,0,-1) for the up vector?

Thanks for the links. I wish I was using Unity, they have that awesome Quaternion.LookRotation which would make things easier. But this is a school project.

On a side note I ended up solving my issue. I realized I was being very stupid. I had my actual models rotated 90 degrees so they’d play well on XY axis without having to touch them, instead of rotating in game. Doh. Sorry for the bother

1 Like