Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Camera Around Sphere

I'm trying to move my camera in a spherical motion around a model in my world. I've seen converting spherical coordinates(rho, theta, phi) to cartesian coordinates (x, y, z), but I'm not sure how to go about setting this up. Here is what I've tried to so far but it isn't continuously orbiting the model. It gets to a certain point and then the rotation seems to reverse itself.

Initialize theta and phi:

private float theta = 0.0f;
private float phi = 0.0f;

Update theta and phi each frame:

// This should move the camera toward the upper-right continuously, correct?
theta = (theta+1.0f)%360;
phi = (phi+1.0f)%360;

Convert theta and phi to cartesian coordinates for the camera:

camera.position.x = CAMERA_DISTANCE * (float)Math.sin(theta*MathHelper.PIOVER180) * (float)Math.cos(phi*MathHelper.PIOVER180);
camera.position.y = CAMERA_DISTANCE * (float)Math.sin(theta*MathHelper.PIOVER180) * (float)Math.sin(phi*MathHelper.PIOVER180);
camera.position.z = CAMERA_DISTANCE * (float)Math.cos(theta*MathHelper.PIOVER180);

Then update the camera look at point and view matrix:

camera.lookAt(0, 0, 0);
camera.update();

Note: I am using Java on Android with the libGDX framework and I am trying to control the rotation using an 2D on-screen virtual joystick and I still need to find a way to map the joystick to theta and phi.

Any help is greatly appreciated!

like image 313
Alex_Hyzer_Kenoyer Avatar asked Dec 25 '12 23:12

Alex_Hyzer_Kenoyer


1 Answers

I recently did something just like this. This website helped me a lot to visualize what I needed to do.

What you need to do is convert your local joystick coordinates (relative to it's center) to pitch and yaw values:

public float getPitch()
{
    return (position.X - center.X) * MathHelper.PIOVER180;
}

public float getYaw()
{
    return (position.Y - center.Y) * MathHelper.PIOVER180;
}

Then you can use a quaternion to represent it's rotation:

public void rotate(float pitch, float yaw, float roll)
{
    newQuat.setEulerAngles(-pitch, -yaw, roll);
    rotationQuat.mulLeft(newQuat);
}

Then you can apply the quaternion to the camera's view matrix using libGDX's built in rotate(quaternion) method:

camera.view.rotate(rotationQuat);

// Remember to set the camera to "look at" your model
camera.lookAt(0, 0, 0);
like image 72
DRiFTy Avatar answered Nov 16 '22 04:11

DRiFTy