What's an alternative to Quaternion.LookRotation() in MonoGame?

I have an object moving along a defined path. I’d like to get a rotation of the object in the direction of the path at this point, with local up point along the paths normal.
I’m trying to rewrite this function from a unity project, however I don’t know how to replace the Quaternion.LookRotation()

var data = CalculatePercentOnPathData(t);
Vector3 direction = Vector3.Lerp(localTangents[data.previousIndex], localTangents[data.nextIndex], data.percentBetweenIndices);
Vector3 normal = Vector3.Lerp(localNormals[data.previousIndex], localNormals[data.nextIndex], data.percentBetweenIndices);
return Quaternion.LookRotation(direction, normal);

The returned value can be a rotation Matrix, not necessarily a Quaternion.
Any help would be appreciated.

I’ve managed to solve the problem with Matrix.CreateWorld() with added position of the object. Case closed :slight_smile:

Not sure if there is a better way to do this, but what I would do is, getting the rotation components.

So I consider my own up vector as a plane (where my Y is the normal of that plane) and projecting the paths forward onto that, the angle (acos(dot)) between those two (my forward and path forward) gives me the y-component for the rotation (I consider Y being up/down)

doing the same for my x and y planes I get 3 components to rotate about. But that is just me.

If you don’t want the 3 components (yaw, roll, pitch) but just a rotation around an axis, you can just get the cross product between your forward and path forward (both unit vectors), giving you the axis to rotate around and acos(dot) to get the angle (be sure to clamp the dot result, acos will do NaN if dot returns outside of -1|1 and it will)

in case I mixed something up, feel free to correct me, I just wrote this from memory :slight_smile:

PS: Be aware to do 0 checks where needed to not run into NaN

1 Like