Rotating a cube about the x and y axis

Started by
4 comments, last by Godden 19 years, 6 months ago
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
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.

Looking for a serious game project?
www.xgameproject.com
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.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="88" height="70" id="H2lvl" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" /> <param name="quality" value="high" /> <param name="wmode" value="transparent"> <embed src="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" quality="high" bgcolor="#ffffff" width="88" height="70" name="H2lvl" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>
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]

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).

Looking for a serious game project?
www.xgameproject.com
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

This topic is closed to new replies.

Advertisement