## 3d movement

Member
Posts: 39
Joined: 2008.10
Post: #1
Hey guys,

Im working in 3d for the first time in a long time. Basically im rotating a sphere and projecting x y z coords to place things on the surface based on the spheres X and Y rotation.

Heres the code im using:

Code:
```        #define piover180 0.01745329252f                  GLfloat cosy = cos(yrot * piover180);                  island[i].x = rad * sin(xrot * piover180)* cosy;         island[i].y = rad * sin(yrot * piover180);         island[i].z = rad * cos(xrot * piover180) * cosy;```

Problem is the Xrot positioning works fine but the Yrot placement always draw the objects into the north and south pole so they all cross at the top, which isnt correct for rotating. I need a way to solve this. Heres a picture to help explain

Any help would be greatly appreciated, let me know if you need any more information
Member
Posts: 39
Joined: 2008.10
Post: #2
I have tried to solve the problem of the strange Vertical movement (it always needing to go to the pole as it approaches the top or the bottom of the sphere) by using more simple transform techniques but the problem even persists then. There must be something im not considering since im only very experienced in 2d game development.

Here it is

// Apply latitudinal rotation (aka "xrot")
glRotatef(xrot, 0, 1, 0);

// Apply longitudinal rotation (aka "yrot")
glRotatef(yrot, 0, 0, 1);

// Move object out to its radius

Let me know if you dont understand the problem and ill provide a video
Member
Posts: 39
Joined: 2008.10
Post: #3
I ended up solving it using Spherical Coordinate System

Heres the code

island[i].z = cos(xrot*(PI/180));

Heres the equations

x = r sinq cosf, y = r sinq sinf, z = r cosq,

r = (x2 + y2 + z2)1/2, q =tan-1(z/(x2+y2)1/2), f = tan-1(y/x).

Just in case anyone could do with it, its perfect for camera control or any exact 3d coord calculations you need to do.

Reference

http://electron9.phys.utk.edu/vectors/3dcoordinates.htm