3D reaching all points of the surface of a sphere
in
Programming Questions
•
1 years ago
Hello,
I have a program in 2D (from this forum) and want to make it work 3D.
In 2D I have a circle and when you move the mouse to the left and to the right, the changing
X-value of the mouse draws a line from the circles center to a point on the circumference (a radius
is drawn).
I now want to do this in 3D:
I have an imagined sphere.
Imagine when I move the mouse to the left and to the right and the changing
X-value (phi) of the mouse is the position on the equator of my sphere (seen from above).
I now want the Y-value of the mouse to be the other angle (phiUP) going straight up from the equator. So let say you moved your mouse' X on the equator to central america and now you move the mouse up (Y) you go over California to alasca etc. over the pole all the way back to your starting point.
Anyway, my maths fail me.
I have
- AddZ = factor*cos(phiUP);
Any help appreciated. And, no, I don't want to use quaternions, thanks.
Thank you!
Greetings, Chrisir
// draws a radial line in a circle / sphere
// according to the mouse, X-value for horizontal,
// Y-value vertically.
int mouseX1;
int mouseY1;
int mouseX2;
int mouseY2;
// angles
float phi; // horizontal (x,y)
float phiUP; // vertically UP and Down
float AddX;
float AddY;
float AddZ;
float factor;
float MiddleX;
float MiddleY;
float MiddleZ;
void setup() {
size (600, 600, P3D);
factor=100;
PFont myFont = createFont("FFScala", 32);
textFont(myFont);
MiddleX=width/2;
MiddleY=height/2;
MiddleZ=-22;
}
void draw() {
background (223);
camera(width/2.0+ 200, height/2.0+100, (height/2.0) / tan(PI*60.0 / 360.0)+100,
width/2.0, height/2.0, 0,
0, 1, 0);
showCoordinateAsLines();
// set new values for circle / sphere circumference
AddX = factor*cos(phi);
AddY = factor*sin(phi);
AddZ = factor*cos(phiUP);
// paint lines going from center to circle / sphere circumference
// 2D: to circle circumference
stroke(color(22, 22, 22));
line(MiddleX, MiddleY,
MiddleX + AddX, MiddleY + AddY );
// 3D: to sphere circumference
stroke(color(2, 222, 22));
line(MiddleX+100, MiddleY, MiddleZ,
MiddleX+100 + AddX, MiddleY + AddY, MiddleZ + AddZ );
showValuesXYZSeparately();
}
// =======================================================
// helping functions
void mouseMoved() {
mouseX2 = mouseX;
mouseY2 = mouseY;
if ((mouseX2 - mouseX1) > 0) {
phi = phi + .174;
}
else if ((mouseX2 - mouseX1) < 0) {
phi = phi - .174;
}
else {
// do nothing
}
if (mouseY2 - mouseY1 > 0) {
phiUP = phiUP + .174;
}
else if (mouseY2 - mouseY1 < 0) {
phiUP = phiUP - .174;
}
else {
// do nothing
}
mouseX1 = mouseX2;
mouseY1 = mouseY2;
} // function
void showCoordinateAsLines() {
// show Z-Koor
stroke(color(222, 22, 22));
line(MiddleX, MiddleY, MiddleZ,
MiddleX, MiddleY, MiddleZ -200 );
line(MiddleX, MiddleY, MiddleZ,
MiddleX, MiddleY, MiddleZ +200 );
// and Y
// stroke(color(222, 122, 222));
line(MiddleX, MiddleY, MiddleZ,
MiddleX, MiddleY+200, MiddleZ );
line(MiddleX, MiddleY, MiddleZ,
MiddleX, MiddleY-200, MiddleZ );
// and X
line(MiddleX, MiddleY, MiddleZ,
MiddleX+200, MiddleY, MiddleZ );
line(MiddleX, MiddleY, MiddleZ,
MiddleX-200, MiddleY, MiddleZ );
} // function
void showValuesXYZSeparately() {
rect ( AddX, 11, 10, 10 );
text ( "X", AddX-22, 11 );
rect ( 11, 200 + AddY, 10, 10 );
text ( "Y", 19, 200 + AddY );
rect ( width-11, 100 + AddZ, 10, 10 );
text ( "Z", width-19, 100 + AddZ );
} // function
1