0
votes

This page https://goocreate.com/learn/everything-you-always-wanted-to-know-about-rotation/ describes a function 'LookAt' as being able to rotate a 3D object to face another object. The internal operation of the LookAt function is described (but not listed in code) as:

The way the function works is that it takes the entity and target position and subtracts them to get a back vector. Then it calculates the cross product between the up vector and the back vector to get the right vector. Then it calculates the cross product between the right vector and the back vector to get an up vector that is orthogonal to both the direction and the right vector and finally stores all three vectors normalized inside our rotation Matrix3x3 in their respective rows.

I am trying to replicate this without a library of 3D functions to help me. I have 3d vector subtraction, matrix multiplication, dot and cross product functions. (I'm coding in Lua.)

What I cannot figure out is how to make one object face the same direction (and, hopefully, orientation) as another.

I have read that this is just a matter of creating a rotation matrix based on angles, but everything I find which talks about angles in 3D finishes by return a single angle - I would think that there would be 3 angles - one for each plane.

How do I construct this rotation matrix? I do not know which direction my object is facing, which is why the problem of calculating the current x,y,z angle of the object is a problem. I think what I need is a function to determine the current 3-dimensional angle of an object and a way to convert that into a rotation matrix for another object.

1

1 Answers

-1
votes

First of all:

You cannot turn an object into a specific orientation if the object does not have a orientation. So you either have to know the vector that defines the objects orientation or you have to define one yourself.

The internal operation of the LookAt function is described (but not listed in code) as:

This is not true. They link a FAQ containing questions that might have come into your mind while reading the article. (Read more carefully!)

The provide you with the following information:

What is the algorithm behind lookAt inside Matrix3x3:
The abbreviated algorithm is:

z.set(back_direction).normalize();
x.set(up).cross(z).normalize();
y.set(z).cross(x);
m[0] = x[0];
m[1] = x[1];
m[2] = x[2];
m[3] = y[0];
m[4] = y[1];
m[5] = y[2];
m[6] = z[0];
m[7] = z[1];
m[8] = z[2];

About your problem with the number of angles:

You only need 3 angles if you rotate around 3 axis. x,y,z for example. But you can rotate around any arbitrary axis. Then of course you only need one angle.

Get a text book on linear algebra or browse the web for tutorials and examples. Make sure you understand what you are doing. It will help you solve future problems without example code. It's simple maths :)