I seem to be having a strange issue with a 2D game I'm working on. In order to determine if my playable character sprite lands on a platform I first do an if statement checking CGRectIntersectsRect and once I've determined that the player sprite and platform sprite have intersected, I then check to make sure that the center point (plus half the player's height) are above the platform, if it is, I then set the player's 'y' velocity to zero.
playerVelocity.x=5;
playerVelocity.y+=gravity;
if (CGRectIntersectsRect([turtle getBounds], [platform getBounds])) {
if ([player getPosition].y>p.position.y+([player getBounds].size.height/2)) {
if (playerVelocity.y>0) {
playerVelocity.y=0;
inJump=NO;
}
}else {
inJump=YES;
}
}
[player setPosition:CGPointMake([player getPosition].x+playerVelocity.x, [player getPosition].y-playerVelocity.y)];
Most of the time this code works over the game cycle, however every now and then, the second "if statement" is disregarded and my player sprite passes through the platform. I'm building this game in OpenGL ES 1.1 with a custom sprite class.
Any insight into this issue would be appreciated!