How rotate cube around a sphere surface

593 views Asked by At

I want to put cubes around the sphere surface like:

cube arround sphere

I'm using Java3D. I have a point of the sphere surface with:

    double s = (random.nextInt(360));
    double t = (random.nextInt(360));
    double radius = 180;
    double x = radius * Math.cos(s) * Math.sin(t);
    double y = radius * Math.sin(s) * Math.sin(t);
    double z = radius * Math.cos(t);

And I need put center of cube in this point and translate and rotate cubes like the figure. How can I do this?

I try rotate with s and t in different axes. But there is my result:

enter image description here

enter image description here

I need that the face of cube and the sphere surface are perpendicular like first image.

1

There are 1 answers

3
Anonymous On

Simplest solution would be to do it in a sequence of 3 calls:

// having s,t,radius
// transform cube
GL11.glRotatef(s, 0, 0, 1);
GL11.glRotatef(t, 1, 0, 0);
GL11.glTranslatef(0, 0, radius);

If you need to calculate such matrix for use by your program, you can follow glRotate & glTranslate specifications how do they modify current matrix and replicate their behaviour. Alternatively you can query GL matrix after these operations using glGetDoublev(GL_MODELVIEW_MATRIX);