Thread: Compute the angle to rotate for 2 vectors

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    Compute the angle to rotate for 2 vectors

    I understand i need to get the Dotproduct of the 2 vectors and then set the rotationz for the bottom teapot so it looks up at the other above it ,do you have to convert the DotProduct to radians as its in degrea's?

    vectors are

    D3DXVECTOR3 TeaPotPos(0.0f,-3.0f,-10.0f);
    D3DXVECTOR3 TeaPotPos2(0.0f,3.0f,-10.0f);

    pic included

    http://img709.imageshack.us/img709/1994/helpk.jpg

    thanks for looking

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Does directX not have a "rotate" function?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    yes it does but i need to find the exact value which is a float to rotate

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    What exactly are you looking for? If you're simply looking for converting the angle from degrees to radian, then you probably already know that 180 degrees is equal to pi radians.
    Last edited by Memloop; 12-16-2009 at 04:08 PM.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well there are a few ways I know of to do this.

    First, the hard way.
    1. Create a vector from the bottom teapot to the top teapot and normalize it.
    2. Cross the look vector of the bottom teapot with the vector you just created. This is the axis of rotation.
    3. Calculate the dot product of the vector from step 1 with the look vector of the bottom teapot. This is the cosine of the angle of rotation.
    4. Rotate the bottom teapot on the axis found in step 2 by the arccosine of the dot product found in step 3. This can be done with a simple D3DXMatrixRotationAxis() and passing in the params you just calculated.


    Now, the easy way:
    1. Use D3DXMatrixLookAtLH() to create a matrix that will look at the top teapot from the bottom teapot
    2. Extract the up, right, and look vectors from the resulting matrix
    3. Ortho-normalize the 3 vectors
    4. Set the bottom teapot's up, right, and look vectors to the normalized vectors.


    For a simple top-down 2D rotation:
    1. Create a vector from the bottom teapot to the top teapot and normalize it.
    2. The Z rotation is calculated via atan2f(y,x). x and y here are the x and y components of the vector created in step 1. atan2f(y,x) computes the arctangent of x or the arctangent of y/x.



    Note that this will cause the bottom teapot to 'snap' to the correct orientation. If you want a smooth transition you will need to lerp from it's current orientation to it's the orientation you just calculated.
    Last edited by VirtualAce; 12-16-2009 at 11:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Camera rotate to function
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 11-15-2008, 12:22 PM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM