Extracting a direction from a Quaternion

Started by
2 comments, last by middy 20 years, 1 month ago
OK Here is my problem. I am using a Quaternion to store the rotation of my model. I rotateting using the following method

	/**
	 * Rotates _currentRotation Quaternion around an axis and an angle
	 * @param angle
	 * @param rotationAxis
	 */
	public void rotate(float angle,Vector3 rotationAxis){
		
		Quaternion	key_quat,		
					temp_quat;		// temp quaternion

		
		//create new Quat from axis and angle

		key_quat = new Quaternion(rotationAxis,angle);	
		
		temp_quat = _currentRotation;				
		_currentRotation = temp_quat.mult(key_quat);		
		
	}

I want the direction in a vector.,because I need it as velocity so I can increment my position, in the correct direction.

	public void simulate(float dt) {
	
		
		_velocity._x += (_force._x/_m) *dt;
		_velocity._y += (_force._y/_m) *dt;
		_velocity._z += (_force._z/_m) *dt;


		_position._x += _velocity._x *dt;
		_position._y += _velocity._y *dt;
		_position._z += _velocity._z *dt;
		
		
	}
Advertisement
simply take a vector that is suppose to be the forward direction of the vehicle, when no orientation is applied (like (0, 0, 1)), and multiply it by the quaternion.

Dir = Vector(0, 0, 1) * Quat;

Everything is better with Metal.

humm

Well I have drawn the damn velocity vector just to see it. It works when I DONT rotate :-)

//My quat is init like thisprotected Quaternion _currentRotation = new Quaternion(0,0,0,1);//I create this to make sure its rotated towards forwardthis._currentRotation = Quaternion.mulVectorWithQuaternion(xVector,_currentRotation,null);//this is my multvectorwith Quat code	/**	 *  multiply a vector and a quaternion , if dest is null it creates a new vector	 * @param v	 * @param q	 * @param dest	 * @return	 */	public static final Quaternion mulVectorWithQuaternion(Vector3 v,Quaternion q,Quaternion dest){			if(dest==null)				dest= new Quaternion(0,0,0,1);							dest._w =-(q._x*v._x + q._y*v._y + q._z*v._z);			dest._x = q._w*v._x + q._z*v._y - q._y*v._z;			dest._y = q._w*v._y + q._x*v._z - q._z*v._x;			dest._z = q._w*v._z + q._y*v._x - q._x*v._y;					return dest;			}


I thought the correct transformation for a rotation was v''=qv(q^-1). And since q is a unit quaternion, q^-1=q*.
.

This topic is closed to new replies.

Advertisement