Advertisement

Moving on the surface of a sphere

Started by February 21, 2003 09:18 AM
3 comments, last by IainC 22 years ago
Hi all, OK, I'm totally lost but will try to express my problem as best I can. Let's say I have a sphere, centered at the origin. Now, I want an object to be able to move on the surface of this sphere. Let's say that as far as the object knows, it's living in a two-dimensional world, and moves only in terms of latitude and longitude (just like us). It may have a heading vector which controls the direction it's moving in (and yes, I'm aware of the problems this will give me around poles), but essentially it's still just thinking in two dimensions. Now, I have my object doing this quite happily and moving around on the surface of the sphere; my problem is in rotating it so that if you draw a line from the top to the bottom of the object and continue it, the line hits the origin (ie, aligning it with the normal to the sphere at any given point). Perhaps a piccy: I understand that the cartesian coordinates of a given point on the sphere, when normalized, will give me the normal of the sphere at that point; what I'm having trouble with is converting this normal to a set of rotations that will align my object properly. In the object's local coordinate system, it sees the x-z plane as the floor, and the y axis as vertical. Now, due to my engine, object translation happens like this: (1) Translate from the world origin to the object's position (2) Rotate the axes (3) Draw the object In C terms using OpenGL, this looks like:
    
glTranslatef (position.x, position.y, position.z);
glRotatef(rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(rotation.y, 0.0f, 1.0f, 0.0f);
glRotatef(rotation.z, 0.0f, 0.0f, 1.0f);
    
Does anyone have any ideas as to how I can derive the correct rotation value to align the y-axis of my object with the normal to the sphere at a given point on the sphere? Many thanks for any help you can offer... www.coldcity.com code, pics, life [edited by - IainC on February 21, 2003 10:20:49 AM]
[size="2"]www.coldcity.com code, art, life
You can easily find the angle between the desired normal and the y axis of your object (call it theta). And the axis of rotation is simply the cross-product of the y axis and the normal. Can''t you use a quaternion or a matrix to find the correct rotation using theta and the axis of rotation?

Cédric
Advertisement
Cédric,

Of course I can

Oh dear, how one can miss the completely obvious sometimes. Head now removed from ass.

Many thanks,
I.
[size="2"]www.coldcity.com code, art, life
Um...

Actually, having said that, I'm lost again.

I need to find the angle between normal (which is the cartesian coodinates of the point on the surface of the sphere, normalized) and the current y axis (which is just (0, 1, 0), yes?) by taking the inverse cosine of the dot product of these two vectors.

OK, got that bit; in my code it looks like this:

****************************
ASEVector3f pos, rot, normal;
planet->GetSurfaceCoordinates(z, phi, &pos); // z and phi here are essentially my current latitude and longitude - this puts a cartesian coordinate on the surface of the sphere into pos
// Find angle between normal and current y-axis
normal = pos;
normal.Normalize();
ASEVector3f yAxis(0, 1.0f, 0);
double angle = acos(normal.DotProduct(yAxis));
****************************

quote:

And the axis of rotation is simply the cross-product of the y axis and the normal.



- OK, no worries...

****************************
// Find axis of rotation
ASEVector3f axisRotation = yAxis.CrossProduct(normal);
****************************

quote:

Can't you use a quaternion or a matrix to find the correct rotation using theta and the axis of rotation?



- No, I don't know how to If I do figure out how to do it with a matrix (off to Google again) will this result in values for rotation.x, rotation.y and rotation.z that I can use when the object comes to set it's position by doing the following?

****************************
// Camera transformations...
glLoadIdentity();
glTranslatef(0, 0, -distance);
glRotated(elevation, 1, 0, 0);
glRotated(azimuth, 0, 0, 1);

// Object transformations...
// (for each object)
glPushMatrix();
glTranslatef (position.x, position.y, position.z);
glRotatef(rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(rotation.y, 0.0f, 1.0f, 0.0f);
glRotatef(rotation.z, 0.0f, 0.0f, 1.0f);
// (draw object)
glPopMatrix();
// (next)
****************************

Hmmm.. Should I be asking in the OpenGL forum instead? Apologies if in the wrong place...

Brgrds,
I.

www.coldcity.com
code, pics, life

[edited by - IainC on February 23, 2003 9:16:39 AM]

[edited by - IainC on February 23, 2003 9:17:51 AM]
[size="2"]www.coldcity.com code, art, life
quote:
Original post by IainC
// Object transformations...
// (for each object)
glPushMatrix();
glTranslatef (position.x, position.y, position.z);
glRotatef(rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(rotation.y, 0.0f, 1.0f, 0.0f);
glRotatef(rotation.z, 0.0f, 0.0f, 1.0f);
// (draw object)
glPopMatrix();
// (next)
****************************

Can''t you write glRotatef(angle, axis.x, axis.y, axis.z)? I don''t know OpenGL, so that''s juste a wild guess.

Otherwise, if you want to split your rotation into three rotations about fixed axes, I don''t know the answer off hand.

quote:
Hmmm.. Should I be asking in the OpenGL forum instead? Apologies if in the wrong place...

Probably... I know that DirectX does allow you to build either a quaternion or a matrix out of an axis and an angle, but I don''t know for OpenGL.

Cédric

This topic is closed to new replies.

Advertisement