• Create Account

# Controlling Spherical Coordinates With Joystick

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1DiscGolfer17  Members   -  Reputation: 133

Like
0Likes
Like

Posted 13 December 2012 - 04:53 PM

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!

### #2Paradigm Shifter  Crossbones+   -  Reputation: 5832

Like
2Likes
Like

Posted 13 December 2012 - 05:38 PM

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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3DiscGolfer17  Members   -  Reputation: 133

Like
0Likes
Like

Posted 13 December 2012 - 05:48 PM

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.

### #4eppo  Crossbones+   -  Reputation: 4492

Like
1Likes
Like

Posted 14 December 2012 - 05:23 AM

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, 14 December 2012 - 05:24 AM.

### #5DiscGolfer17  Members   -  Reputation: 133

Like
0Likes
Like

Posted 15 December 2012 - 02:49 PM

@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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS