# Thread: 3D Math (Vector & Matrices)

1. ## 3D Math (Vector & Matrices)

Hello everyone,

I have question about 3D math. I have a vector representing the direction i want my matrix to face. How can I change the forward(z axis) vector of the matrix to the direction without distorting the X or Y axis.

I know one way is to use dot product.
cos(theta) = vec1 dot vec2

Code:
```float angle = arccos(currentMatrix.AxisZ dot directionVector);
currentMatrix.Rotate(angle);```

I was wondering if there was a less expensive way to do it. Arc cosine is an expensive call I hear. 2. O_o

What exactly are you doing with `currentMatrix'? What does the matrix represent?

I only ask because depending on what you are doing one of the columns or rows, depending on the API, will be the forward matrix verbatim. Updating them could literally be a simple copy operation.

Otherwise, you could calculate the rotation necessary and perform the a probably simpler multiplication.

*shrug*

Who knows? My point is, we kind of need more information in order to even guess what might be a better solution.

Anyway, considering how many times I call `sin', `cos', `asin', and `acos' in my own code I think you'd probably be fine, but if you think you are not throw a little code around and benchmark it.

Soma 3. 1. Create a vector from the source object to target object
2. Normalize the vector
3. Take the dot product of the vector you created in step 1 and 2 and the look vector of the source object - this is the amount of rotation.
4. Cross the look vector of the object with the normalized vector to the object you created in steps 1 and 2. This is the axis of rotation.
5. Rotate the source object on the axis of rotation calculated in step 4 by the amount of rotation calculated in step 3.

Alternatively:
Create a look at matrix and transform the source object by the look at matrix. The eye point is the source object, the look at is the target object, and the up vector is usually the world up vector. 4. Thanks Virtual Ace.

Those steps actually make sense. So dot-product actually returns the degree difference between two vectors? Anyways, would I need to orthonomalize my matrix after I rotate on this arbitrary axis.

As far as I'm aware, orthonomalization is used to transform distorted axis' to 90 degree axis' but is it needed in this case. 5. Before calculating your view matrix be sure you do ortho-normalize your vectors.

So dot-product actually returns the degree difference between two vectors?
It returns the arccosine of the degree of difference. You will need to use acosf() to rotate correctly. Remember the geometric meaning of the dot product is it is the arccosine of the angle between two vectors as well as the rules about products less than zero, zero, and greater than zero. 6. Actually, dot product is defined as
a*b = |a||b|cos(theta)
If your vectors are normalized, then their length will be 1, hence the dot product will be the cosine of the angle between them. 7. Yes the dot product is the cosine of the angle between two vectors. I got ahead of myself with the arc-cosine. You need to take the arccos of the dot product to get the angle between two vectors. Popular pages Recent additions 