C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-16-2009, 12:20 PM   #1
Registered User
 
Join Date: Nov 2002
Posts: 259
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
__________________
Anddos is offline   Reply With Quote
Old 12-16-2009, 12:35 PM   #2
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
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
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 12-16-2009, 01:10 PM   #3
Registered User
 
Join Date: Nov 2002
Posts: 259
yes it does but i need to find the exact value which is a float to rotate
__________________
Anddos is offline   Reply With Quote
Old 12-16-2009, 03:02 PM   #4
Robot
 
Join Date: Mar 2009
Posts: 372
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.
Memloop is offline   Reply With Quote
Old 12-16-2009, 11:34 PM   #5
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 8,322
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.
__________________
I remember when The Weather Channel talked about weather, MTV played music videos, and the History Channel talked about history instead of conspiracy theories.

Last edited by Bubba; 12-16-2009 at 11:44 PM.
Bubba is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Camera rotate to function Bubba Game Programming 0 11-15-2008 12:22 PM
Vectors naseerhaider C++ Programming 11 05-09-2008 08:21 AM
How properly get data out of vectors of templates? 6tr6tr C++ Programming 4 04-15-2008 10:35 AM
HUGE fps jump DavidP Game Programming 23 07-01-2004 10:36 AM
Points, vectors, matrices subnet_rx Game Programming 17 01-11-2002 02:29 PM


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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