# Square is not moving...

**URL:** <https://discourse.glfw.org/t/square-is-not-moving/1486>\
**Category:** support\
**Created:** [February 6, 2020, 8:54am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486 "2020-02-06T08:54:19Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinDrawins](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@VinDrawins](https://discourse.glfw.org/u/VinDrawins)\
**Post date:** [February 6, 2020, 8:54am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/1 "2020-02-06T08:54:19Z")

</div>

I’m trying move square, but it’s not happening. I’m just started using “glm”. So my doubt is **how to move object using “glm”?**. Or if you not able to understand following code, then send code for this using “glfw” & “glm” .Here is code:

```
#include <GLFW/glfw3.h>

#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>

#define SCREEN_WIDTH 500
#define SCREEN_HEIGHT 500

#include<iostream>
using namespace std;

int posx = 0;

void Coordinate_System(void)
{
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-100, 100, -100, 100, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Process_Input(GLFWwindow* window)  
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true); 
}

int main(void)
{
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    float vertices[] = {
        0,0,0,
        10,0,0,
        10,10,0,
        0,10,0
    };

    Coordinate_System();

    while (!glfwWindowShouldClose(window))
    {
        Process_Input(window);

        glClearColor(0.2, 0.3, 0.3, 0.1);
        glClear(GL_COLOR_BUFFER_BIT);

        glEnableClientState(GL_VERTEX_ARRAY);   
        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);
        glm::mat4 trans = glm::mat4(1.0f);
        trans = glm::translate(trans, glm::vec3(1.0f, 100.0f, 0.0f));
        vec = trans * vec;
        cout << vec.x << " " << vec.y << " " << vec.z << endl;

        glDrawArrays(GL_POLYGON, 0, 4);
        glDisableClientState(GL_VERTEX_ARRAY);
    
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
  }
```

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 6, 2020, 11:19am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/2 "2020-02-06T11:19:59Z")

</div>

You need to update the GL\_MODELVIEW matrix before rendering. Note that this is OpenGL, not GLFW - GLFW handles the creation of windows, mouse and keyboard feedback etc. and has functions named `glfw*` whereas OpenGL has functions named `gl*`.

You could do that with [glTranslatef](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml) and your translation or create the translation matrix and call [glLoadMatrix](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glLoadMatrix.xml).

You will also need to alter the translation every frame to get some movement, otherwise you will just be setting the coordinates.

I don’t know **glm** , but looking online I found [this article on glm](https://glm.g-truc.net/0.9.2/api/a00001.html) and so I think you would need to use something like:

```auto
glm::mat4 trans = glm::mat4(1.0f);
trans = glm::translate(trans, glm::vec3(1.0f, 100.0f, 0.0f));
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf( glm::value_ptr( trans ) );

```

Note this will place the square, but if you want it to keep moving you need to change the value of the translation vector every frame.

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [February 14, 2020, 3:16pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/3 "2020-02-14T15:16:31Z")

</div>

Dear Doug Binks,  
Please accept my many thanks for your message. I’m beginner in graphics programming. Snippet you suggests throws error “‘glLoadMatrix’: identifier not found”.  
According to your link [glLoadMatrix](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glLoadMatrix.xml) two options are legal: glLoadMatrixd and glLoadMatrixf. Second works in my program. Regards.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 14, 2020, 3:40pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/4 "2020-02-14T15:40:52Z")

</div>

Many thanks for the correction, I’ve fixed the code snippet now.

Good luck in your programming journey!

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [February 14, 2020, 3:52pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/5 "2020-02-14T15:52:53Z")

</div>

Dear Doug Binks,  
Please accept my many thanks for you replied and that immediately.  
By the way let me ask you how I can “change the value of the translation vector every frame”. Program I work is yours at [A very basic issue with draw line](https://discourse.glfw.org/t/a-very-basic-issue-with-draw-line/804).  
Regards.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 14, 2020, 4:41pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/6 "2020-02-14T16:41:36Z")

</div>

In the case of the draw lines program rather than changing the `GL_MODELVIEW` matrix it is simplest to just change the line coordinates.

So we change:

```auto
void render_loop()
{
	glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPointSize(10.0f);
	glLineWidth(2.5f); 
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_LINES);
	glVertex3f(10.0f,10.0f,0.0f);
	glVertex3f(200.0f,200.0f,0.0f);
	glEnd();
}

```

to something like:

```auto
void render_loop( float x, float y, float z)
{
	glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPointSize(10.0f);
	glLineWidth(2.5f); 
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_LINES);
	glVertex3f(10.0f+x,10.0f+y,0.0f+z);
	glVertex3f(200.0f+x,200.0f+y,0.0f+z);
	glEnd();
}

```

and then in your main function you change the main loop from:

```auto
	// Main loop
	while( !glfwWindowShouldClose(window) )
	{
		// Draw gears
		render_loop();

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

```

to:

```auto
	// Main loop
	float x = 0.0f, y=0.0f,z=0.0f;
	float speed = 10.0f;
	double time = glfwGetTime();
	while( !glfwWindowShouldClose(window) )
	{
		// Update time and position
		double timeNow = glfwGetTime();
		double deltaTime = timeNow - time;
		x = x + speed * deltaTime;
		if( x > 400.0f ) // move back to beginning once out of window
		{
		    x = 0.0f;
		}

		// Draw
		render_loop(x,y,z);

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

```

Note that I’ve not compiled or tested this code, so it may need changes.

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [February 17, 2020, 9:03am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/7 "2020-02-17T09:03:59Z")

</div>

Dear Teacher,  
Please let me express my sincere gratitude for your instructive answer, and my sorrow for delay in response. I have to learn a lot from you.  
Regards.

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [February 17, 2020, 9:52am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/8 "2020-02-17T09:52:30Z")

</div>

Dear Teacher,  
Please let me say that Visual Studio 2019 compiler throws some warnings for double and float, and one error:  
‘time’: undeclared identifier  
I added  
double time{ 0 }; above  
double deltaTime = timeNow - time; and program works.  
I’m interested for a horizontal line moving to left with stable speed and never return, though. I have modified your snippets in below program. My question now is how many pixels per second correspond to speed = 1.0? Regards.

```
#include <GLFW/glfw3.h>
#include <gl/GL.h>
#include <iostream>

void render_loop(float x)
{
float a1{ 0.2 }, b1{ 0.2 }, c1{ 1.0 };
float a2{ 1.0 }, b2{ 1.0 }, c2{ 0.2 };

glClearColor(a1, b1, c1, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(10);
glLineWidth(2);
glColor3f(a2, b2, c2);
glBegin(GL_LINES);
glVertex3f(800.0f+x, 700.0f, 0.0f);
glVertex3f(1000.0f+x, 700.0f, 0.0);
glEnd();
}

/* program entry */
int main(int argc, char* argv[])
{
GLFWwindow* window;

if (!glfwInit())
{
	std::cout << "Failed to initialize GLFW\n";
	exit(EXIT_FAILURE);
}

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(1200, 800, "LearnOpenGL", NULL, NULL);
if (!window)
{
	std::cout << "Failed to open GLFW window\n";
	glfwTerminate();
	exit(EXIT_FAILURE);
}

glfwMakeContextCurrent(window);
glfwSwapInterval(1);

// set up view
glViewport(0, 0, 1200, 800);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// see https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml
glOrtho(0.0, 800.0, 0.0, 800.0, 0.0, 1.0); // this creates a canvas you can do 2D drawing on

// Main loop
float x = 0.0f, y = 0.0f, z = 0.0f;
float speed = 10.0f;
while (!glfwWindowShouldClose(window))
{
	//Update time and position
	//double timeNow = glfwGetTime();
	//double time{ 0 };
	//double deltaTime = timeNow - time;
	x = x - speed;// *deltaTime;

	// Draw gears
	render_loop(x);

	// Swap buffers
	glfwSwapBuffers(window);
	glfwPollEvents();
}

// Terminate GLFW
glfwTerminate();

// Exit program
exit(EXIT_SUCCESS);
}
```

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 17, 2020, 5:36pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/9 "2020-02-17T17:36:33Z")

</div>

I’m afraid I don’t have the time to help with general math and programming questions not related to using GLFW, but I think the corrected code (again completely untested) is:

```auto
	// Main loop
	float x = 0.0f, y=0.0f,z=0.0f;
	float speed = 10.0f;
	double time = glfwGetTime();
	while( !glfwWindowShouldClose(window) )
	{
		// Update time and position
		double timeNow = glfwGetTime();
		float deltaTime = static_cast<float>( timeNow - time );
                time = timeNow;
		x = x + speed * deltaTime;
		if( x > 400.0f ) // move back to beginning once out of window
		{
		    x = 0.0f;
		}

		// Draw
		render_loop(x,y,z);

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

```

Good luck with getting this working from here!

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [February 24, 2020, 12:48pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/10 "2020-02-24T12:48:25Z")

</div>

Mr. Doug Binks,  
Please let me the question. In snippets you suggest me parameters in  
**glVertex3f(200.0f+x,200.0f+y,0.0f+z);**  
are floats, so do not represent pixels. Then what do they represent?  
Thanks in advance. Regards.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [February 24, 2020, 1:07pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/11 "2020-02-24T13:07:38Z")

</div>

These represent the x,y,z coordinates of a 3d vertex position which is then transformed by the OpenGL matrix stack and viewport to result in a screen position.

I suggest you try searching for OpenGL tutorials to find some which will guide you. At the moment you are using what is called ‘fixed function OpenGL’, and modern graphics are drawn using the programmable pipeline with shaders. If you find the later too complex try searching for ‘fixed function OpenGL tutorials’.

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [February 25, 2020, 8:01am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/12 "2020-02-25T08:01:41Z")

</div>

Mr. Doug Binks,  
Please accept my many thanks for you answered my question and many more for your instructive suggestions.  
Regards.

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [March 13, 2020, 3:26pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/13 "2020-03-13T15:26:50Z")

</div>

Dear Doug Binks,  
Please let me say you that I can’t find tutorials for ‘fixed function OpenGL’. I’m interested in 2D graphics and vertex’s parameters represent pixels, not numbers betwwen -1 and 1. Then do you suggest me 2D tutorials or 3D and me apply 0.0 as third dimension? Also do you suggest me GLAD?  
Regards.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [March 13, 2020, 6:04pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/14 "2020-03-13T18:04:44Z")

</div>

If you are interested in only 2D graphics then you might find using a 2D library like [NanoVg](https://github.com/memononen/nanovg) or [dear imgui](https://github.com/ocornut/imgui).

If you want to learn OpenGL then perhaps [https://learnopengl.com/Introduction](https://learnopengl.com/Introduction) is a good start, even though it uses shaders.

Good luck - note that this forum is for GLFW rather than OpenGL and you might find better help on other forums.

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [March 14, 2020, 8:06am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/15 "2020-03-14T08:06:54Z")

</div>

Dear Doug Binks,  
Please accept my thanks for you replied and many more for your suggestion.  
Regards

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [November 7, 2020, 8:31am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/16 "2020-11-07T08:31:51Z")

</div>

Dear Doug Binks,

Could you please suggest me a website for learn NanoVg?

With regards and friendship  
Georges Theodosiou

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [November 7, 2020, 3:28pm UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/17 "2020-11-07T15:28:11Z")

</div>

Sadly I don’t know any, but there is a demo as part of the code on Github.

---

<div class="post-metadata">

**Author:** ![Chretienorthodox](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/chretienorthodox/32/311_2.png) [@Chretienorthodox](https://discourse.glfw.org/u/Chretienorthodox)\
**Post date:** [November 10, 2020, 8:09am UTC](https://discourse.glfw.org/t/square-is-not-moving/1486/18 "2020-11-10T08:09:41Z")

</div>

Mr Doug Binks,  
Please accept my thanks for you responded my message. It’s helpful though.  
With regards and friendship,  
Georges Theodosiouy
