iDevGames Forums
A few pixels inside - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: A few pixels inside (/thread-7973.html)



A few pixels inside - Miglu - Aug 10, 2010 08:00 PM

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;



RE: A few pixels inside - Miglu - Aug 12, 2010 02:46 PM

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.


RE: A few pixels inside - McSpider - Aug 12, 2010 07:03 PM

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?


RE: A few pixels inside - Miglu - Aug 12, 2010 07:41 PM

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.