Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Vector Math

Posted on 2003-02-22
4
Medium Priority
?
1,089 Views
Last Modified: 2013-12-26
Lets say you have 2 vectors which are normals, A and B.

how would you go about findind the rotations, so that you can rotate Vector A to equal Vector B.

thx in advanced

 
0
Comment
Question by:GoldStone32767
  • 3
4 Comments
 

Expert Comment

by:pimaster
ID: 8014115
The two vectors establish a plane.  
First, find the angle Φ between the vectors A and B:
Φ = cosˉ¹      (Ax * Bx + Ay * By + Az * Bz) /
           ( sqrt(Ax²+Ay²+Az²) * sqrt(Bx²+By²+Bz²) )

Whew!

Next, find the vector normal to the plane:

Nx = Ay * Bz - Az * By
Ny = Ax * Bz - Az * Bx
Nz = Ax * By - Ay * Bx

The axis of rotation will be the normal vector N.

Rotate the point (Ax,Ay,Az) Φ radians about N.  

The DirectX SDK contains a very useful routine which produces a rotation matrix from an arbritrary vector
& angle of rotation.

There are also routines to calculate normals, dot products, etc.

I don't know if this is the easiest way to do this,  but it was the first method that came to mind.  You'll have to modify the normal calculations for a right handed system.  I haven't had time to test this, but the basic reasoning should be sound.
0
 

Expert Comment

by:pimaster
ID: 8014142
Ok, let's try it WITHOUT the useless Unicode characters:

The two vectors establish a plane.  
First, find the angle between the vectors A and B:
Theta = cos-1      (Ax * Bx + Ay * By + Az * Bz) /
              ( sqrt(Ax2+Ay2+Az2) * sqrt(Bx2+By2+Bz2) )

Whew!

Next, find the vector normal to the plane:

Nx = Ay * Bz - Az * By
Ny = Ax * Bz - Az * Bx
Nz = Ax * By - Ay * Bx

The axis of rotation will be the normal vector N.

Rotate the point (Ax,Ay,Az) Theta radians about N.

Ugh...
0
 

Author Comment

by:GoldStone32767
ID: 8029195
I understand how to calculate the cross product between the 2 vectors... but the problem im having is, how do you rotate around that vector?
0
 

Accepted Solution

by:
pimaster earned 800 total points
ID: 8031149
Rotating about an arbitrary vector can be very tricky if done the traditional 3D way. It involves aligning the vector with the Z axis, performing a Z axis rotation, then rotating everything back. You also have to check for the dreaded Divide by Zero error.

Luckily, there is a 4 dimensional entity called a quaternion which greatly simplifies this task. A quaternion adds another element to the 3 dimensional vector: a rotation angle. I've borrowed two routines from DirectX 7.0.  The first routine creates a quaternion from a normalized vector and angle of rotation. The second routine creates a rotation matrix from this quaternion.  

I'm told that quaternions are immune to divide by zero errors, and that NASA uses them regularly!

step 1:
create a quaternion from the normal vector and angle.

step 2:
create a rotation matrix from the quaternion.

step3:
multiply the vector by the matrix.

//-----------------------------------------------------------------------------
// File: D3DMath.cpp
//
// Desc: Shortcut macros and functions for using DX objects
//
// Copyright (c) 1997-1999 Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Name: D3DMath_QuaternionFromRotation()
// Desc: Converts a normalized axis and angle to a unit quaternion.
//-----------------------------------------------------------------------------
VOID D3DMath_QuaternionFromRotation( FLOAT& x, FLOAT& y, FLOAT& z, FLOAT& w, D3DVECTOR& v, FLOAT fTheta )
{
    x = sinf( fTheta/2.0f ) * v.x;
    y = sinf( fTheta/2.0f ) * v.y;
    z = sinf( fTheta/2.0f ) * v.z;
    w = cosf( fTheta/2.0f );
}


//-----------------------------------------------------------------------------
// Name: D3DMath_MatrixFromQuaternion()
// Desc: Converts a unit quaternion into a rotation matrix.
//-----------------------------------------------------------------------------
VOID D3DMath_MatrixFromQuaternion( D3DMATRIX& mat, FLOAT x, FLOAT y, FLOAT z, FLOAT w )
{
    FLOAT xx = x*x; FLOAT yy = y*y; FLOAT zz = z*z;
    FLOAT xy = x*y; FLOAT xz = x*z; FLOAT yz = y*z;
    FLOAT wx = w*x; FLOAT wy = w*y; FLOAT wz = w*z;
   
    mat._11 = 1 - 2 * ( yy + zz );
    mat._12 =     2 * ( xy - wz );
    mat._13 =     2 * ( xz + wy );

    mat._21 =     2 * ( xy + wz );
    mat._22 = 1 - 2 * ( xx + zz );
    mat._23 =     2 * ( yz - wx );

    mat._31 =     2 * ( xz - wy );
    mat._32 =     2 * ( yz + wx );
    mat._33 = 1 - 2 * ( xx + yy );

    mat._14 = mat._24 = mat._34 = 0.0f;
    mat._41 = mat._42 = mat._43 = 0.0f;
    mat._44 = 1.0f;
}


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question