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 & HelpOpenGL and 3D Libraries › 3D Camera Rotation in Space (SOLVED!)
Page Index Toggle Pages: 1
3D Camera Rotation in Space (SOLVED!) (Read 2666 times)
3D Camera Rotation in Space (SOLVED!)
May 22nd, 2009, 1:48am
 
What I want is a stationary camera to rotate via the mouse to view stars all around.

To face a star, move the mouse towards it and it comes into center frame.

The problem is, rotating around the X and Y axis does not accomplish this correctly.

How is this code be improved so the star moves in a strait line to the center frame?

Code:
import processing.opengl.*;

final static int R = 1000;
float u, v;
float[][] stars;

void setup()
{
 size(800, 800, OPENGL);
 frameRate(30);
 sphereDetail(1);
 textFont(createFont("Monaco", 14));
 stars = new float[1000][3];
 for(int i = 0; i < stars.length; i++)
 {
   float[] star = { random(-R, R), random(-R, R), random(-R, R) };
   stars[i] = star;
 }
}

void draw()
{
 u += (mouseY - height / 2.0) / height / 20;
 v += (mouseX - width  / 2.0) / width  / 20;
 background(0);
 lights();
 camera(0, 0, 0, 0, 0, R, 0, 1, 0);
 rotateX(u); rotateY(v);
 noStroke();
 for(int i = 0; i < stars.length; i++)
 {
   pushMatrix();
   translate(stars[i][0], stars[i][1], stars[i][2]);
   sphere(5);
   popMatrix();
 }
 camera();
 stroke(255);
 line(width / 2 - 5, height / 2 - 0, width / 2 + 4, height / 2 + 0);
 line(width / 2 - 0, height / 2 - 5, width / 2 + 0, height / 2 + 4);
 text("u: " + int(degrees(u)) + " v: " + int(degrees(v)), 5, 17);
}

void mousePressed() { u = v = 0; }
Re: 3D Camera Rotation
Reply #1 - May 23rd, 2009, 5:55pm
 
After much frustrating effort, I have solved my problem. The trick is to very the camer() upXYZ:

Code:
import processing.opengl.*;

final static int R = 1000;
PMatrix3D cam;
float[][] stars;

void setup()
{
 size(800, 800, OPENGL);
 frameRate(30);
 sphereDetail(1);
 textFont(createFont("Monaco", 14));
 stars = new float[1500][3];
 for(int i = 0; i < stars.length; i++)
 {
   float p = random(-PI, PI);
   float t = asin(random(-R, R) / R);
stars[i] = new float[] {
R * cos(t) * cos(p),
R * cos(t) * sin(p),
R * sin(t)
};
 }
 cam = new PMatrix3D();
}

void draw()
{
 cam.rotateX(-(mouseY - height / 2.0) / height / 20);
 cam.rotateY(-(mouseX - width  / 2.0) / width  / 20);
 PVector x = cam.mult(new PVector(1, 0, 0), new PVector(0, 0, 0));
 PVector y = cam.mult(new PVector(0, 1, 0), new PVector(0, 0, 0));
 PVector d = x.cross(y); d.normalize(); d.mult(R);
 background(0);
 noStroke();
 camera(0, 0, 0, d.x, d.y, d.z, y.x, y.y, y.z);
 for(int i = 0; i < stars.length; i++)
 {
   pushMatrix();
   translate(stars[i][0], stars[i][1], stars[i][2]);
   sphere(5);
   popMatrix();
 }
 camera();
 stroke(255);
 line(width / 2 - 9, height / 2 - 0, width / 2 + 8, height / 2 + 0);
 line(width / 2 - 0, height / 2 - 9, width / 2 + 0, height / 2 + 8);
}
Re: 3D Camera Rotation in Space (SOLVED!)
Reply #2 - May 29th, 2009, 11:11am
 
Ok.. I'm ready to test out the lasers that shoot out on mouse click and blast the stars. Smiley
Re: 3D Camera Rotation in Space (SOLVED!)
Reply #3 - Jun 2nd, 2009, 10:59am
 
You can fire those lasers now:

 gendou.com/asteroids

Enjoy!
Re: 3D Camera Rotation in Space (SOLVED!)
Reply #4 - Jun 2nd, 2009, 1:40pm
 
I would - but Firefox (on OS X) shows me: Error: Start failed: class not found: Asteroids 3D
Page Index Toggle Pages: 1