Intuitive way to have ofRotate align with 3D vector?

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

	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


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

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 :slight_smile: thanks

Could you share your code? how many butterflies are you drawing?

hey, yes sure, I’ve pushed the project on github and made sure it starts exactly on the mentioned scene, the project is here

when I set the nBoids (number of boids) parameter to 2 inside Boids.cpp or more the window never opens

My single Boid class is here :


and Boid.h

But if I replace ofNode by any other type of geometry it works fine, am I supposed to call a pointer of ofNode or somth ?

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


If I replace ofNode by an ofBoxPrimitive I can clearly see the box moving and my butterfly wings staying behind.

I’ve pushed this change to the git as I’m trying to understand what’s wrong with my parenting and comparing with the example you gave me

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.

I have this is my header

	ofNode butterfly;
	ofPlanePrimitive lWing ,rWing;

this in my butterfly setup


	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;

and this in my draw function


yet my butterfly ofNode flies away, but my wings stay in place at the origin (camera is rotated a little)

is there something wrong in my call order ?
I dont see where the problem could be

I’ve found a lead concerning this while reproducing the problem in a micro environment, I’ve wrapped it up here and made a downloadable reporducible example here : ofNode parenting not working when ofNode is in std::vector


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

// 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
} butterfly.restoreTransformGL();