1
votes

I'm trying to draw a circle in opengl, but i can't seem to get the right coordinates, so i always get an ellipsis no matter what method i use. The current code is as follows:

void Ball::Render() {
    float center_position_x = _body->GetWorldCenter().x;
    float center_position_y = _body->GetWorldCenter().y;
    float radius = static_cast<b2CircleShape*>(_body->GetFixtureList()->GetShape())->m_radius;

    glBegin(GL_LINE_STRIP);
        for(float angle = 0; angle <= 360; ++angle) {
            float angleInRadians = glm::radians(angle);
            float x = glm::cos(angleInRadians);
            float y = glm::sin(angleInRadians);

            glVertex3f(x,y,0);
        }
    glEnd();
}

( I know that code should draw the circle at the origin, but even then it's not a perfect circle, if i understand correctly that should draw a circle at the origin with radius=1 )

The other methods i used were: http://slabode.exofire.net/circle_draw.shtml http://www.opengl.org/discussion_boards/showthread.php/167955-drawing-a-smooth-circle

This is my OpenGL window setup code (It's a stub program so i'm starting with hardcoded values): gluOrtho2D(-10, 15, -10, 15);

2
Sounds like an aspect ratio problem. How are you handling that? - Michael Dorgan
@MichaelDorgan Oh jesus. post that as an answer so i can accept it. shame on me - Délisson Junio

2 Answers

1
votes

Use gluOrtho2D(-WIDOWS_WIDTH, WIDOWS_WIDTH, -WINDOWS_HEIGHT, WINDOWS_HEIGHT);

By the way your are using fixed pipeline in 2013. With a vertex shader this would be much easy to understand.

1
votes

The aspect ratio of your 2D projection matrix defined by gluOrtho2d must be same as the viewport/window on which you are rendering otherwise you'll notice distortions in the figures you are rendering. You can use the above gluOrtho2d statement or other way of writing it is -

float ar = (float)WindowWidth/WindowHeight;

gluOrtho2D(-1*ar, ar, -1, 1)