1
\$\begingroup\$

I have a sphere with a radius of .5

The sphere is centred at 0,0,0

I am now trying to place items around my sphere based on a given x,y,z on the surface of the sphere.

My items have been draw/created in such a way that they are positioned on the surface when translated to 0,0,0 because their centre offset has been positioned at the centre of the sphere.

What I need is some help with the calculation needed to get the correct glRotate values so that my icons will show at the correct x,y,z on the surface.

To help explain what I mean, here is a picture:

enter image description here

So you can see 0,0,0 at the centre, and also rotating around the same point is the blue line, This represents the rotation of my object. The green part is the visible part of my item, and so will draw on the surface of the sphere.

I have everything ready to go for this, in that I can already draw my items in this manner, however I am not able to correctly calculate the rotation.

With the default rotation of Identity my items will draw on the bottom of the sphere.

In summary I am looking for something like this:

glRotate = GetRotationForSurface(X,Y,Z);

I hope that's clear, this is a bit tricky to explain.

Thanks,

\$\endgroup\$
5
  • 1
    \$\begingroup\$ If you use no rotaton matrix, what 'default' x,y,z coordinates your objects have? \$\endgroup\$ Commented Aug 5, 2018 at 11:19
  • \$\begingroup\$ If you're talking about 3D, you'll need more than 1 value to represent the rotation. You'll either need spherical coordinates, or an axis of rotation and an angle around that axis, or something like that. \$\endgroup\$ Commented Aug 5, 2018 at 16:02
  • \$\begingroup\$ @HolyBlackCat 0,0,0 The object has an offset position so that it can rotate around the centre of the sphere while rendering on the surface. No translation needed. \$\endgroup\$
    – Paul Spain
    Commented Aug 6, 2018 at 0:06
  • \$\begingroup\$ What they meant was what's the 3D position of the point on the surface that the object's visual occupies before it's been rotated? To know how much rotation to apply to reach a particular destination orientation, we need to know what direction we're starting from. Judging by your answer below, it looks like the answer to HolyBlackCat's question is (0, -1, 0) \$\endgroup\$
    – DMGregory
    Commented Aug 6, 2018 at 4:07
  • \$\begingroup\$ @DMGregory correct 0,-1,0 With no rotation the objects draws at the bottom of the sphere. Note that is a normalised vector. Its real position would have been a centre at 0,0,0 and a visual position of 0,-.5,0 \$\endgroup\$
    – Paul Spain
    Commented Aug 6, 2018 at 4:29

1 Answer 1

1
\$\begingroup\$

Got it, thanks to euclideanspace

This is how its done:

//Representing the centre vector of my sphere
V1.X = 0;
V1.Y = -1;
V1.Z = 0;

//Representing where we want to rotate to
V2.X = SurfaceTargetX
V2.Y = SurfaceTargetY
V2.Z = SurfaceTargetZ

//Normalise both
//V1.Normalize(); Already normalised
V2.Normalize();

//Angle
angle = acos(V1.dotProduct(V2));

//Axis
axis = (V1.crossProduct(V2)).normalize();

//OpenGL Rotation (Note the angle needs to be in degrees)
glRotatef(angle * (180.0f / 3.14f), axis.x, axis.y, axis.z);

And your done, only 1 rotation needed.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .