Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Quaternions, rotate a model and align with a direction


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 hdlopesrocha   Members   -  Reputation: 145

Like
0Likes
Like

Posted 06 November 2012 - 03:09 PM

Suppose you have quaternion that describes the rotation of a 3D Model.
What I want to do is, given an Object (with rotationQuaternion, side vector...), I want to align it to a target point.
For a spaceship, I want the cockpit to point to a target.
Here is some code I have ... It's not doing what I want and I don't know why...

		if (_target._ray.Position != _obj._ray.Position)
		{
			Vector3 vec = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);
			float angle = (float)Math.Acos(Vector3.Dot(vec, _obj._ray.Direction));
			Vector3 cross = Vector3.Cross(vec, _obj._ray.Direction);
			if (cross == Vector3.Zero)
				cross = _obj._side;
			_obj._rotationQuaternion *= Quaternion.CreateFromAxisAngle(cross,angle);
		}
		// Updates direction, up, side vectors and model Matrix
		_obj.UpdateMatrix();


after some time the rotationQuaternion is filled with almost Zero at X,Y,Z and W
Any help? Thanks ;-)

Sponsor:

#2 max343   Members   -  Reputation: 340

Like
0Likes
Like

Posted 06 November 2012 - 06:49 PM

Do you normalize your final quaternions before transforming them into a matrix (or just at the end of the frame)? If not then you should.

#3 hdlopesrocha   Members   -  Reputation: 145

Like
0Likes
Like

Posted 07 November 2012 - 06:43 AM

Mmmm, probably it's that...

#4 hdlopesrocha   Members   -  Reputation: 145

Like
0Likes
Like

Posted 07 November 2012 - 01:47 PM

ok now it's rotating like crazy...

		    if (_target._ray.Position != _obj._ray.Position)
		    {
			    Vector3 vec = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);
			    float angle = (float)Math.Acos(Vector3.Dot(vec, _obj._ray.Direction));
			    Vector3 cross = Vector3.Cross(vec, _obj._ray.Direction);
			 
			    cross = (cross == Vector3.Zero) ? _obj._side : Vector3.Normalize(cross);
			
			    _obj._rotationQuaternion *= Quaternion.CreateFromAxisAngle(cross,angle);
			    _obj._rotationQuaternion.Normalize();
		    }
		    _obj.UpdateMatrix();

I have normalized the cross and quaternion...

#5 max343   Members   -  Reputation: 340

Like
0Likes
Like

Posted 07 November 2012 - 03:14 PM

"Like crazy" in a good or bad way?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS