Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:41:32+00:00 2026-06-02T06:41:32+00:00

I can rotate a 3D object, but it doesn’t seem to work for a

  • 0

I can rotate a 3D object, but it doesn’t seem to work for a 2D one.

I’d like to rotate my moveable (via arrows) square for 90 degrees right (rotation center: a center of a square). I’ve come up with a following code:

class CSquare : public CObject {
    SPoint pnta;         //left top corner of a square
    uint16 len;          //length
    bool bFill, bRotate; //filled? rotating?
    GLubyte color[4];    //filling color
    float angle;         //rotate for this

public:
    CSquare();
    CSquare(const CSquare &sqr);
    CSquare(SPoint &a, uint16 l, bool fill = false);
    CSquare(uint16 x, uint16 y, uint16 l, bool fill = false);

    void draw();
    void toggleRotate();
    void setColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a);
    void setPoint(uint16 x, uint16 y);

    SPoint getPoint();
    uint16 getPosX();
    uint16 getPosY();
    uint16 getLength();
};

void CSquare::draw() {
  glPushMatrix();
  if (bRotate) 
    if (++angle < 360.0f) 
    {
        glTranslatef(pnta.nX + len/2, pnta.nY + len/2, 0);
        glRotatef(90, 0, 0, 1);
    }
    else angle = 0.0f;

  if (bFill == true) glBegin(GL_QUADS);
  else glBegin(GL_LINE_LOOP);
    glColor4ubv(color);
    glVertex2i(pnta.nX, pnta.nY);
    glColor4ub(255, 255, 0, 0); //temporary to visualise rotation effect
    glVertex2i(pnta.nX + len, pnta.nY);
    glColor4ub(0, 255, 0, 0);
    glVertex2i(pnta.nX + len, pnta.nY + len);
    glColor4ub(0, 0, 255, 0);
    glVertex2i(pnta.nX, pnta.nY + len);
  glEnd();
  glPopMatrix();
}

My code works to some extent: it does rotate the object but not with a center in a desired point.

PS. I can upload full application if you need it (Visual Studio 2010 Project, uses FreeGLUT and SDL).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:41:34+00:00Added an answer on June 2, 2026 at 6:41 am

    I’m going to assume that you’re not actually rotating by a fixed angle: glRotatef(90, 0, 0, 1); If that’s not a transcription error, you should fix that first.

    That said, rotation always happens around the origin. You draw your shape at (pnta.nX, pnta.nY). It seems that you want to rotate around the shape’s center. To do that, you have to first move that point to the origin. Then perform the rotation, then move the point back where you want it:

    glPushMatrix();
    glTranslatef(pnta.nX + len/2, pnta.nY + len/2, 0);
    glRotatef(angle, 0, 0, 1);
    glTranslatef(-pnta.nX - len/2, -pnta.nY - len/2, 0);
    drawShape();
    glPopMatrix();
    

    We often model objects with their geometry centered around the origin by default. That way, we can simply rotate the object and then translate its reference point to where we want it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I rotate an image horizontally in WP7? It should seem like if
i have following code for moving 4 picture around circular way,but it can rotate
How can I rotate an Bitmap a given number of degrees while maintaining the
(iPhone) I'm trying to make it so that the user can rotate an object
in a .swf object, i rotate pictures in one direction : clockwise (+90°), with
I would like to drag an object in one line. I already know how
I am trying to change my Sprite anchor point so that I can rotate
I have a problem with rotating. I know that I can rotate a Texture2D
How can I rotate the Apache Access and Error logs on a Window 2000
How can I rotate the text within an HSSFCell class of Apache POI?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.