im trying to (interpolate / lerp / tween) between two ofMatrix4x4.
where my approach falls short is when the interpolation needs to take into account the “registration point” as they call it in flash. the “registration point” being the centre around which the matrix transformation needs to take place… for example if the registration point is the centre of the box, when spun, the box will rotate around its centre. here is my working code so far,
static float mod1 = 1.0;
void ofApp::draw(){
float p = modf(ofGetElapsedTimef() * 0.1, &mod1);
ofRectangle box(0, 0, 200, 200);
ofVec3f pos0(100, 100);
ofVec3f pos1(300, 100);
ofVec3f registration(100, 100);
ofMatrix4x4 mat0;
mat0.preMultTranslate(pos0);
mat0.preMultTranslate(registration);
mat0.preMultRotate(ofQuaternion(0, ofVec3f(0, 0, 1)));
mat0.preMultTranslate(-registration);
ofMatrix4x4 mat1;
mat1.preMultTranslate(pos1);
mat1.preMultTranslate(registration);
mat1.preMultRotate(ofQuaternion(90, ofVec3f(0, 0, 1)));
mat1.preMultTranslate(-registration);
ofVec3f p0(mat0.getTranslation());
ofVec3f p1(mat1.getTranslation());
ofVec3f s0(mat0.getScale());
ofVec3f s1(mat1.getScale());
float r0, r1;
ofVec3f zAxis(0, 0, 1);
mat0.getRotate().getRotate(r0, zAxis);
mat1.getRotate().getRotate(r1, zAxis);
ofVec3f pos = (p1 - p0) * p + p0;
ofVec3f scl = (s1 - s0) * p + s0;
float rot = (r1 - r0) * p + r0;
ofMatrix4x4 mat;
mat.preMultTranslate(pos);
mat.preMultRotate(ofQuaternion(rot, ofVec3f(0, 0, 1)));
mat.preMultScale(scl);
ofPushMatrix();
ofMultMatrix(mat);
ofSetColor(255);
ofRect(box);
ofSetColor(0);
ofCircle(registration, 4);
ofSetColor(255);
ofPopMatrix();
ofSetColor(0);
ofCircle(pos0 + registration, 4);
ofCircle(pos1 + registration, 4);
ofLine(pos0 + registration,
pos1 + registration);
ofSetColor(255);
}
this is what this code looks like in action,
but what should be happening is the centre of the box should be rotating and moving along the line.
just to explain the code a bit,
ofRectangle box(0, 0, 200, 200);
ofVec3f pos0(100, 100);
ofVec3f pos1(300, 100);
ofVec3f registration(100, 100);
pos0 and pos1 are the start and end positions of the box.
registration is the centre position of the box around which it rotates.
ofMatrix4x4 mat0;
mat0.preMultTranslate(pos0);
mat0.preMultTranslate(registration);
mat0.preMultRotate(ofQuaternion(0, ofVec3f(0, 0, 1)));
mat0.preMultTranslate(-registration);
ofMatrix4x4 mat1;
mat1.preMultTranslate(pos1);
mat1.preMultTranslate(registration);
mat1.preMultRotate(ofQuaternion(90, ofVec3f(0, 0, 1)));
mat1.preMultTranslate(-registration);
mat0 and mat1 are the two matrices of where the box is at the start and finish.
ofVec3f p0(mat0.getTranslation());
ofVec3f p1(mat1.getTranslation());
ofVec3f s0(mat0.getScale());
ofVec3f s1(mat1.getScale());
float r0, r1;
ofVec3f zAxis(0, 0, 1);
mat0.getRotate().getRotate(r0, zAxis);
mat1.getRotate().getRotate(r1, zAxis);
ofVec3f pos = (p1 - p0) * p + p0;
ofVec3f scl = (s1 - s0) * p + s0;
float rot = (r1 - r0) * p + r0;
ofMatrix4x4 mat;
mat.preMultTranslate(pos);
mat.preMultRotate(ofQuaternion(rot, ofVec3f(0, 0, 1)));
mat.preMultScale(scl);
this is the interpolation code between the two matrices.
as you can probably see, it does not take the registration point into account… and thats what im trying to figure out.
the rest is just drawing everything to the screen.
it would be amazing if someone could help me with this problem in some way.