?
Solved

Vector Math

Posted on 2003-02-22
4
Medium Priority
?
1,086 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 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