Having a OfDrawBox follow the rotation of an Of3dPrimitive

Hi,

I want to have a see through box that that surrounds my 3dPrimitive (Ex, Box), but having issues syncing rotations. I’ve been using OfDrawBox because OfNoFill does not seem to work on BoxPrimitive to achieve my goal.

Here my code simplified, the vector_rotation is a ofVec3f that gets it’s values changed by 3 ofParameter< float > (x,y,z), both box do rotate but they don’t seem to follow each other.

void ofApp::draw() {

	box.set(10);
	box.setPosition(0, 0, 0);

	ofSetColor(0, 0, 0);
	box.setOrientation(vector_rotation);
	box.draw();

        ofNoFill();
	ofSetColor(255, 0, 0);
	ofPushMatrix();
	ofRotateX(vector_rotation.x);
	ofRotateY(vector_rotation.y);
	ofRotateZ(vector_rotation.z);
	ofDrawBox(box.getPosition(), 10);
	ofPopMatrix();

}

I’m pretty sure there’s a discrepancy between the “SetOrientation” vs the 3 OfRotate but I’m unable to find a solution to make this works. I did look into quaternions, tho I’m not familiar with them.

Ex

it will work if you do in that order

	ofRotateZDeg(vector_rotation.z);
	ofRotateYDeg(vector_rotation.y);
	ofRotateXDeg(vector_rotation.x);
1 Like

Wow thank you so much, forgot about that detail :sweat_smile:

When I was moving the position of the Box the rotation wouldn’t follow since they were tied to the scene axis so I translated the coordinate on my 3D box.

It fixes the rotations problem I was having but for some reason there seem to be an offset with both positions after the translation, if I move on any Axis the OfDrawBox always ends up further despite using the same position. (Even through debugging the value are the same which is really weird)

void ofApp::draw() {

	box.set(10);
	box.setPosition(0, 0, 0)
	ofSetColor(0, 0, 0);
	box.setOrientation(vector_rotation);
	box.draw();

    ofNoFill();
	ofSetColor(255, 0, 0);
	ofPushMatrix();
    ofTranslate(box.getPosition());
	ofRotateX(vector_rotation.x);
	ofRotateY(vector_rotation.y);
	ofRotateZ(vector_rotation.z);
	ofDrawBox(box.getPosition(), 10);
	ofPopMatrix();

}

Weirdly enough if I multiply the position by 0.001 precisely the offset is gone, any reason why this offset occurs ?

ex2

are you setting position and orientation on both on the same order?
in fact your code draw() doesn’t reflect what you are doing in the gif

Yes it’s in the same order, I can actually remove everything related to scaling/rotation and the offset still occurs just by moving on the (X,Y,Z) axis, simplified below:


	ofBoxPrimitive box;
    box.set(10);
	box.setPosition(10, 0, 0);
	ofSetColor(0, 0, 0);
	box.draw();

	ofNoFill();
	ofSetColor(255, 0, 0);
	ofPushMatrix();
	ofTranslate(box.getPosition());
	ofDrawBox(box.getPosition(), 10);
	ofPopMatrix();

Only happens if I Translate on the Box Position, which is necessary for other transform.

you are translating twice.
try ofDrawbox(glm::vec3(0,0,0), 10);

1 Like

Yeh I realized the second I saw your comment … Thanks again and sorry, everytime I translate I forget to change my previous set position elsewhere to 0,0,0 … :sweat_smile:

Cheers !