# OpenGL Opengl Rotate and Translate

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

## Recommended Posts

I am a beginner in Opengl. any advice would be gratefully accepted. I am programming on this opengl program.It consists of an arrow. and the pivot is the tip of the arrow.. once u hit the 'a' key the arrow rotates to the left with the tip as pivot and when u hit the 'd' key it rotates to the right.. now when i hit the 'w' key the arrow should move in the direction pointed by the arrow. and while moving in that direction when the 'a' or 'd' arrow is hit it should turn around in the respective direction. I have achieved the rotation of the arrow and once the arrow is positioned ,as i press w it moves in that direction. but while moving if i press the 'a' or 'd' key the movement is not as i want to be.. can any of u please help me This is the code void display(void) { glPushMatrix(); glRotatef(it,0,0,-1); glTranslatef(ttx,tty,0); arrow(); glPopMatrix(); glFlush(); glutSwapBuffers(); } int arrow(void) { glBegin(GL_POLYGON); glColor3f( 1.0, 1.0, 1.0 ); glVertex2f( 0.0, 0.0 ); glVertex2f( 0.5, -1.0 ); glVertex2f( 0.2, -1.0 ); glVertex2f( 0.2, -2.0 ); glVertex2f( -0.2, -2.0 ); glVertex2f( -0.2, -1.0 ); glVertex2f( -0.5, -1.0 ); glEnd(); glutSwapBuffers(); return true; } void rotate(int i) { it=it+i*1; glutPostRedisplay(); } void move(void) { ttx=(ttx+.01)*sin(angle); tty=(tty+.01)*cos(angle); glutPostRedisplay(); }

##### Share on other sites
I think you want something more like:

ttx= ttx + (0.1 * sin(angle));tty= tty + (0.1 * cos(angle));

Note I've added parentheses to emphasis the evaluation order. You don't want to modify ttx and tty (your world translation) by the angle every frame, you want to add an offset which is transformed by the angle.

##### Share on other sites
No dude it didnt work..

its like this
i want the arrow ,actually the tip of the arrow to turn...and while moving when i turn the arrow..i want it to move forward in the direction of the arrow tip..

##### Share on other sites
So what actually happens? What are the values in ttx and tty?

##### Share on other sites
well this link has the compiled exe file
run the
runme file and try it ..u can notice the unusual turning of the arrow

http://engr.smu.edu/~avelapan/unzip.zip

##### Share on other sites
If i'm not mistaken...

sin(theta) = opp/hyp

You have y = cos and x = sin which is flipped.

//Edit
Unless of course you are flipping the axis. But either way the code below will work. There is no need to multiply it by .1

This is what you want for it to move in the correct direction.

float ttx = 0;
float tty = 0;

void move()
{
ttx += cos(angle)
tty += sin(angle)
}

This works because you store ttx and tty as variables and you update there position with the normalized angle.

//Edit

If you need me to explain further, please let me know.

##### Share on other sites
nope didnt work..
:(

its moving ackwardly

##### Share on other sites
Do you have an the angle in radian or degree, because cos and sin need angle in radian? I checked your incremetation position (tty a ttx) and I have same code in my project and work it.

##### Share on other sites
I think he's asking for 1st/3rdPersonShooter type free control over the arrow's motion.

Ie, he wants to be able, to, rotate the arrow, move it forward(where its facing), then rotate again from that location, then be able to move it some more from where it's now pointed, etc
-translations and rotations that are not based on the origin...

Anyhow, if thats what you wanted.
Thats complicated...
You'll need to calculate the position and rotations independantly of opengl.
Since opengl Does Not handle incremental transforms over time like that.

how much linear algebra do you know?

##### Share on other sites
If I may ask, what are "it" and "angle"? You are using "it" in your OpenGL rotations, and angle in your movement code - why not simply use one angle value that is used by both (with a conversion between radians and degrees for one or the other, depending on which form you decide to store your angle in)?

Finally, you say:
Quote:
 its moving ackwardly

Do you mean that it moves backwards? In that case, try negating one or both of your movement calculations (e.g. ttx -= 0.1*cos(angle) or tty -= 0.1*sin(angle), or perhaps both.)

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 17
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007470
• ### Who's Online (See full list)

There are no registered users currently online

×