# OpenGL Opengl Rotate and Translate

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

##### Share on other sites
i dont think the angle is in radians...it is in degrees
cause i made the incremented to 45 and it moved 45 degrees on screen

yes exactly i want it to be a first person shooter kinda thingy but from the top
except tat it is already in motion..i dont have to keep moving forward..
is it possible??

no it is not moving backward..
it is moving awkward...
at first u can rotate any direction u want..
and then u can go forward..and it moves ..but when u turn again..it sorta turns in a trajectory..

u can check out the code

it is at

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

just click on runme.bat and use w,a,d to navigate..
and q to quit

##### Share on other sites
Quote:
 Original post by Darthashwinyes exactly i want it to be a first person shooter kinda thingy

First, your grammar and punctuation sucks. So it's hard to understand you. Fix it.

Secondly, for a FPS style control:
You have stumbled into the same problem of so many before you; Realize this truth: Opengl Will Not handle motion calculations for you; it can only affect the graphics that are rendered, not the ultimate position of your game character.

In order to get the motion controls you ask, you will need to recalculate the positions and rotations yourself as the user input changes. Once you have those, opengl can be used to render the graphics model.

You will need to Learn if you dont already know:
coordinate systems
trigonometry (specifically rotations from sin/cos)
linear algebra -vectors -matrices(optional here but needed for graduating to full 3d)

Using that math you need to develop a separate 'physics' model of your game character from the opengl graphical model.
I recommend going to the Math+Physics forum and asking them "how to make a character walk around like in doom"

##### Share on other sites
Well,I have just posted it in the other section. Hope to get some answers there too.

I badly want this problem solved

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627646
• Total Posts
2978384
• ### Similar Content

• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• Hello all,

On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
#include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }
The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
What is the problem and how to solve it please?

• 10
• 12
• 22
• 13
• 33