Hello,
I'm building a little platform game. I am currently stuck on the collision detection. here is my code:
-(void)gameLoop:(ccTime)dt
{
CGPoint ccPlayerPos = CGPointMake(player.position.x, player.position.y);
CGPoint playerPos = [[Director sharedDirector]convertCoordinate:ccPlayerPos];
CGPoint ccGroundPos = CGPointMake(ground.position.x, ground.position.y);
CGPoint groundPos = [[Director sharedDirector]convertCoordinate:ccGroundPos];
playerRect = CGRectMake(playerPos.x - (player.contentSize.width/2),playerPos.y - (player.contentSize.width/2), player.contentSize.width, player.contentSize.height);
groundRect = CGRectMake(groundPos.x - (ground.contentSize.width/2),groundPos.y - (ground.contentSize.width/2), ground.contentSize.width, ground.contentSize.height);
if (CGRectIntersectsRect(playerRect, groundRect)) {
groundCollision = YES;
}
else {
groundCollision = NO;
}
if(groundCollision == NO) {
ySpeed -= gravity;
}
else {
ySpeed = 0;
}
[player setPosition:ccp(player.position.x + xSpeed,player.position.y + ySpeed)];
}
This works, but in the wrong place. My players stops falling somewhere in the middle of the screen, not on the ground. What am I doing wrong? if I remove the code with the convertCoordinate, my player stops some far below the screen. I thing it was (x,-143) or something... Now it stops at (x, 173.399994). Can someone help me? :)
Thanks in advance, any help is appreciated.
