# OpenGL translations and rotations colliding

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

## Recommended Posts

hello as i've been doing openGL for quite a while now, i have come across a snag. i have too many translations and rotations going on in my Render() function, so much that they all get combined. so everything rotates and translates from the top function call. how would i be able to separate each translation and rotation so that they don't end up having one translation collide with another one, i.e:
glTranslatef(2.0f,1.0f,4.0f);
myFunction();
glRotatef(angle,0.0f,0.0f,1.0f);
angle+=0.2f;
myFunction2();


that would make myFunction2(); translate too, even though i only tell it to rotate. how can i fix this? thanks

##### Share on other sites
call glPushMatrix() before you translate, then call glPopMatrix() after the call to myFunction(). That will restore the transformation matrix to what it was before the translation.

glPushMatrix();glTranslatef(2.0f,1.0f,4.0f);myFunction();glPopMatrix();glRotatef(angle,0.0f,0.0f,1.0f);angle+=0.2f;myFunction2();

##### Share on other sites
gah, i should have known that myself. thanks for the help though halcyonX:)

but now i have another problem:( it's hard to explain without the code, so here's the code:
glPushMatrix();glTranslatef(laserX,laserY,0.0f);laserX-=0.05f;if(laserX <= 5.0f){ glTranslatef(laserX,laserY,0.0f); laserY+=0.05f;}if(laserY >= 15.0f){ laserY-=0.5f;}if(laserY <= -5.0f){ laserY+=0.5f;}drillLaser();glPopMatrix();

as you can see, i'm trying to make my object go diagonal across the screen, so that when it reaches a certain height, it goes downwards, and when it reaches a certain height in the -y-axis, that it goes up again. the problem is, after it goes up, it doesn't come back down, it just goes up forever. what would be the problem here?

##### Share on other sites
The way your program is structured, you are continuosly evaluating if your object, lazer, is past a certain point.

The problem with this is that when your object, going up, is past a certain point, it will start moving down BUT once it passes that same point, it will move up again. Thus it will never get past that certain point because your object's location is constantly being evaluated and acted upon.

float yLazer;bool LazerDirection;...if(LazerDirection){    yLazer+=0.5f; //Going up since LazerDirection is TRUE}else{    yLazer-=0.5f; //Going down since LazerDirection is FALSE}if(yLazer > 10.0){    LazerDirection=FALSE;  //FALSE means we will go DOWN}if(yLazer < -10.0){    LazerDirection=TRUE; //TRUE means we will go UP}DrawLazer();

This shows that the object will only change direction when it reaches past a stipulated point, say 10.0. After it passes 10.0 to become 9.5, it will still continue decrementing until it is past 0.0 and then change direction.

##### Share on other sites
thanks a bunch:) now last question, i promise: (just go to that site, i'm not sure how you post images on here:()
[img src = "http://www.geocities.com/team_infern0/concept_art/screenshot.JPG"]now all i want to do is rotate my stick figures legs so that it has a walk-like animation. whenever i rotate it though, it doesn't rotate about the origin, but in a huge circle. how do i make my legs rotate about their origin?

##### Share on other sites
Sounds like you're translating before rotating. Try flipping it around (ie. rotate before you translate).

EDIT:
oh btw, due to the way openGL's matrix multiplication works, the order of the transformations is actually reversed. So right now you're probably calling glRotate before glTranslate. In either case, try flipping it around.

##### Share on other sites
ok, i don't think i'm understanding you correctly. i've already tried translating and then rotating, and vice-versa. i'll show you the code i tried using:
glPushMatrix();glTranslatef(0.0f,0.0f,0.0f);glRotatef(rotateZ,0.0f,0.0f,1.0f);rotateZ+=0.5f;leg1(0.0f,0.0f,0.0f);leg2();glPopMatrix();

and i've also tried:
glPushMatrix();glRotatef(rotateZ,0.0f,0.0f,1.0f);glTranslatef(0.0f,0.0f,0.0f);rotateZ+=0.5f;leg1(0.0f,0.0f,0.0f);leg2();glPopMatrix();

but nothing seems to work, it just spins in a big arc. what do you suggest i do?

thanks again:-)

##### Share on other sites
theres no translation done at all in that code, since you're passing all 0.0f's into ur call to glTranslate. How are you positioning the leg? You must have made a call to glTranslate somewhere with non-zero values.

##### Share on other sites
oh yeah, i have a translation for the leg, and it is working a little better now. it's going upwards instead of downwards, but it's still going in a large arc:-( how do i get rid of the huge arc that it keeps going in?

##### Share on other sites
It sounds like your matrix has a residual translation in it from before the glPushMatrix().

Try something like this:

glPushMatrix();glLoadIdentity();glRotatef(rotateZ,0.0f,0.0f,1.0f);leg1(0.0f,0.0f,0.0f);leg2();glPopMatrix();rotateZ+=0.5f;

That should put the translation back to the origin before the rotation. You should be able to do whatever Scale/Rotate/Translate you need to get the object to world space after this point as well.

1. 1
Rutin
43
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633398
• Total Posts
3011659
• ### Who's Online (See full list)

There are no registered users currently online

×