Initialize a quaternion with one vector rotating to another direction initialize the Quaternary element based on the vector rotation direction

Source: Internet
Author: User

The first step I want to realize in my camera system using quaternion is to bulid a quaternion according to the camera eye "look at" Some a certain deriction. just say, we know at beginning the "front vector" of the camera points to (0,-1) (in right-hand coordinates system) and we let the camera look at somewhere and it points to (x, y, z ). hence we got two direction vectors for this camera, and th En the question is, how to build a quaternion? The aim to build that quaternion is to facilitate the future use of the camera.

I read that paper and got a little progress to build up a quaternion. And I designed a method to do that.Untested yet. Just "fire and forget...". You can see it as pseudo code.

 

In my camera system, I want to implement the first step on the four-element model. It is to build a four-element model based on the target point of the camera. For example, we know that the camera starts to point to (0, 0, 1) (in the right-hand coordinate system), and then we set the camera to a certain orientation (x, y, z ). In this way, we get two direction vectors. The next question is, how can we build a quaternary matrix? The purpose of constructing this element is of course to facilitate subsequent operations on the camera.

I recently read the paper and made a little progress in understanding it. I wrote a method to achieve this goal.Not tested. In addition, you can understand it as a pseudo-code.

------------------------------------------------------------------

P.s. This code has been tested and it works well .~

Note: This code was tested later and runs well ~

 

 

Qutearnion: qutearnion (vector3 from, vector3 DEST)
{
// 1. First to normalize the two vectors
If (! From. isnormalized ()){
From. normalize ();
}
If (! DeST. isnormalized ()){
DeST. normalize ();
}

// 2. Then get their outerproduct vector that is
// Perpendicular both to the two SRC Vectors
Vector3 axis = from. crossproduct (DEST );

// 3. And get its length and normalize it to be a unit
// Vector
Float axis_length = axis. normalize ();

// 4. Since we have such a formula:

/* If: c = A x B
/* Then: | c | = | A | * | B | * sin <A, B> */

// And we also know that the two SRC vectors'
// Length both equals to 1 (they 've been normalized ).
// So the length of the outerproduct equals
// Sin <A, B>. And we can get the value of the Angle
// By using the arcsin () function.

Float Sita = Asin (axis_length); // calling math lib

// 5. But we need to have a sense that, the angle we
// Got is the one of the two possible candidates.
// That's because the result of function arcsin ()
// Is limited between [-PI/2, PI/2], but actually
// We always measure the angle between two vectors
// Between [-2 * Pi, 2 * Pi] (negative angle means
// Reverse result vector). So it exists a potential
// Candidate, say, maybe actuall 150 degree if we
// Get a result of 30degree.
//
// The dot product (innerproduct) is a good way
// Help us choose the right one. Since we have such
// Formula below:

// D = a * B = | A | * | B | * Cos <A, B>

// Since we already knew that both A and B are Unit
// Vectors, the result of dot product equals to
// Function cos () for that angle, which is exactly
// The one we discussed above. So we can kickoff one
// Invalided value through the polarity of the CoS ().
//
// If cos () is negative, we then need pi to minus
// Angle and reassign to it.

// For example, we got a result-60 degree
// Two vectors, and we know that maybe it's actually
//-120degree. Then we dot producct them and find
// Innerproduct <0. Since cos (-150 degree) <0 so we hence
// Kickoff the-60 degree and remain-150 degree, which is
// Exactly we want.

Float dot_product = from. dotproduct (DEST );
If (dot_product <0 ){
Sita = pi-sita;
}

// 6. Because there is a theorem for Quaternion which says,
// If n (q) = 1, then q = [V ösin W, cos w]
// Acts to rotate around unit axis v öby 2 W.

// Since we knew the SITA is the angle that the "from"
// Vector rotates to "DEST" vector. So obviusly
// Quaternion which its W equals to Sita/2 can make
// "From" vector rotate to "DEST" vector by Sita.

Sita = Sita/2;

// 7. Finally initialize this Quaternion.

Initialize (axis * sin (SITA), cos (SITA ));

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.