Controlling Spherical Coordinates With Joystick

Recommended Posts

DiscGolfer17    133
I am working on a 3D game in OpenGL on Android using the libGDX framework. I have a sphere drawn at the origin and I am working on getting the camera to orbit the sphere freely on any axis. I would like to control the camera with a virtual joystick that I have being drawn in the bottom left corner. I have started to attack this using spherical coordinates and converting them to cartesian coordinates. My problem is how can I control the spherical coordinates of theta (angle between x axis and ray from the camera position to the origin), and phi (angle from the z axis to the same ray)... I've also noticed that as the camera is orbiting the sphere, after the angles hit 45 degrees the direction changes by itself and starts to loop around the other way (note: I am currently getting movement by just incrementing theta and phi at the same time by 1 degree up to 360 and then back around). Thanks for any help at all on this!

Share on other sites
Use quaternions instead, they don't suffer from singularities at the poles of the sphere. Or you could extract the axes from the look at matrix and do cross products etc. to build an orthogonal axis set, but quaternions are easier.

Share on other sites
DiscGolfer17    133
I've thought about trying to use quaternions but they seemed a little difficult to understand how to use... How would I go about setting it up, and applying it to my camera? I just noticed the the framework I'm using (libGdx) has a Quaternion class. I'm just not quite sure how to begin using them. Though I've read that this is the way to go several times.

Share on other sites
eppo    4877
You need a way to map the x and y offsets of your virtual joystick to a yaw and pitch offset based on the camera's current orientation.

First convert the quaternion to a matrix and extract its x (pitch-axis) and y (yaw-axis) vectors. These two are the camera's orientation-axes transformed from camera view space to world space. Then simply add two additional rotations to the orientation(-quaternion). e.g.:

orientation *= quaternionRotationAroundAxis(yaw-axis, joystick.x);
orientation *= quaternionRotationAroundAxis(pitch-axis, joystick.y); Edited by eppo

Share on other sites
DiscGolfer17    133
@eppo

I have tried and tried to get this to work but I can't seem to get it to do anything! It's not responding to my Quaternion at all... Can you explain to me the correct way to get the x and y axes out of the matrix returned from my quaternion?

I can't seem to get the quaternion to be applied to my camera in any way. If I could just get it to simply rotate around the x or y axis to start that would at least be progress.

Create an account

Register a new account