clem
April 6, 2018, 11:23am
#1
I have this from an old tutorial:
//Set initial coordinate system
axeX = ofPoint( 1, 0, 0 );
axyY = ofPoint( 0, 1, 0 );
axyZ = ofPoint( 0, 0, 1 );
...
float time = ofGetElapsedTimef();
float twistAngle = 5.0 * ofSignedNoise( time * 0.3 + 332.4 );
float rotateAngle = 1.5;
//Rotate the coordinate system of the circle
axeX.rotate( twistAngle, axyZ );
axyY.rotate( twistAngle, axyZ );
axeX.rotate( rotateAngle, axyY );
axyZ.rotate( rotateAngle, axyY );
And I’ve tried converting it to the new gym syntax but it doesn’t seem to be rotating as expected…
//Set initial coordinate system
axeX = glm::vec3( 1, 0, 0 );
axeY = glm::vec3( 0, 1, 0 );
axeZ = glm::vec3( 0, 0, 1 );
float time = ofGetElapsedTimef();
float twistAngle = 5.0 * ofSignedNoise( time * 0.3 + 332.4 );
float rotateAngle = 1.5;
...
// Rotate the coordinate system of the circle
glm::rotate( axeX, twistAngle, axeZ);
glm::rotate( axeY, twistAngle, axeZ );
glm::rotate( axeX, rotateAngle, axeY );
glm::rotate( axeZ, rotateAngle, axeY );
Is the issue that, in those last 4 lines, I should have a matrix as the first variable? Not sure how this works.
Any help would be appreciated.
edapx
April 6, 2018, 12:17pm
#2
Hello @clem , yes the glm syntax is sligthly different. There are two ways to rotate a vector:
the first one is what you were trying
auto vec = glm::vec3(0.2, 0.6, 1.0);
float angle = 90; // since glm version 0.9.6, rotations are in radians, not in degrees
// see https://github.com/g-truc/glm/issues/570
cout << vec << endl;
auto rotatedVec = glm::rotate(vec, angle, glm::vec3(0.0f, 1.0f, 0.0f));
cout << rotatedVec << endl;
The second one is to create a rotation matrix, and then multiplying your vector for that matrix.
glm::mat4 mat = glm::rotate(angle,glm::vec3(0.0f, 1.0f, 0.0f));
auto rotVector = mat * glm::vec4(vec, 1.0);
cout << rotVector << endl; // the result will be the same as in your previous operation
You can also create a rotation matrix from a quaternion, see:
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/#how-do-i-create-a-quaternion-in-c-
2 Likes
clem
April 6, 2018, 3:20pm
#3
Cheers! 2 things I was doing wrong, based on your answer: using degrees instead of radians & not actually assigning a new value to the variables.
The code that gives me the result I wanted is this:
//Set initial coordinate system
axeX = glm::vec3( 1, 0, 0 );
axeY = glm::vec3( 0, 1, 0 );
axeZ = glm::vec3( 0, 0, 1 );
float time = ofGetElapsedTimef();
float twistAngle = 0.1 * ofSignedNoise( time * 0.3 + 332.4 ); // 5 degrees = 0.1 Radians approx
float rotateAngle = 0.02; // 1.5 degrees = 0.02 Radians approx
...
// Rotate the coordinate system of the circle
axeX = glm::rotate( axeX, twistAngle, axeZ);
axeY = glm::rotate( axeY, twistAngle, axeZ );
axeX = glm::rotate( axeX, rotateAngle, axeY );
axeZ = glm::rotate( axeZ, rotateAngle, axeY );
With the first assignment of the variables in a different function, of course.
edapx
April 6, 2018, 3:24pm
#4
Happy that it worked. By the way, glm offers utilities to convert radians to degree and viceversa, you do not need approximations.
https://glm.g-truc.net/0.9.4/api/a00136.html
genType glm::degrees ( genType const & radians )
genType glm::radians ( genType const & degrees )
1 Like
clem
April 6, 2018, 3:30pm
#5
Great. Works perfectly now with this:
float twistAngle = glm::radians(5.0) * ofSignedNoise( time * 0.3 + 332.4 );
float rotateAngle = glm::radians(1.5);
The degrees need to be floating point, I’ve noticed.