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.
IndexProgramming Questions & HelpPrograms › Rotating an object toward another in 3D
Page Index Toggle Pages: 1
Rotating an object toward another in 3D (Read 1029 times)
Rotating an object toward another in 3D
Feb 9th, 2010, 10:02am
 
Hi,

I'm trying to rotate an object towards another object in 3d space. I'm rotating along the Y (vertical) axis, but am having some issues. I have a few translations in my code, and am unsure if this is effecting the calculations for my rotation (for that matter, i'm also unsure if my rotation calculation is correct). For example, I am calculating the Y axis rotation with the following:

Code:
      float angle = atan2(z2-z1,x2-x1);
     float  rotation = angle * (180/PI);


The context in which rotation occurs looks like this:
     
Code:
      translate(width/2, height/2)
     float angle = atan2(z2-z1,x2-x1);
     float  rotation = angle * (180/PI);
     pushMatrix();
     translate(x1,y1,z1);
     rotateY(rotation);
     //draw a few lines here
     popMatrix();


This doesn't seem to be rotating my first object (x1,y1,z1) toward the second (x2,y2,z2) along the Y axis.  I'm wondering if i need to factor in the translations i'm doing into my angle calculation, or if my angle calculation is simply wrong to begin with.

Thanks.
Re: Rotating an object toward another in 3D
Reply #1 - Feb 9th, 2010, 11:36am
 
It's hard to tell without seeing the rest of your code.  You might need to put your angle measurement within your pushMatrix():
Code:

translate(width/2, height/2)
pushMatrix();
float angle = atan2(z2-z1,x2-x1);
float rotation = angle * (180/PI);
translate(x1,y1,z1);
rotateY(rotation);
//draw a few lines here
popMatrix();





Re: Rotating an object toward another in 3D
Reply #2 - Feb 9th, 2010, 9:30pm
 
catstack wrote on Feb 9th, 2010, 10:02am:
Code:
float angle = atan2(z2-z1,x2-x1);
float  rotation = angle * (180/PI);
rotateY(rotation);

So long as you're only concerned with rotating on the Y axis, this is the correct method, except your mistake is in thinking that rotateY needs an angle in degrees. The angle should be in radians, as returned from atan2().

Code:
float angle = atan2(z2-z1,x2-x1);
rotateY(angle);


If you try to orient "in 3D" you will run into problems much more complicated than this.

-spxl
Re: Rotating an object toward another in 3D
Reply #3 - Feb 11th, 2010, 7:06pm
 
Ah, thanks for the tip subpixel. I've been translating like crazy in 3d, which seemed to be the primary cause of the issue.  
Page Index Toggle Pages: 1