A few pixels inside

Member
Posts: 131
Joined: 2010.08
Post: #1
These are pieces of code for drawing a circle and doing collision detection with the view's edges. Why are the left and right collision edges a few pixels inside the view?

Code:
    size = [self bounds].size;
    viewAspect = size.width / size.height;
glOrtho(0.0, viewAspect, 0.0, 1.0, -1.0, 1.0);

Code:
    GLint i;
    
    GLfloat cosine, sine, PI;
    PI = 3.14159265;
    glTranslatef(ballX, ballY, 0.0f);
    glBegin(GL_POLYGON);
    {
        for(i=0;i<100;i++)
        {
            cosine=cos(i*2*PI/100.0);
            sine=sin(i*2*PI/100.0);
            glVertex2f(RADIUS*cosine, RADIUS*sine);
        }
    }
    glEnd();

Code:
    if (((ballX + ballSpeedX) - viewAspect*RADIUS_FRACTION) < 0.0)
    {
        penetration = 0.0f - ((ballX + ballSpeedX) - viewAspect*RADIUS_FRACTION);
        ballX += penetration * 2.0f;
        ballSpeedX = -ballSpeedX;
        
    }
    if (((ballX + ballSpeedX) + viewAspect*RADIUS_FRACTION) > viewAspect)
    {
        penetration = ((ballX + ballSpeedX) + viewAspect*RADIUS_FRACTION) - viewAspect;
        ballX -= penetration * 2.0f;
        ballSpeedX = -ballSpeedX;
        
    }
    if (((ballY + ballSpeedY) - RADIUS_FRACTION) < 0.0)
    {
        penetration = 0.0f - ((ballY + ballSpeedY) - RADIUS_FRACTION);
        ballY += penetration * 2.0f;
        ballSpeedY = -ballSpeedY;
        
    }
    if (((ballY + ballSpeedY) + RADIUS_FRACTION) > 1.0)
    {
        penetration = ((ballY + ballSpeedY) + RADIUS_FRACTION) - 1.0;
        ballY -= penetration * 2.0f;
        ballSpeedY = -ballSpeedY;
        
    }
    
    ballY += ballSpeedY;
    ballX += ballSpeedX;
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #2
It seems that no-one who has looked at this question can answer it. Is any OpenGL expert on these forums willing to look at the entire project, if that makes it easier, so that I would send it.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.02
Post: #3
I'm no pro but wouldn't this work?
You would of course need a viewWidth variable.

Code:
ballY += ballSpeedY;
            ballX += ballSpeedX;

    if ((ballX - RADIUS) < 0.0)
    {
    penetration = 0.0 - (ballX - RADIUS);
        ballX += penetration * 2.0f;
        ballSpeedX = -ballSpeedX;
        
    }
    if ((ballX + RADIUS) > viewWidth)
    {
    penetration = (ballX + RADIUS) - viewWidth;
        ballX -= penetration * 2.0f;
        ballSpeedX = -ballSpeedX;
        
    }
    if ((ballY - RADIUS) < 0.0)
    {
        penetration = 0.0f - (ballY - RADIUS);
        ballY += penetration * 2.0f;
        ballSpeedY = -ballSpeedY;
        
    }
    if ((ballY + RADIUS) > 1.0)
    {
        penetration = (ballY + RADIUS) - 1.0;
        ballY -= penetration * 2.0f;
        ballSpeedY = -ballSpeedY;
        
    }

And I don't really get why your doing:
((ballX - viewAspect*RADIUS) < 0.0)
Could you explain your logic behind that if statement?
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #4
I improved the third piece of code, but forgot to update the post until now. However, the edges are still a few pixels inside the view.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Drawing: Transparent Pixels and NSImage elhochme 4 5,726 Jun 17, 2003 06:39 PM
Last Post: elhochme