c++ - Is it possible to rotate a vector in 3D space using quaternions only? -


i want rotate vector in 3d space around origin.

let's have hypothetical polygon centered around origin, , laying perpendicular y-axis.

i want rotate polygon around arbitrary axis arbitrary rotation amount.

example: rotate around y axis 90 degrees. view along -y axis.

rotate 90 degrees

one way create rotation matrix , apply rotation each of points of polygon. can presently this.

however, decided wanted achieve using quaternions.

attempt: rotate around y axis 90 degrees. view along -y axis.

failure

it seems there's wrong code.

to try , isolate problem, created function qrotate should rotate single point (or vector) around origin. check math correct, derived final rotation using 2 different methods. however, both methods yield identically incorrect result.

void qrotate(glm::vec3 point, glm::vec3 rotate) {   printf("\n\n%f %f %f around point %f %f %f",            rotate.x, rotate.y, rotate.z, point.x, point.y, point.z);    //create quaternion.   glm::quat orientation = glm::quat(rotate);    //normalize   orientation = glm::normalize(orientation);    /*method 1 - pure vector quaternion.*/   //create 'pure' vector quaternion   glm::quat pure = glm::quat(0.0, point.x, point.y, point.z);   glm::quat qpq = orientation * pure * (glm::conjugate(orientation));   //form result   glm::vec3 result = glm::vec3(qpq.x, qpq.y, qpq.z);   printf("\nresult1 = %f %f %f", result.x, result.y, result.z);    /*method 2 - apply orientation point.*/   glm::vec3 alpha = orientation * point;   printf("\nresult2 = %f %f %f", alpha.x, alpha.y, alpha.z); } 

some sample inputs , outputs:

qrotate(glm::vec3(10.0, 0.0, 0.0), glm::vec3(0.0, 45.0, 0.0));  0.000000 45.000000 0.000000 around point 10.000000 0.000000 0.000000 result1 = 5.253221 0.000000 -8.509035 result2 = 5.253221 0.000000 -8.509035 

(expected results - close 7.5, 0.0, 7.5)

qrotate(glm::vec3(10.0, 0.0, 0.0), glm::vec3(0.0, 90.0, 0.0));  0.000000 90.000000 0.000000 around point 10.000000 0.000000 0.000000 result1 = -4.480736 0.000000 -8.939966 result2 = -4.480736 0.000000 -8.939966 

(expected results - 0.00000, 0.00000, 10.00000)

qrotate(glm::vec3(10.0, 0.0, 0.0), glm::vec3(0.0, 180.0, 0.0)); result1 = -5.984600 0.000000 8.011526 result2 = -5.984600 0.000000 8.011526 

(expected results - -10.00000, 0.00000, 0.0000)


it's understanding math checks out, therefore, there must wrong way trying use it. have seen many resources taking rotation quaternion (orientation) , converting 4x4 matrix, applying rotation vector, , converting matrix (rotated) 3d vector.

in other parts of code, perform quaternion-matrix-vector conversion, thought seemed inefficient.

i've read it's possible derive rotated point using quaternions alone, , have used algorithms suggested in code. considered results may not garbage , in fact representative of useful, don't know what.

is not possible quaternions alone?

your function rotates angles in radians input angles in degrees. should either input angles in radians, or convert degrees radians @ top of function.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -