PDA

View Full Version : Rotation and translation not working as expected



squirrel
03-30-2006, 09:37 PM
Hello all!
Probably a noob question, but I am stumped on this one. I am trying to recursively create squares. However, their children are not comming out as planned.
C# code:


DoRecurse(1, 4, 1.0, 0.5, 3 );

private static void DoRecurse(int myNum, int numSides, double rad, double scale, int stopLevel)
{
double x, y;
double len=(1+scale)*rad;
double theta=360.0/numSides;

// Are we done yet?
int level = (int)Math.Log( (numSides-1)*myNum, numSides );

// if so, we return and pop off call stack.
if( level > stopLevel )
return;

angle=0.0;
myChildNum=1;

// Draw me
Gl.glCallList( poly );

// Draw my children
for( int i=0; i<numSides; i++ )
{
angle=(i)*theta;
Gl.glPushMatrix();

// Takes degrees. (
x = Math.Cos(angle)*len;
y = Math.Sin(angle)*len;

Gl.glTranslated( x, y, 0.0);
Gl.glRotated( angle, 0.0, 0.0, 1.0);
Gl.glScaled(scale, scale, 1.0);

myChildNum = myNum*numSides - (numSides-2) + 1;
DoRecurse( myChildNum, numSides, rad, scale, stopLevel );
Gl.glPopMatrix();
}

} The x and y values are computed correctly, but each child is "moved" (translated or translated then rotated, I am not sure) a little bit to the one side. What is happening that I am not expecting? :confused:

I would post a screen shot, but I don't have any place to put it.

mikael_aronsson
03-31-2006, 02:53 AM
I guess this could be visual artifact, it all depends on how you have setup your projection and modelview matrix and also that you are doing all the transformations on the modelview matrix.

squirrel
03-31-2006, 08:04 PM
I am pretty sure I am doing all my transformations on the model view matrix. Am I correct in thinking that after glMatrixMode(Gl.GL_MODELVIEW), the current matrix is the model view, unless I run glMatrixMode(Gl.GL_GL_PROJECTION)?

I found a place to post pics: (http://imageshack.us)
This is the way it looks now:
http://img80.imageshack.us/img80/6168/isnow8ps.jpg (http://imageshack.us)

This is the way I want it to look:
http://img81.imageshack.us/img81/6821/needstobe6pt.jpg (http://imageshack.us)

I did the latter all using the C# graphics
library, but it is waaaaay too slow for what
I want to do.

Thanks to all, and long live the internet!

Komat
03-31-2006, 08:47 PM
You are calling the Math.Cos(angle), Math.Sin(angle) functions with angle in degress however they, unlike the glRotate, expect angle in radians.

squirrel
04-01-2006, 10:13 AM
Doh! *whack* *whack* *whack* Yes, that was the problem. Thank you muchly. May the programming gods smile upon you daily.
Thanks again.