Jump to content
  • Advertisement
Sign in to follow this  
Darthashwin

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
If i'm not mistaken...


cos(theta) = adj/hyp

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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!