using atan2 to rotate objects 3-dimensional in relation to another position (mouse position)
in
Programming Questions
•
6 months ago
hello people outthere,
I´ve tried a function with "atan2()" to rotate objects (in this case "rects") 3-dimensional in positioning to the mouse (but it could be any other position as well). First I thought that could work, but as I made a test by drawing a "box" on the "rects", that have always the same height as the distance from the position of the rect to the position of the mouse, I saw it doesnt: when it would be right, the top of the boxes should all meet at the position of the mouse (ellipse).
Is there a way to handle it with "atan2()"? What do you suggest?
have a look! Thanks!!
I´ve tried a function with "atan2()" to rotate objects (in this case "rects") 3-dimensional in positioning to the mouse (but it could be any other position as well). First I thought that could work, but as I made a test by drawing a "box" on the "rects", that have always the same height as the distance from the position of the rect to the position of the mouse, I saw it doesnt: when it would be right, the top of the boxes should all meet at the position of the mouse (ellipse).
Is there a way to handle it with "atan2()"? What do you suggest?
have a look! Thanks!!
- import processing.opengl.*;
ArrayList<myRect> rectlist = new ArrayList<myRect>();
int rectsize = 100;
int myBorder = 6;
int myDepth = -100;
void setup(){
size(300,300, OPENGL);
smooth();
rectMode(CENTER);
ellipseMode(CENTER);
for(int i = rectsize; i<width; i+=rectsize){
for(int e = rectsize; e<height; e+=rectsize){
rectlist.add(new myRect(i, e));
}
}
}
void draw(){
background(255);
for (int i = 0; i<rectlist.size(); i++){
rectlist.get(i).display();
}
ellipse(mouseX, mouseY, 10,10);
}
/*class of rotating objects*/
class myRect {
float x,y,z, mySize,xR, yR, zR, myDist;
myRect(float _x, float _y){
x = _x;
y = _y;
z = myDepth;
mySize = rectsize-myBorder;
}
void display(){
myDist = dist(x,y,z,mouseX, mouseY,0);
xR = func_rotateX(y, z)+HALF_PI;
yR = func_rotateY(x, z)+HALF_PI;
stroke(0);
strokeWeight(2);
pushMatrix();
translate(x,y,z);
rotateX(xR);
rotateY(yR);
noFill();
rect(0,0,mySize, mySize);
fill(0);
strokeWeight(1);
stroke(0,100);
ellipse(0, 0, 10, 10);
fill(255,0,0);
ellipse(-mySize/2, -mySize/2, 10, 10);
ellipse(mySize/2, mySize/2, 10, 10);
ellipse(-mySize/2, mySize/2, 10, 10);
ellipse(mySize/2, -mySize/2, 10, 10);
noStroke();
translate(0,0,myDist/2);
/*testbox: to check if the 3D orientation works, the boxes should meet at the mouse*/
box(2, 2, myDist);
popMatrix();
}
/*important functions for the orientation*/
float func_rotateX(float _y, float _z) {
return atan2(0-_z,mouseY-_y);
}
float func_rotateY(float _x, float _z) {
return atan2(0-_z,mouseX-_x);
}
}
1