# Rotating a cube about the x and y axis

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

## 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 on other sites
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 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 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 on other sites
Quote:
 Original post by baddogjbefore 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 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

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×