Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# rotating a sphere around its own axis

Posted on 2007-09-30
Medium Priority
442 Views
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
0
Question by:DJ_AM_Juicebox
• 3
• 3
• 3

LVL 20

Accepted Solution

ikework earned 2000 total points
ID: 19989024
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

LVL 20

Expert Comment

ID: 19989061
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

LVL 85

Expert Comment

ID: 19989714
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 Comment

ID: 19994946
@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

LVL 85

Expert Comment

ID: 19995008
> 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 Comment

ID: 19995075
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 Comment

ID: 19995121
@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

LVL 85

Expert Comment

ID: 19995931
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

LVL 20

Expert Comment

ID: 20005455
> 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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  Iâ€™ll start with the algorâ€¦
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I wasâ€¦
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery procedâ€¦
###### Suggested Courses
Course of the Month13 days, 7 hours left to enroll

#### 579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.