3D Math (Vector & Matrices)

This is a discussion on 3D Math (Vector & Matrices) within the Game Programming forums, part of the General Programming Boards category; Hello everyone, I have question about 3D math. I have a vector representing the direction i want my matrix to ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    31

    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.
    Last edited by JohnLeeroy; 03-17-2011 at 02:14 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,406
    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. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    31
    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.
    Last edited by JohnLeeroy; 03-18-2011 at 06:30 PM.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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 subscribe to a feed

Similar Threads

  1. Game Programming general questions... and Math?
    By MsJordan in forum Game Programming
    Replies: 15
    Last Post: 11-06-2010, 08:46 PM
  2. BFS for 3D matrices
    By satty in forum C Programming
    Replies: 6
    Last Post: 08-23-2010, 09:20 AM
  3. 3d math questions
    By linuxdude in forum Game Programming
    Replies: 3
    Last Post: 01-04-2005, 10:07 PM
  4. Min Math level req for 3d graphics?
    By EvBladeRunnervE in forum Game Programming
    Replies: 12
    Last Post: 02-24-2003, 10:32 PM
  5. 3D Math translations
    By Crossbow in forum Game Programming
    Replies: 2
    Last Post: 07-18-2002, 10:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21