View Full Version : Mathquestion to rotations and vectors

chriss123

08-28-2001, 03:30 PM

I need to get the rotation from a vector (normalized)

but why this doesnt work:

myrotation= DOT(FromN,ToN)

readymade = acos(myrotation)

glrotatef readymade,1.0,1.0,1.0

FromN is my old viewdir (aka. my view normal)

ToN is my new one I have calculated like this: Substract(MyPosition,ObjectPosition)

Anyone knows where my mistake is?

mikael_aronsson

08-28-2001, 10:26 PM

Hi !

glRotate( ang,1,1,1) will rotate ang degrees around the axis defined by 1,1,1 so that will not work.

You get the angle from the dot product and that's fine, but you need to now the axis to rotate around to make that work.

I don't have it in my head at the moment, but there are lots of documentation available on how to transform from one vector to another.

You do it by creating a matrix that transform from the first to the second vector for example.

Mikael

Korval

08-28-2001, 10:42 PM

OK, if you have a direction 'ToN' and the direction 'FromN', and you want to find the angle/axis rotation that transforms 'FromN' into 'ToN', do the following:

axis = Cross(FromN, ToN)

angle = acos(Dot(FromN, ToN))

Then, just call glRotatef(angle, axis).

BTW, I don't have my linear algebra book with me, but I seem to recall something about using the arcsin directly from the cross product to get the angle, but I'm not certain.

chriss123

08-29-2001, 02:19 AM

Well its still not working as I want it.

FromN = DirectionVector

toN = VectorSubstract(Vector(1, 1, 1), Vector(Camera.PosX, Camera.PosY, Camera.PosZ))

Axis = VectorMultiply(FromN, toN)

Angle = ArcCos(VectorDotProduct(FromN, toN))

I think my misttake is in the VectorSubstract

the vector(1,1,1) is the point I want to "look at" but its not correctly working, sometimes it even screw my rotation up.

DaViper

08-29-2001, 07:37 AM

Originally posted by Korval:

axis = Cross(FromN, ToN)

angle = acos(Dot(FromN, ToN))

hmmm my math book telles me the angle between two Vectors a and b is

angle=acos(Dot(a,b)/(|a| |b|))

May be that helps.

Chris

chriss123

08-29-2001, 07:54 AM

I thought that can be left out when the vector is normalized... or?

DaViper

08-29-2001, 08:09 AM

yup, silly me dividing by 1*1 doesn't change much. http://www.opengl.org/discussion_boards/ubb/wink.gif

chriss123

08-29-2001, 08:17 AM

Well I tried around, nothing helps, maybe its the best I paste my current code here, its VB

Dim toN As tagVECTOR, FromN As tagVECTOR

Dim Axis As tagVECTOR, Angle As GLfloat

FromN = Camera.m_Quat.GetDirectionVector

FromN = VectorNormalize(FromN)

toN = VectorSubstract(Vector(Camera.PosX, Camera.PosY, Camera.PosZ), Vector(1, 1, 1))

toN = VectorNormalize(toN)

Axis = VectorMultiply(FromN, toN)

Angle = ArcCos(VectorDotProduct(FromN, toN))

Debug.Print Angle, Axis.x, Axis.Y, Axis.Z

Debug.Print VectorLength(toN) & " " & "ToN" & " " & VectorLength(FromN) & " " & "FromN"

' Camera.m_Quat.ResetQuaternion

Dim aquat As New CQuaternion

aquat.SetAngleXYZ Angle, Axis.x, Axis.Y, Axis.Z

Camera.m_Quat.PostMult aquat

the ' = //

rest should be clear http://www.opengl.org/discussion_boards/ubb/smile.gif

the main problem is: sometimes this screws my rotation to the object up, but if im near to it, Iam smoothly turning to it.

s.th. must be wrong with the way i calculate my normal to the vector I want to look at, or s.th. may be wrong with the cross product...

Zadkiel

08-29-2001, 10:35 AM

I think acos returns radians but OpenGL uses angles. That could be the problem.

Ritchie

08-29-2001, 11:42 AM

Zadkiel is right. I had similar troubles when trying to convert points into different coordinate systems. Examination of the matrices revealed strange behavior.

A function that converted the Degrees to Radians solved this problem.

Ritchie

chriss123

08-29-2001, 11:55 AM

Hm k but sure that its not twisted, rad to degree?

Ritchie

08-30-2001, 12:07 AM

Oops, my mistake... Yes, you need to have a function the other way around. http://www.opengl.org/discussion_boards/ubb/smile.gif

I needed to compute a matrix and my angles were specified in degrees, so I had to convert to Rad.

This might be a bit OT, but I'm wondering.

Is it better to query OpenGL for a matrix, or to compute the matrices yourself?

The red book doesn't say much about this (I might have missed something though, and I feel it is better to compute them yourself).

What it does say is that it is better to use glRotate* instead of glMultMatrix or glLoadMatrix. It fails to explain why though. Performance reasons? Surely storing a matrix is cheaper than performing three matrix calculations?

Ritchie

[This message has been edited by Ritchie (edited 08-30-2001).]

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.