Perform a rotation in 3D world

75 Views Asked by At

I got a character at some point $A$ facing to point $O$ that is equal to $(0,0,0)$, then I move it to point $B$ and I want to rotate him to face point $O$. Since this is 3D world I think that I need to do two rotations. I also got an idea of how to do this, but I need your help to actually write it down.

So this is my plan: enter image description here

Create $BC$ that is vertical to $AO$, then find $C$ and calculate $CO$, $BO$ and $BC$ over the $xy$ plane, and then find the angle COB. This is for rotation one, and also I need to do this with the $x$ and $z$ axes as triangles only live in a two dimensional world.

I am not sure that my plan is good, if it's not please tell me what is the proper way of doing this, and please help me make the calculations.

I need this for my game where a person walks on a star and I need to rotate the Matrix4 when I translate him based on the player movement. I want to make sure his legs are pointed toward planet center.

1

There are 1 best solutions below

0
On

I assume you are representing your character's orientation using an orthonomal frame $$F = \left[\begin{array}{c|c|c}\\ v & u & l\\\ \end{array}\right]$$ where $v$ is the character's view ("forward") direction, $u$ is up, and $l = v\times u$ is the character's "left" direction.

You want a new orientation $F'$ where $v' = \frac{O-B}{\|O-B\|}$. You also need to compute the new up direction, which is just $u' = \frac{B-C}{\|B-C\|}$ where $C$ is the star's center. Finally you have $l' = v' \times u'$.

You can now simply replace the orientation of the character with $F'$; or if you need the transformation as a rotation relative to the original orientation, the rotation is just $F'F^T$.