Red Richardson
Red Richardson

Reputation: 29

Orienting a camera that orbits spheres in JOGL2

I'm modelling a simple solar system in jogl and the camera isn't able to look at a specific planet (Earth). I tweaked a lot of code and managed to get the camera to orbit around the planet, but when I try to get the camera to look at the planet (rotate on its own y axis towards the planet), the simulation breaks and the earth (and camera) appear to be in the center of the sun.

This is the code that I can get to work. Also yes I am aware that I am using an outdated JOGL; it works for my purposes.

        //Rotate and place the camera at it's position
        //dt = angle between camera's lookat direction and the planet.
        //The camera can't rotate but it can orbit. It needs to do both
//      gl.glRotatef(dt, 0.0f, 1.0f, 0.0f);
        gl.glTranslatef(-camX, -camY, -camZ);
        
        //draw the planets
        for(Planet p : this.artbook) {
            setColor(p);
            gl.glPushMatrix();
                
                //Go to planet's position in orbit and rotate on its y axis
                gl.glTranslatef(p.getX(), 0.0f, p.getZ());
                gl.glRotatef(p.getRotation(), 0.0f, 1.0f, 0.0f);
                
                //draw the planet
                glu.gluSphere(quad, p.getRadius(), 10, 10);=
            gl.glPopMatrix();
        }

Basically, the camera needs to be able to orbit earth (or any planet) and rotate on it's y axis once in orbit to look at the planet. The rest of the solar system must appear to rotate normally from the camera. I think the problem lies in the order of rotate, translate, and push/popping that I'm doing here but I can't figure out the right order. I also double checked the direction of orbiting and rotation to make sure I'm being consistent

Upvotes: 1

Views: 16

Answers (0)

Related Questions