hi, I’m having a few troubles understanding rotations. I’m currently creating a simple butterfly and trying to place the wings along the head-tail axis which I have.
ofDrawSphere(head, 5); // head of butterfly
ofDrawSphere(tail, 5); // tail of butterfly
ofPushMatrix();
ofTranslate(tail); // Now my origin is my butterfly tail
// Rotate my matrix so that my two wings are created along the head-tail axis
ofQuaternion quat;
quat.makeRotate(glm::vec3(1, 1, 1), tail - head);
ofRotate(quat); // wait ofRotate doesn't take quats ?
glm::vec3 plane1 = glm::vec3(- 30, 0, 0); // Create Left Wing
ofDrawPlane(plane1, 50.0, 50.0);
glm::vec3 plane2 = glm::vec3( 30, 0, 0); // Create Right Wing
ofDrawPlane(plane2, 50.0, 50.0);
// when I've found out how to solve my first pb I can try rotating the wings around thea head-tail axis
ofPopMatrix();
I’d really love to avoid going deep into quaternions, and was wondering what kind of wrapper functions could apply intuitively to my case and more generally in openframeworks. I’m having some trouble udnerstanding either the doc or the other topics with same questions.
This is one of the case where using ofNode really make the difference. You could create your own ofNode butterfly, that has as childs two plane for the wings and two sphere for the bodies, and then simply call the rotateDeg() method on the parent object. You can find a good usage example and relative documentation here https://github.com/openframeworks/openFrameworks/tree/patch-release/examples/3d/ofNodeExample
I’m having some troubles using ofNode. As I have multiple butterflies, each time I draw more than one, the application won’t start at all. I’m using ofNode as empty parent geometry, it works fine when I have just one butterfly, or even n butterflies if I’m using ofBoxPrimitive as a parent geometry, is it not intented to call ofNode directly ?
Also do you happen to know any bits of code or any more examples surrounding rotations with ofNode ? I’m hitting almost the same questions I was hitting before, even though it does seem a lot more practical thanks
hey, yes sure, I’ve pushed the project on github and made sure it starts exactly on the mentioned scene, the project is here https://github.com/itsKaspar/tilde2
when I set the nBoids (number of boids) parameter to 2 inside Boids.cpp or more the window never opens
One problem at time. First, your initial problem was the rotation of the elements. Is this solved? are you able to rotate the butterflies?
The problem that you are describing in this last post has nothing to do with rotation, but with dinamically resizing the vector containing the boids. I would suggest you to make a small application, where you have one single class, multiple instances of that class are created and pushed into the vector in the setup method. Then something has to happen when you change the size of the vector, like clear the whole vector and ri-initialize it.
not yet, even before rotation actually I don’t get why my butterfly wings aren’t moving with my butterfly.
I should have parented them correctly to the ofNode using
Every Primitive in OF inherits from ofNode, that’s why it works.
If you have “ofNode butterfly;” in your header and the code you posted in your setup method, when you call “buterfly.rotateYDeg(1)”, for example, in your update method, that wings should rotate together with the butterfly node. Note that you are rotating on the Y axis of the buttefly node. you can call butterfly.draw() to see the axis.
lWing.setParent(butterfly);
rWing.setParent(butterfly);
lWing.move(-110, 0,0); //offset both wings
rWing.move(110, 0, 0);
butterfly.move(pos); // update to current butterfly position
this in my update function
pos += vel;
butterfly.move(vel);
and this in my draw function
butterfly.draw();
rWing.draw();
lWing.draw();
yet my butterfly ofNode flies away, but my wings stay in place at the origin (camera is rotated a little)
You could also use the transformGL function inside ofNode if you wanted to avoid the parenting issue. Move around and rotate the butterfly node, acting as the global transform and then manipulate the wings as if it were local.
You can also try using the lookAt function in ofNode. Using velocity could be a simple approach
butterfly.move(vel);
// you'll need to make sure your velocity is not zero
if( glm::length(vel) > 0.0 ) {
glm::vec3 up( 0, 1, 0);
// lookAt() aligns using the -z vector I believe
butterfly.lookAt( butterfly.getPosition() + vel, up );
}
In the draw function, try something like
butterfly.transformGL(); { // moves everything into the butterfly transform space
rWing.draw();
lWing.draw();
} butterfly.restoreTransformGL();