Rotate towards up with forces


I’d like to gradually flip my entity upright. I’m hearing that I need to get the current orientation and the up orientation and multiply them, then scale it and apply an angular velocity.

Here’s where the fact that I never got my head around quaternions is a problem.

Does anyone have some example code? I started to modify this:
But it’s slow going.




Is this the kind of effect you are looking for where it tries to right itself when floating in the air?


Ha, I guess so yes. I have a spaceship that ends up flipping upside down on curves and impacts and want to auto-right it.

Simpler though - just want to drift rotation to up.


I think you answered something similar here:

If I had to rephrase my question:

Given a desired target heading (I was thinking up, but actually away from camera may be the way I go) and an entity moving in a given direction, as a result of collisions etc, how do I apply a force to smoothly rotate the object toward the target heading?



I’m afraid I don’t have time to do an example. Ultimately it comes down to working out the difference of the current rotation of the object is to the target rotation and applying a torque force by said difference and then apply a damping force as it gets closer to the target rotation.

It gets tricky as you have to tweak a whole bunch of numbers to get the right effect.

You can use rotation velocity instead of torque if you want finer control. You just have to deal with the edge case of what happens when you overshoot the target rotation.


Thank you for helping.

Yes, that’s what I figured. It’s the math for that that I don’t currently have worked out - Figure it’s some sort of quaternion deal (although I don’t care if the rotation looks clumsy).
Is it just a cross product or something trivial?


I was thinking of using a (few) dot products between two orientation vectors. Eg, assuming you want it upright, the world up and the entity’s local up vector.


Managed to grab 30 mins. His an example of what was done in the Harry Potter video above:

Press R to randomly set the box
Press Space to toggle the self righting floating behaviour


Edit: there’s a bug if the box is upside down to start with. It should be an easy fix with another dot product check.


Oh that’s great! I’ll dig in and try to get that working!