Few things I spot that need a couple of tweaks:
1- Your matrix stack implementation is not correct, you are trying to rotateX + rotateY + axis-angle rotationg in the same transformation block dont get me wrong it is possible to do it in the same block, but other steps needs to be implemented.
2- The rotation code I post have a fix target vector in this case the xyzVec should be your target so you need to update the xyzVec to your desired point in space for the rotation to work
3- I made a mistake, the angleOfRotation should be calculated by the xyzVec and the upVec, example of this in the code below
The following code have the green line always looking at the black dot
- import processing.opengl.*;
- PVector xyzVec = new PVector( 50, 60, 100 ); // our target vector
- void setup() {
- size(800, 800, OPENGL);
- }
- void draw() {
- background(255);
-
- // This is the posiiton of the black point,
- // we'll use a copy of this as our target vector to rotate
- // I made it so the point goes to the mouse "xy" position and goes back and forward using sin() in "z"
- xyzVec.x = mouseX - width / 2;
- xyzVec.y = mouseY - height / 2;
- xyzVec.z = sin( millis() * 0.001 ) * 300;
-
- // Apply transformations to the point only
- pushMatrix();
- translate(width/2, height/2);
- // Draw black point
- stroke(0);
- strokeWeight(10);
- point(xyzVec.x, xyzVec.y, xyzVec.z);
- popMatrix();
-
- // Target vec, this is the vector we'll us to rotate
- // our target is the xyzVec to targetVec is a copy of xyzVec
- PVector targetVec = xyzVec.get();
- targetVec.normalize();
-
- PVector upVec = new PVector( 0, 1, 0 );
- // Get axis of rotation from targetVec and the upVec
- PVector axisOfRotation = targetVec.cross( upVec );
- axisOfRotation.normalize();
-
- // Get angle of rotation from targetVec and upVec
- float angleOfRotation = PVector.angleBetween( targetVec, upVec );
-
- // Apply transformation to the line and box only
- pushMatrix();
- translate(width/2, height/2);
- // Apply rotation
- rotate(-angleOfRotation, axisOfRotation.x, axisOfRotation.y, axisOfRotation.z );
-
- // Draw green line
- stroke(0, 255, 0);
- line(0, 0, 0, 200);
-
- // Draw red wireframe box
- noFill();
- strokeWeight(1);
- stroke( 255, 0, 0 );
- box( 100 );
- popMatrix();
- }
Hope that helps