Sign in to follow this  

Continous movement for square

This topic is 663 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

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 ;

}

 

Share this post


Link to post
Share on other sites

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.

Edited by Nanoha

Share this post


Link to post
Share on other sites

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));

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This topic is 663 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this