I wanted to make the black line to spin on a plane formed by 2 vectors (a2, b2) (and the origin). I am trying to rotate a matrix around the cross product btw a2 and b2 (green line) using the rotate (angle, v0, v1, v2) method, which I lack of some understanding.
Vector b2 (described as a blue line) initializes as (0,1,0) so things seem to be right. Until you press “space” so values get randomized. The black line spins out of what I expected it to be its axis ( green line ). Alternatively, holding x, y or z while moving the mouse will change values on a2. [edited] You can see axis angle changes and orbiting the camera its possible to see black line successfully passes over blue and red lines. Problem is just when changing values of b2.
The code below is a little long. But I would like to understand what I am missing.
[edited the code normalizing some vectors I had forgot]
/*
space to randomize blue vector
hold x, y or z to move red vector
*/
import peasy.*;
float i = 0;
PeasyCam cam;
PMatrix3D mat = new PMatrix3D();
PVector a2 = new PVector(0.4, 1, 0).normalize();
PVector b2 = new PVector(0,1,0).normalize();
PVector axis;
void setup() {
size(300, 300, P3D);
cam = new PeasyCam(this, 200);
}
void keyPressed(){
if(key ==' ') {
b2 = PVector.random3D();
b2.normalize();
}
}
void draw() {
background(100);
if (keyPressed) moveVector();
strokeWeight(2);
stroke(#ff0000);
drawVector(a2);
stroke(#0000ff);
drawVector(b2);
stroke(#00ff00);
axis = b2.cross(a2);
axis.normalize(); //b2.copy().cross(a2.copy()).normalize();
println(axis);
drawVector(axis);
pushMatrix();
applyMatrix(updateMat());
fill(-1, 100);
box(20);
stroke(0, 100);
strokeWeight(4);
line(0, 0, 0, 50);
popMatrix();
}
PMatrix3D updateMat() {
float angle = PVector.angleBetween(a2, b2); //to replace testing
//reseting rotation before applying new aim
mat.m00 = 1;
mat.m01 = 0;
mat.m02 = 0;
mat.m10 = 0;
mat.m11 = 1;
mat.m12 = 0;
mat.m20 = 0;
mat.m21 = 0;
mat.m22 = 1;
float testing = radians( (frameCount) %360);
mat.rotate( testing , axis.x, axis.y, axis.z );
return mat;
}
void drawVector( PVector v ) {
float scale = 50;
PVector n = v.copy().mult(scale);
beginShape(LINES);
vertex(0, 0, 0);
vertex( n.x, n.y, n.z );
endShape();
}
void moveVector() {
if (key == 'x') a2.x = map(mouseX, 0, width, -1f, 1f);
if (key == 'y') a2.y = map(mouseX, 0, width, -1f, 1f);
if (key == 'z') a2.z = map(mouseX, 0, width, -1f, 1f);
a2.normalize();
}