We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Index › Angle / rotation between two points in 3D
 Pages: 1
 Angle / rotation between two points in 3D (Read 2061 times)
 eric_p Angle / rotation between two points in 3D Mar 7th, 2009, 3:46am   Making my first steps into 3D right now. I'm able to draw a line between two 3D points that are stored as PVectors by using: Code:```line(location1.x,location1.y, location1.z, location2.x, location2.y, location2.z); ```Now I want to change that line into a solid rectangle, but I'm having problems finding the values that I need to rotate the X, Y or Z plane by in order to have it align with the line. I tried using PVector's angleBetween() method, but that just gives me one number which I don't know how to use. After some googling I started messing about with the atan2 function and PVector's  dot() method which got me somewhere, but not in the right place. I have to say that my understanding of 3D trigonometry is as of yet pretty limited. So maybe someone could point me in the right (and easy to understand) direction? zzzzzz...
 bohnacker Re: Angle / rotation between two points in 3D Reply #1 - Mar 9th, 2009, 5:19pm   I hope the following code might help you. I did this for own sketches and think that it is mathematically correct. The main thing is the function cartesianToPolar that returns a PVector with (length, angleY, angleZ). The code in setup() is just to show you, how to use the function. Code:```void setup() {  size(800, 800, P3D);  PVector location1 = new PVector(-3, 2, -3);  PVector location2 = new PVector(4, -1, -2);  PVector d = PVector.sub(location2, location1);  println("vector from location1 to location2 : " + d);  PVector polar = cartesianToPolar(d);  println("length of this vector              : " + polar.x);  println("rotation angle around y-axis       : " + polar.y);  println("rotation angle around z-axis       : " + polar.z);  // draw the line  translate (width/2, height/2);  scale(100);  line(location1.x, location1.y, location1.z, location2.x, location2.y, location2.z);  // draw the rectangle  fill(255, 0, 0, 100);  noStroke();  rectMode(CORNERS);  translate(location1.x, location1.y, location1.z);  rotateY(polar.y);  rotateZ(polar.z);  rect(0, 0, polar.x, 1);}// Converts 3D cartesian coordinates to polar coordinates// // theVector : vector to convert// returns   : vector containing 'length', 'angleY' and 'angleZ', //             so that rotating a point (length, 0, 0) first  //             around the y-axis with angleY and then around the  //             z-axis with angleZ results again in point (x, y, z)PVector cartesianToPolar(PVector theVector) {  PVector res = new PVector();  res.x = theVector.mag();  if (res.x > 0) {    res.y = -atan2(theVector.z, theVector.x);    res.z = asin(theVector.y / res.x);  }  else {    res.y = 0;    res.z = 0;  }  return res;} ```
 eric_p Re: Angle / rotation between two points in 3D Reply #2 - Mar 10th, 2009, 4:47am   Ahhh! That's how to do it.Simple enhough actually.Well, you're a hero, thanks a lot! zzzzzz...
 bohnacker Re: Angle / rotation between two points in 3D Reply #3 - Mar 10th, 2009, 9:18am   I'm happy, it helped!
 Pages: 1