Public Group

#### Archived

This topic is now archived and is closed to further replies.

# How do you use Quaternions to do rotations

This topic is 5371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I wan't to do rotations with quaternions, but I do not understand quaternions. What I want to do is rotate a given amount, but only rotate a little portion of it at a time. I also want to be able to get the angle of a rotation. Here is some of the code I am using (however, I have little clue as to what I am doing in the main part):
void DirectionalCameraRotator::updateCamera(const FrameEvent &evt)
{
long tmp;
Real rotateFactor = rotationDegreesPerSecond * evt.timeSinceLastFrame;

//update the camera on the Y axis

tmp = input->getMouseRelativeX();
if (invertRotations)
//invert the sign if we ARE inverting rotations

tmp ^= 0x80000000;
if (tmp < 0)
yaw(-rotateFactor); //cameraNode->yaw(-rotateFactor);

if (tmp > 0)
yaw(rotateFactor); //cameraNode->yaw(rotateFactor);

//update the camera on the X axis

tmp = input->getMouseRelativeY();
if (!invertRotations)
//invert the sign if we ARE NOT inverting rotations

tmp ^= 0x80000000;
if (tmp < 0)
pitch(-rotateFactor); //cameraNode->pitch(-rotateFactor);

if (tmp > 0)
pitch(rotateFactor); //cameraNode->pitch(rotateFactor);

//

//!!! THE REAL MEAT AND POTATOES PART OF THE FUNCTION !!!

//(so to speak)

//

Real theta;
Vector3 axis;
Quaternion deltaRotation;

//get the rotation

rotations.ToAngleAxis(theta, axis);

//restrict the rotation to the maximum

if (Math::Abs(theta) > rotateFactor) {
Real factor = rotateFactor / rotationDegreesPerSecond;
deltaRotation = Quaternion::Slerp(factor, Quaternion::IDENTITY, rotations);
rotations = rotations * deltaRotation.Inverse();
} else {
deltaRotation = rotations;
rotations = Quaternion::IDENTITY;
}

//set new rotation

cameraNode->rotate(deltaRotation);
}

u r what u bcome
[edited by - bcome on February 5, 2004 9:13:52 PM] [edited by - bcome on February 5, 2004 9:18:13 PM]

##### Share on other sites
quote:
Original post by bcome
but I do not understand quaternions

##### Share on other sites
yeah, start by reading the articles in the "Articles & Resources" section of this site on quaternions. there's enough there to get you understanding them farily well. Particularly the gamasutra article on quaterions is a great introductory article. Other than that, google for "quaternion tutorial" or something and read about it. you'll break your brain trying to just use quaternions in a zen way without any understanding of the math behind them. they are quirky and the only way to make your quaternion class work is to understand WHY they are quirky. things like order of operations are crucial in quaternions as is how modify them to represent new rotational states, etc. So read up.

-me

[edited by - Palidine on February 5, 2004 11:45:50 PM]

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 49
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632965
• Total Posts
3009546
• ### Who's Online (See full list)

There are no registered users currently online

×