# rotation with quaterion

## Recommended Posts

If I make my quaternion from an axis angle giving it the Y axis it rotate around fine.. but if give it the X axis it rotates kinda on an angle.. its not moving around the x axis.. First: setting up the rotations, angleToAddX/Y is the angle that it should be rotated around calculated from the position of the mouse curser
		Math::Quaternionf rotX;
Math::Quaternionf rotY;

rotX.normalize();
rotY.normalize();


then if I do; listElement.state.orientation = listElement.state.orientation * rotY; it rotates fine around the Y axis.. but if I do; listElement.state.orientation = rotX * listElement.state.orientation; it rotates around some other axis.. looks like its on a 45 degree from x,y,z.. what I realy want is to set the orientation so it rotates around the X axis and the Y axis at the same time using thoese two rotations.. [Edited by - Nanook on November 8, 2009 7:02:23 AM]

##### Share on other sites
This is the fromAngleAxis function by the way..

void fromAngleAxis(const qType rfAngle, const Math::Vector3<qType>& rkAxis)			{				// assert:  axis[] is unit length				//				// The quaternion representing the rotation is				//   q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)				qType fHalfAngle = 0.5*rfAngle;				qType fSin = sin(fHalfAngle);				w = cos(fHalfAngle);				x = fSin*rkAxis.x;				y = fSin*rkAxis.y;				z = fSin*rkAxis.z;			}

##### Share on other sites
I have found the following to work: Store the yaw and pitch separately and increase / decrease them as needed. Then rebuild the quaternion:

transform.resetOrientation(); // calls orientation.setIdentity();transform.rotate(yaw, 0.0f, 1.0f, 0.0f);transform.rotate(pitch, 1.0f, 0.0f, 0.0f);public void rotate(float angle, Vector3f axis) {	Quaternion rot = new Quaternion();	axis.normalise();		Vector4f a1 = new Vector4f(axis.x, axis.y, axis.z, 		angle * Constants.DEG2RAD);			rot.setFromAxisAngle(a1);			orientation = Quaternion.mul(orientation, rot, null);				}

##### Share on other sites
Hmm.. I dont have Vector4f class.. Aren't you doing the same thing as I am though?

##### Share on other sites
Quote:
 Original post by NanookHmm.. I dont have Vector4f class.. Aren't you doing the same thing as I am though?

The Vector4f is just storing a 4f vector, it's nothing magical - you already have a function for getting quaternion from axis-angle.

Err forget the whole storing pitch and yaw. I was looking at the camera class. Quaternion multiplication is not commutative though - try post-multiplying rotX instead of premultiplying. Other than that yes I think it's the same. These rotations occur in the local coordinate space of your object though - the axis change once your object rotates. Particularly once you rotate around the y-axis, your object's x-axis is no longer aligned with the world's x-axis. To always rotate in the same manner with respect to the world axis, you'll have to resort to keeping track of pitch and yaw and rebuilding the quaternion.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627676
• Total Posts
2978582

• 11
• 12
• 10
• 12
• 22