Advertisement

Continous movement for square

Started by February 18, 2016 06:59 AM
5 comments, last by TheStudent111 8 years, 10 months ago

How does one go about implementing continuous movement with OpenGL, using something like SFML

So far I have managed to make my square object translate by 0.1. The square can translate to the left and to the right, but it can not move in either direction continuously, only once per key press. How can I make it so that the square can move continuously in one direction, if the user presses either the left or right key. Is a model matrix required? (Pretty new at this, so I do not know when one should use a model matrix)

C++ Code:


    GLint trans_location = shader.getUniformLocation();

    glm::vec3 Velocity;

    glm::mat4x4 Identity(1.0f);



    //Necessary Buffers are created

Main Loop:


        shader.Use();



        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))

        {

            Velocity = glm::vec3(0.1f, 0.0f, 0.0f);

            glm::mat4 trans_matrix = glm::translate(Identity, Velocity);

            glUniformMatrix4fv(trans_location, 1, GL_FALSE, glm::value_ptr(trans_matrix));

        }

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))

        {

            Velocity = glm::vec3(-0.1f, 0.0f, 0.0f);

            glm::mat4 trans_matrix = glm::translate(Identity, Velocity);

            glUniformMatrix4fv(trans_location, 1, GL_FALSE, glm::value_ptr(trans_matrix));

        }



        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

 

GLSL Vertex Shader:


#version 400 core



layout (location = 0) in vec4 position;

layout (location = 1) in vec4 color;



out vec4 out_color;



uniform mat4 trans_matrix;



void main()

{



    out_color = color;

    gl_Position =  trans_matrix * position ;

}

 

You just need some very basic physics.


float xPosition = 0;
float xVelocity = 0.1;

while(true)
{
    if(keypressed(right))
        xPosition += xVelocity;
    if(keyressed(left))
        xPosition -= xVelocity;
    // Build transformation
    Matrix translate;
    translate.LoadTranslation(xPosition, 0);
    // Set uniform value
    // Render
}

That's the general idea at least. Later you would need to account for frame rate (faster frame rate means faster speed).

Each frame you just add velocity onto position and then you translate by position rather than velocity.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Advertisement

Thanks for the help, Nanoha.

One additional question, when I apply a translation over the z axis, my square just disappears. I originally thought that the square would slowly be translating away from me if I pressed the left key, then towards me if I apply the right key. But the output that I am actually getting is a square that just magically disappears when either key is pressed. How would one remedy this? Is there a command that activates the z axis functionality?

    GLint trans_location = shader.getUniformLocation();
    glm::vec3 Velocity = glm::vec3(0.1f, 0.0f, 0.0f);
    glm::mat4 trans_matrix;
    float zPosition = 0.0f;
    float zVelocity = 1.0f;

Main loop:

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            zPosition += zVelocity * dt.asSeconds();
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            zPosition -= zVelocity * dt.asSeconds();
 
        Velocity = glm::vec3(0.0f, 0.0f, zPosition);
        trans_matrix = glm::translate(Identity, Velocity);
        glUniformMatrix4fv(trans_location, 1, GL_FALSE, glm::value_ptr(trans_matrix));
If something disappears when moving on the Z axis, the first thing I would check is the near and far clipping planes.

If you aren't using a projection matrix at all, I believe Z values need be between 0 and 1.

Currently, not using a projection matrix. My values (zPosition), are fluctuating between -1 0 1 when I press the left and right keys. However, the square does not move. The square does disappear when the value (zPosition) is over -1 and 1.

Pretty new with 3D graphics, so I would have to look into checking the near and far planes. The only thing that comes to mind involving these two planes is that they are part of the frustum, which dictates what can be seen by the user. Anything that is outside of the frustum is not rendered on screen.

Correct. They're exactly what they sound like in that context.

The reason your square appears not to move when you adjust the z coord is that you don't have a projection set up, so you're probably still using the orthographic projection from SFML. If you set up a projection matrix then the object should appear to move nearer or farther as you adjust the z. Setting a projection also involves setting the near and far planes, so it should give you more room to work in as well.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement

@Khatharr

Thanks, managed to set up a projection matrix. Everything is working perfectly.

This topic is closed to new replies.

Advertisement