Jump to content
  • Advertisement
Sign in to follow this  
Godden

Rotating a cube about the x and y axis

This topic is 5047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I have a cube made up of 6 sides of vectors. I need to be able to rotate the cube around the x and y axis, (im using openGL). How could i go about this

Share this post


Link to post
Share on other sites
Advertisement
Simply rotate the 6 points that define the cube around the center of the cube. Do this by first creating a rotation matrix, and then multiplying each point by that rotation matrix (Note that this works only for rotating around the origin, so you may need to translate to the origin, rotate, and translate back depending on your setup). The problem is, if you constantly rotate the same 6 points, floating point precision errors will accumulate and your cube will end up distorted.

On a side note, when defining a cube, I prefer to store it with parameters. Like a position, a size (half the width), and rotation angles. That way, when I need to render it, I just define points from the size, rotate them according to the rotation angles, and then translate it to its position. This way, you avoid re-rotating everything all the time and losing precision.

Share this post


Link to post
Share on other sites
before your glBegin() code, type the function glRotatef(rotspeed,xrot,yrot,zrot);

rotspeed is how fast the rotation is. you can use a variable or number. xrot, yrot, and zrot, are the rtoation directions. if you wanted the cube to rotate in the x direction you put 1.0f for xrot. you can have the cube rotating in two directions as well.

Share this post


Link to post
Share on other sites
I am currently using glRoate the only problem is with collision detection later on the rotation the glrotate doesnt work on it.


the way i have my cube is by using a structure (a vector) that has the x,y,x points in it. so i have say cubeSide1[3]
thatll have values like cubeSide1[0] = [1,1,1]
cubeSide1[1] = [-1,1,1]
cubeSide1[2] = [-1,-1,1]
cubeSide1[3] = [1,-1,1]

Share this post


Link to post
Share on other sites
Quote:
Original post by baddogj
before your glBegin() code, type the function glRotatef(rotspeed,xrot,yrot,zrot);

rotspeed is how fast the rotation is. you can use a variable or number. xrot, yrot, and zrot, are the rtoation directions. if you wanted the cube to rotate in the x direction you put 1.0f for xrot. you can have the cube rotating in two directions as well.


You can always use that "solution", but this will only rotate the cube visually. If you wanted to perform collision detection on it, you'd have to rotate it yourself. Because OpenGL only rotates the cube in your videocard's memory. Just go with transformation matrices like I suggested if you want collision detection.

Just to clarify some more: OpenGL is a rendering API. If you want to actually rotate something, there are probably ways to retrieve the operations OpenGL performs on your data, but that is not an efficient solution (plus you won't learn much).

Share this post


Link to post
Share on other sites
thanks for the help i understand how it works now i just need to find out how i can multiply these vectors together in C++. I thought that that glrotate only moved the camera :D. thanks again

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!