# rotating a sphere around its own axis

Hi,

I'm making a small 3d renderer for fun. I just added a sphere primitive. I applied a rotation transform to make it appear as if it's sitting at a 30 degree angle (like our planet, kind of). I'd like to just keep rotating it around its axis now every frame of animatin, but it appears that when I am applying my transform of 3 degrees around the y axis, it's doing it around the global origin - so it looks like the axis of the sphere is actually pointing in different directions while rotating.

I'm not sure what the best solution is - should I implement a rotateSelf() function which applies a transform about the object's current origin? Or is there some other way?

Thanks
###### Who is Participating?

Commented:
hi dj,

> I'm not sure what the best solution is - should I implement a rotateSelf() function which applies a transform about the object's current origin?

you can do that, but one essence of 3d-programming is to learn how to use matrices, to do your desired transformations..

here is how you can do it, you need 3 matrices:

M_30_deg -> 30 degrees rotation around axis z (as if it's sitting at a 30 degree angle (like our planet, kind of)
M_mod -> modulated rotation around axis y (for rotating around its own axis)

now multiply both matrices in this order each frame:

M_current_frame = M_30_deg * M_mod

and multiply that matrix with your object's vertices, that's it .. :)

the rotateSelf()-function would multiply the M_mod-matrix with the object's-vertices first and after that you would multiply the M_30_deg-matrix with the object's vertices.. that works as well, but wouldn't be so nice, since you have a double amount matrix-vertex-multiplications.
rather than the rotateSelf()-function i'd recommend to put a applyMatrix()-function to your object and create the matrix-transform-chain first with matrices and multiply them, to get the final matrix and at the end apply the final matrix to your object ..

ike
0

Commented:
so rather than the rotateSelf-function, create a

matrix createRotationMatrixAxisX( float angle )

and the other axes .. but i guess you already have that, right? :)

of course you can create the rotateSelf() func as well, but it will hide the important part, the matrix-transform-chain ..
0

Commented:
x=sin(pi*30/180), y=cos(pi*30/180)   Unit vector at 30 degrees from y axis
then a rotation by 3 degrees around that axis is a multiply by

1+(1-cos(pi*3/180))*(x*x-1)     (1-cos(pi*3/180))*x*y                 y*sin(pi*3/180)
(1-cos(pi*3/180))*x*y               1+(1-cos(pi*3/180))*(y*y-1)     -x*sin(pi*3/180)
-y*sin(pi*3/180)z                      x*sin(pi*3/180)                            cos(pi*3/180))
0

Author Commented:
@ikework:

I may have implemented my library incorrectly. When I apply a transformation to the vertices of my object, the vertices get transformed ok, that's not a problem. But the vertices are - well - transformed now to some new location.

So if on the subsequent animation frame I try applying the tilt matrix AGAIN, it will keep making the vertices tilt even more. Instead of keeping a constant 30 degree tilt on the z-axis, it will appear to rotate by 30 degrees around the z-axis with every animation frame!

Should I implement this somewhat differently?

Thanks

0

Commented:
> When I apply a transformation to the vertices of my object, the vertices get transformed ok, that's not a problem. But the vertices are - well - transformed now to some new location.
A rotation will always transform vertices to some new location, unless a vertex is on the axis of rotation or the rotation is by a multiple of 2pi

> So if on the subsequent animation frame I try applying the tilt matrix AGAIN,
Don't you want to apply the rotation matrix again, not the tilt matrix again?
The tilt matrix cab be used to derive the proper rotation matrix.

0

Author Commented:
going off of my last point - I mean do I just really let the object's vertices be altered by its transformation matrix - or should I keep the original vertices unchanged when the object is first created, and only get the transformed vertices when I need to draw them -- the two options look like:

Option 1:
create object with vertices centered around world origin
apply matrix, vertex locations are modified
draw to screen using modified vertex locations

Option 2:
create object with vertices centered around world origin
apply matrix, vertex locations are NOT modified
draw to screen by constantly applying internal transformation matrix to unmodified original points

which is the right way to go?

0

Author Commented:
@ozo:

Yeah I just want to apply the rotation matrix over and over again - but I don't understand this:

1+(1-cos(pi*3/180))*(x*x-1)     (1-cos(pi*3/180))*x*y                 y*sin(pi*3/180)
(1-cos(pi*3/180))*x*y               1+(1-cos(pi*3/180))*(y*y-1)     -x*sin(pi*3/180)
-y*sin(pi*3/180)z                      x*sin(pi*3/180)                            cos(pi*3/180))

do you mean I should create a new empty matrix and fill each row/col with those values then apply it to my sphere?

Thanks
0

Commented:
those are the coefficients of a 3×3 rotation matrix.
if you multply it by the coordinates of a vertex, the result will be the coordinates
after a rotation by 3 degrees around an axis defined by the unit vector in the (x,y,0) direction
0

Commented:
> which is the right way to go?

neither 1 nor 2 :)

> create object with vertices centered around world origin

create the points of the object centered around its own center of mass .. if you have an airplane, it should roll/pitch around its own center of mass, its own {0,0,0} point or origin. keep those points during all the program, dont change them. rather in the begin each frame copy them to another buffer of the same size (so each object should hold 2 buffers of points, the original and the transformed in each frame). in each frame compute the movements of the airplane, its roll/pitch and stuff, apply it to the second buffer and apply its position to that buffer and render it at the end of the frame ..

the same answer, like in your other q: http://www.experts-exchange.com/Programming/Game/3D_Prog./Q_22865584.html

ike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.