Page 1 of 2

Platformer Collision Help

Posted: Sun Dec 19, 2010 6:34 pm
by thejahooli
I haven't done much coding in recent months apart from the small console programs here and there due to school, but I've decided to try and get back into it.

I am trying to make a platformer game and I have been able to make my character jump and have collision with just the outside of the screen, however I'm having trouble handling collision with tiles. I don't need someone to give me exact code but if someone could please give me an idea of how I could handle this I would be grateful. When I tried to do it my character either got stuck in the ground or fell through it altogether.

Re: Platformer Collision Help

Posted: Sun Dec 19, 2010 7:02 pm
by dandymcgee
Get the tiles the user COULD be colliding with using position and size of the sprite. For each of those tiles:

Code: Select all

if(player.y + player.vel.y >= tile.y) {
    player.y = tile.y - player.h
    player.vel.y = 0;
}

Re: Platformer Collision Help

Posted: Sun Dec 19, 2010 7:15 pm
by thejahooli
That is basically what I'm doing at the moment. There is probably one small aspect which is causing problems so I'm going to look through my code and see what could be wrong.

Re: Platformer Collision Help

Posted: Sun Dec 19, 2010 8:54 pm
by eatcomics
thejahooli wrote:That is basically what I'm doing at the moment. There is probably one small aspect which is causing problems so I'm going to look through my code and see what could be wrong.
Yeah, its usually mixing up x,y or a messed up variable or a / somewhere it shouldn't be :P

Re: Platformer Collision Help

Posted: Mon Dec 20, 2010 5:07 am
by MadPumpkin
I basically use this for strictly rectangular collision...

Code: Select all

bool srectcolide(SRECT*Rect, SRECT*TargetRect)
{
if( ( Rect->x > TargetRect->x ) && ( Rect->x < TargetRect->wx) && ( Rect->y > TargetRect->y ) && ( Rect->y < TargetRect->hy) )
     return true;
/*
-- And repeat with the variations for each possible type of collision from each direction. --
-- Which Falco discusses in his game development tutorial videos. --
-- TargetRect->wx -- is just TargetRect->w + TargetRect->x --
-- TargetRect->hy -- is just TargetRect->h + TargetRect->y  --
-- SRECT is just my rectangle structure, you can really use any that uses x, y, h, w respectively, with this code --
}

Re: Platformer Collision Help

Posted: Mon Dec 20, 2010 6:55 am
by N64vSNES
I'm fairly sure that this should be efficient for a platformer.....
http://elysianshadows.com/phpBB3/viewto ... art=999999

Re: Platformer Collision Help

Posted: Mon Dec 20, 2010 9:34 am
by MrDeathNote

Re: Platformer Collision Help

Posted: Mon Dec 20, 2010 1:44 pm
by thejahooli
I've got it working somewhat, there are still bugs, but I know why most of them are and am about to fix them. I ended up just rewriting it altogether utilising some of what has been said here.

Re: Platformer Collision Help

Posted: Wed Dec 22, 2010 10:44 pm
by thejahooli
I've finally got around to trying to fix this after being out of town for a while, but I'm having another problem. I understand why the problem occurs but I don't know how I could solve it.
The problem is that I do not know which tiles I should check for collision. If I check all of the ones which it is colliding with, I get incidents where the object acts as if it has landed on the floor when it hits a wall horizontally. When using Falco's method of checking from which direction the collision is occuring from, the object is colliding on the x-axis with one tile, however it is also colliding with the tile below it, but with the y-axis as the shallow axis, making the game think that he has collided with the floor. Sorry if this is a bit complicated to understand, but it is hard to phrase the problem.

*eedit*
diagram.png
diagram.png (2 KiB) Viewed 3313 times
The blue is the player, the red is a wall. The player is moving horizontally into the walls, and collides with both of them. My code checks both of these and, because he is colliding with the bottom one with the y axis as the shallow collision, it thinks that he has landed on it and therefore stops falling.

Re: Platformer Collision Help

Posted: Thu Dec 23, 2010 1:22 pm
by dandymcgee
thejahooli wrote: P.S. If someone could tell me how to insert an image into this post it could likely make this post a lot easier to understand.
Right underneath the Submit button there's two tabs "Options" and "Upload attachment". Upload the image and use the place inline button to put it where you want in your post.

Alternatively you can upload the img elsewhere and place the link between [img] tags.

Re: Platformer Collision Help

Posted: Thu Dec 23, 2010 3:57 pm
by N64vSNES
thejahooli wrote:I've finally got around to trying to fix this after being out of town for a while, but I'm having another problem. I understand why the problem occurs but I don't know how I could solve it.
The problem is that I do not know which tiles I should check for collision. If I check all of the ones which it is colliding with, I get incidents where the object acts as if it has landed on the floor when it hits a wall horizontally. When using Falco's method of checking from which direction the collision is occuring from, the object is colliding on the x-axis with one tile, however it is also colliding with the tile below it, but with the y-axis as the shallow axis, making the game think that he has collided with the floor. Sorry if this is a bit complicated to understand, but it is hard to phrase the problem.

P.S. If someone could tell me how to insert an image into this post it could likely make this post a lot easier to understand.
I think I understand, Are you saying that if the player hits a wall in mid air then as he comes down it detects a collision from the ground and lets him stand?.....I think thats what you said correct me if I'm wrong ;)

Re: Platformer Collision Help

Posted: Thu Dec 23, 2010 6:39 pm
by thejahooli
N64vSNES wrote:I think I understand, Are you saying that if the player hits a wall in mid air then as he comes down it detects a collision from the ground and lets him stand?.....I think thats what you said correct me if I'm wrong ;)
Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.

Re: Platformer Collision Help

Posted: Thu Dec 23, 2010 10:05 pm
by dandymcgee
thejahooli wrote:
N64vSNES wrote:I think I understand, Are you saying that if the player hits a wall in mid air then as he comes down it detects a collision from the ground and lets him stand?.....I think thats what you said correct me if I'm wrong ;)
Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.
How do you collide vertically with a wall? :shock:

Re: Platformer Collision Help

Posted: Thu Dec 23, 2010 10:32 pm
by eatcomics
dandymcgee wrote:
thejahooli wrote:
N64vSNES wrote:I think I understand, Are you saying that if the player hits a wall in mid air then as he comes down it detects a collision from the ground and lets him stand?.....I think thats what you said correct me if I'm wrong ;)
Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.
How do you collide vertically with a wall? :shock:
vertically with the ground you mean? most walls are vertical

Re: Platformer Collision Help

Posted: Fri Dec 24, 2010 8:35 am
by N64vSNES
thejahooli wrote:
N64vSNES wrote:I think I understand, Are you saying that if the player hits a wall in mid air then as he comes down it detects a collision from the ground and lets him stand?.....I think thats what you said correct me if I'm wrong ;)
Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.
If you're using the method Falco suggests to find the minimal overlap of the axis then that shouldn't happen. Post your collision function the may be somthing wrong.