# Translating on a spherical surface

This topic is 2985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey. I 'm trying to translate a model on a spherical surface (a planet), I need it to move around the sphere up/down and right/left (I don't need to freely set the orientation, only four directions) (Y is up, X is right) I 'm trying doing it in Spherical coordinates and then converting to Cartesian, but it doesn't work: my theta angle is originally 0 degrees, phi is 90. radius is the distance from the planet origin to the model.
       double thetaSin = Math.sin(Math.toRadians(theta));

//Translating based on the current direction (variable dir)
//Up:
theta  += turnspeed;
if (theta >= 360) {
theta = 0;

}
//Right:
theta += turnspeed;
if (theta >= 360) {
theta = 0;

}
phi  -= turnspeed;
if (phi < -180) {
phi = 180;

}
...................... left and down directions.



x = radius * (float) thetaSin*(float)phiCos;
y = radius * (float) thetaSin * (float)phiSin;
z = radius * (float) thetaCos;

model.setTranslation(x, y, z);


I mostly have trouble correctly changing theta/phi while moving right/left, but I'm generally not sure if I'm doing this right. Can you guys help me?

##### Share on other sites
You can do this in spherical coordinates using the geodesic equations, but I wouldn't really recommend it.

Rather, given a coordinate frame on the sphere, think about how you need to rotate it about the center of the sphere. The transformation will leave the left vector the same, change the up vector to whatever the sphere normal will be at the new location, and set the forward vector to the cross product of these other two. Is this enough for you to figure out how to build the appropriate transformation matrix?

If you need more help I can post again in more detail later.

##### Share on other sites
Every motion on a sphere can be represented, as Emergent already suggested, by a rotation around its center. In the following, p = (P - C) will be the position vector of the model on the sphere and v the direction vector. There is no need to restrict this considerations to the four directions you are considering. I will also use the angular velocity ω and the time t.

The best way to represent a rotation in 3D in this case is to use the axis-angle representation. Indeed, the axis is simply the unit vector u = (p x v) / ||p x v|| (where x is the cross product and || || is the norm), and the angle is θ = ω t. To rotate the model you can now choose to build a matrix or use quaternions.

EDIT: I corrected a typo

[Edited by - apatriarca on December 24, 2009 8:18:00 AM]

##### Share on other sites
yeah, I understand. thank you.