Unable to rotate 2D object properly with opengl












2















I'm using opengl, glfw and glew. I want to rotate the tip of the spaceship without moving the whole spaceship when users press on the left and right arrow respectively. I've tried using gltransform glrotate, it does rotate but it also move my spaceship along. The output of the movement of spaces



objects objects::rotateSpaceship(GLFWwindow * window, objects & sp)
{
GLfloat spaceship_angle = sp.getAngle();

GLfloat newAngle = sp.getAngle() + 0.5;
sp.setAngle(newAngle);

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);

GLfloat spaceShipRightLine =
{
x, y + 80, 0,
x - 50, y - 50, 0
};


GLfloat spaceShipLeftLine = {
x, y + 80, 0,
x + 50, y - 50, 0
};

GLfloat spaceShipMiddleLine = {
sp.x - 30 ,sp.y,0,
sp.x + 35,sp.y,0
};


glPushMatrix();

glTranslatef(0.0, 0.01, 0.0); // 3. Translate to the object's position.

glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.

glTranslatef(0.0, 0.01, 0.0); // 1. Translate to the origin.

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glPopMatrix();

// Swap front and back buffers
glfwSwapBuffers(window);

// Poll for and process events
glfwPollEvents();
}

glfwTerminate();
return sp;
}









share|improve this question





























    2















    I'm using opengl, glfw and glew. I want to rotate the tip of the spaceship without moving the whole spaceship when users press on the left and right arrow respectively. I've tried using gltransform glrotate, it does rotate but it also move my spaceship along. The output of the movement of spaces



    objects objects::rotateSpaceship(GLFWwindow * window, objects & sp)
    {
    GLfloat spaceship_angle = sp.getAngle();

    GLfloat newAngle = sp.getAngle() + 0.5;
    sp.setAngle(newAngle);

    while (!glfwWindowShouldClose(window))
    {
    glClear(GL_COLOR_BUFFER_BIT);

    GLfloat spaceShipRightLine =
    {
    x, y + 80, 0,
    x - 50, y - 50, 0
    };


    GLfloat spaceShipLeftLine = {
    x, y + 80, 0,
    x + 50, y - 50, 0
    };

    GLfloat spaceShipMiddleLine = {
    sp.x - 30 ,sp.y,0,
    sp.x + 35,sp.y,0
    };


    glPushMatrix();

    glTranslatef(0.0, 0.01, 0.0); // 3. Translate to the object's position.

    glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.

    glTranslatef(0.0, 0.01, 0.0); // 1. Translate to the origin.

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
    glDrawArrays(GL_LINES, 0, 2);
    glDisableClientState(GL_VERTEX_ARRAY);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
    glDrawArrays(GL_LINES, 0, 2);
    glDisableClientState(GL_VERTEX_ARRAY);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
    glDrawArrays(GL_LINES, 0, 2);
    glDisableClientState(GL_VERTEX_ARRAY);

    glPopMatrix();

    // Swap front and back buffers
    glfwSwapBuffers(window);

    // Poll for and process events
    glfwPollEvents();
    }

    glfwTerminate();
    return sp;
    }









    share|improve this question



























      2












      2








      2








      I'm using opengl, glfw and glew. I want to rotate the tip of the spaceship without moving the whole spaceship when users press on the left and right arrow respectively. I've tried using gltransform glrotate, it does rotate but it also move my spaceship along. The output of the movement of spaces



      objects objects::rotateSpaceship(GLFWwindow * window, objects & sp)
      {
      GLfloat spaceship_angle = sp.getAngle();

      GLfloat newAngle = sp.getAngle() + 0.5;
      sp.setAngle(newAngle);

      while (!glfwWindowShouldClose(window))
      {
      glClear(GL_COLOR_BUFFER_BIT);

      GLfloat spaceShipRightLine =
      {
      x, y + 80, 0,
      x - 50, y - 50, 0
      };


      GLfloat spaceShipLeftLine = {
      x, y + 80, 0,
      x + 50, y - 50, 0
      };

      GLfloat spaceShipMiddleLine = {
      sp.x - 30 ,sp.y,0,
      sp.x + 35,sp.y,0
      };


      glPushMatrix();

      glTranslatef(0.0, 0.01, 0.0); // 3. Translate to the object's position.

      glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.

      glTranslatef(0.0, 0.01, 0.0); // 1. Translate to the origin.

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
      glDrawArrays(GL_LINES, 0, 2);
      glDisableClientState(GL_VERTEX_ARRAY);

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
      glDrawArrays(GL_LINES, 0, 2);
      glDisableClientState(GL_VERTEX_ARRAY);

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
      glDrawArrays(GL_LINES, 0, 2);
      glDisableClientState(GL_VERTEX_ARRAY);

      glPopMatrix();

      // Swap front and back buffers
      glfwSwapBuffers(window);

      // Poll for and process events
      glfwPollEvents();
      }

      glfwTerminate();
      return sp;
      }









      share|improve this question
















      I'm using opengl, glfw and glew. I want to rotate the tip of the spaceship without moving the whole spaceship when users press on the left and right arrow respectively. I've tried using gltransform glrotate, it does rotate but it also move my spaceship along. The output of the movement of spaces



      objects objects::rotateSpaceship(GLFWwindow * window, objects & sp)
      {
      GLfloat spaceship_angle = sp.getAngle();

      GLfloat newAngle = sp.getAngle() + 0.5;
      sp.setAngle(newAngle);

      while (!glfwWindowShouldClose(window))
      {
      glClear(GL_COLOR_BUFFER_BIT);

      GLfloat spaceShipRightLine =
      {
      x, y + 80, 0,
      x - 50, y - 50, 0
      };


      GLfloat spaceShipLeftLine = {
      x, y + 80, 0,
      x + 50, y - 50, 0
      };

      GLfloat spaceShipMiddleLine = {
      sp.x - 30 ,sp.y,0,
      sp.x + 35,sp.y,0
      };


      glPushMatrix();

      glTranslatef(0.0, 0.01, 0.0); // 3. Translate to the object's position.

      glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.

      glTranslatef(0.0, 0.01, 0.0); // 1. Translate to the origin.

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
      glDrawArrays(GL_LINES, 0, 2);
      glDisableClientState(GL_VERTEX_ARRAY);

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
      glDrawArrays(GL_LINES, 0, 2);
      glDisableClientState(GL_VERTEX_ARRAY);

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
      glDrawArrays(GL_LINES, 0, 2);
      glDisableClientState(GL_VERTEX_ARRAY);

      glPopMatrix();

      // Swap front and back buffers
      glfwSwapBuffers(window);

      // Poll for and process events
      glfwPollEvents();
      }

      glfwTerminate();
      return sp;
      }






      c++ opengl glfw






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 17:07









      genpfault

      42.6k954100




      42.6k954100










      asked Jan 3 at 17:05









      Lim TingLim Ting

      256




      256
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades.
          Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.






          I want to rotate the tip of the spaceship without moving the whole spaceship [...]




          First you have to define the pivot, which is the "tip" of the spaceship:



          float pivot_x = sp.x;
          float pivot_y = sp.y + 80.0f;


          To rotate around a pivot you have to define a model matrix, which displaces by the inverted pivot, then rotate (glRotatef) and final transforms back by to the pivot (glTranslate):



          glTranslatef(pivot_x, pivot_y, 0);
          glRotatef(angle, 0, 0, 1);
          glTranslatef(-pivot_x, -pivot_y, 0);


          The final code may look like that:



          float pivot_x = x;
          float pivot_y = y + 80.0f;

          glPushMatrix();

          glTranslatef(pivot_x, pivot_y, 0 ); // 3. Translate to the object's position.
          glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
          glTranslatef(-pivot_x, -pivot_y, 0.0); // 1. Translate to the origin.

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glPopMatrix();





          share|improve this answer
























          • ahhh.. I see, thank you!

            – Lim Ting
            Feb 3 at 15:35












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026714%2funable-to-rotate-2d-object-properly-with-opengl%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades.
          Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.






          I want to rotate the tip of the spaceship without moving the whole spaceship [...]




          First you have to define the pivot, which is the "tip" of the spaceship:



          float pivot_x = sp.x;
          float pivot_y = sp.y + 80.0f;


          To rotate around a pivot you have to define a model matrix, which displaces by the inverted pivot, then rotate (glRotatef) and final transforms back by to the pivot (glTranslate):



          glTranslatef(pivot_x, pivot_y, 0);
          glRotatef(angle, 0, 0, 1);
          glTranslatef(-pivot_x, -pivot_y, 0);


          The final code may look like that:



          float pivot_x = x;
          float pivot_y = y + 80.0f;

          glPushMatrix();

          glTranslatef(pivot_x, pivot_y, 0 ); // 3. Translate to the object's position.
          glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
          glTranslatef(-pivot_x, -pivot_y, 0.0); // 1. Translate to the origin.

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glPopMatrix();





          share|improve this answer
























          • ahhh.. I see, thank you!

            – Lim Ting
            Feb 3 at 15:35
















          0














          Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades.
          Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.






          I want to rotate the tip of the spaceship without moving the whole spaceship [...]




          First you have to define the pivot, which is the "tip" of the spaceship:



          float pivot_x = sp.x;
          float pivot_y = sp.y + 80.0f;


          To rotate around a pivot you have to define a model matrix, which displaces by the inverted pivot, then rotate (glRotatef) and final transforms back by to the pivot (glTranslate):



          glTranslatef(pivot_x, pivot_y, 0);
          glRotatef(angle, 0, 0, 1);
          glTranslatef(-pivot_x, -pivot_y, 0);


          The final code may look like that:



          float pivot_x = x;
          float pivot_y = y + 80.0f;

          glPushMatrix();

          glTranslatef(pivot_x, pivot_y, 0 ); // 3. Translate to the object's position.
          glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
          glTranslatef(-pivot_x, -pivot_y, 0.0); // 1. Translate to the origin.

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glPopMatrix();





          share|improve this answer
























          • ahhh.. I see, thank you!

            – Lim Ting
            Feb 3 at 15:35














          0












          0








          0







          Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades.
          Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.






          I want to rotate the tip of the spaceship without moving the whole spaceship [...]




          First you have to define the pivot, which is the "tip" of the spaceship:



          float pivot_x = sp.x;
          float pivot_y = sp.y + 80.0f;


          To rotate around a pivot you have to define a model matrix, which displaces by the inverted pivot, then rotate (glRotatef) and final transforms back by to the pivot (glTranslate):



          glTranslatef(pivot_x, pivot_y, 0);
          glRotatef(angle, 0, 0, 1);
          glTranslatef(-pivot_x, -pivot_y, 0);


          The final code may look like that:



          float pivot_x = x;
          float pivot_y = y + 80.0f;

          glPushMatrix();

          glTranslatef(pivot_x, pivot_y, 0 ); // 3. Translate to the object's position.
          glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
          glTranslatef(-pivot_x, -pivot_y, 0.0); // 1. Translate to the origin.

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glPopMatrix();





          share|improve this answer













          Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades.
          Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.






          I want to rotate the tip of the spaceship without moving the whole spaceship [...]




          First you have to define the pivot, which is the "tip" of the spaceship:



          float pivot_x = sp.x;
          float pivot_y = sp.y + 80.0f;


          To rotate around a pivot you have to define a model matrix, which displaces by the inverted pivot, then rotate (glRotatef) and final transforms back by to the pivot (glTranslate):



          glTranslatef(pivot_x, pivot_y, 0);
          glRotatef(angle, 0, 0, 1);
          glTranslatef(-pivot_x, -pivot_y, 0);


          The final code may look like that:



          float pivot_x = x;
          float pivot_y = y + 80.0f;

          glPushMatrix();

          glTranslatef(pivot_x, pivot_y, 0 ); // 3. Translate to the object's position.
          glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
          glTranslatef(-pivot_x, -pivot_y, 0.0); // 1. Translate to the origin.

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glEnableClientState(GL_VERTEX_ARRAY);
          glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
          glDrawArrays(GL_LINES, 0, 2);
          glDisableClientState(GL_VERTEX_ARRAY);

          glPopMatrix();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 17:50









          Rabbid76Rabbid76

          42.7k123354




          42.7k123354













          • ahhh.. I see, thank you!

            – Lim Ting
            Feb 3 at 15:35



















          • ahhh.. I see, thank you!

            – Lim Ting
            Feb 3 at 15:35

















          ahhh.. I see, thank you!

          – Lim Ting
          Feb 3 at 15:35





          ahhh.. I see, thank you!

          – Lim Ting
          Feb 3 at 15:35




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026714%2funable-to-rotate-2d-object-properly-with-opengl%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas