How do I find the rotateX,rotateY, and rotateZ values to make any vector vertical?

I’ve got two points in 3d space, and I want to draw a box between them that acts like a line. If I can translate to get to the first point, how would I find the rotation values that could make my box aligned so that one of the edges of the box connects from the first point to the second point?

Check out line3D function here Controlling rotational motion in Processing - #2 by Chrisir

1 Like

I haven’t tested but perhaps lookAt in this article is what you need

Basically you need to translate to the midpoint of two points, rotate to lookAt one of the points and scale it so that the box reaches both points.

1 Like

simplified of my link above

// Demo program for a 3D line 

final color RED   = color(255, 0, 0); 
final color GREEN = color(0, 255, 0); 
final color BLUE  = color(0, 0, 255); 

final float a = 200; 

PVector point0 = new PVector (  -a, -a, -3*a); // point 0
PVector point1 = new PVector (  133, 10, 2.1*a); // point 1 

// -------------------------------------------------------

void setup() {
  size(1200, 700, P3D);
} // func

void draw() {
  background(0);
  lights();

  translate (width/2, height/2, 0);

  // decoration: Two spheres
  sphere3DPVector(point0, 17, GREEN );
  sphere3DPVector(point1, 17, RED );

  // 3D Line 
  line3DPVector ( point0, point1, 13, BLUE );
} // func 

// ---------------------------------------------------------

void line3DPVector(PVector p1, PVector p2, 
  float weight, 
  color colorLine) {
  // drawLine was programmed by James Carruthers
  // see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9 

  PVector v1 = new PVector(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z);
  float rho = sqrt((v1.x * v1.x) + (v1.y * v1.y) + (v1.z * v1.z));
  float phi = acos(v1.z/rho);
  float theta = atan2(v1.y, v1.x);
  v1.mult(0.5);

  pushMatrix();
  translate(p1.x, p1.y, p1.z);
  translate(v1.x, v1.y, v1.z);
  rotateZ(theta);
  rotateY(phi);
  stroke(22);   //noStroke();// use noStroke or weak stroke here 
  fill(colorLine);

  box(weight, weight, p1.dist(p2));
  popMatrix();
  //
} // method

//---------------------------------------------------------------------------------------

void sphere3DPVector (PVector p1, 
  float size1, 
  color col ) {
  fill(col);
  noStroke();
  pushMatrix();
  translate(p1.x, p1.y, p1.z);
  sphere(size1);
  popMatrix();
}
//

Hello,

I did this a few different ways:

  • Trigonometry to get the angles and then do 2 rotations.
  • Convert the Cartesian co-ordinate to spherical co-ordinates and do 2 rotations.
    It is a worthwhile exercise to do this from scratch from the formulas provided.
  • Use PVectors to get the angleBetween() and do 2 rotations.

This will help visualize:

:)