Platformer Collision Help

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Platformer Collision Help

Post 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.
I'll make your software hardware.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Platformer Collision Help

Post 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;
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Platformer Collision Help

Post 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.
I'll make your software hardware.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Platformer Collision Help

Post 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
Image
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Platformer Collision Help

Post 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 --
}
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Platformer Collision Help

Post by N64vSNES »

I'm fairly sure that this should be efficient for a platformer.....
http://elysianshadows.com/phpBB3/viewto ... art=999999
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Platformer Collision Help

Post by MrDeathNote »

http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Platformer Collision Help

Post 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.
I'll make your software hardware.
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Platformer Collision Help

Post 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 3308 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.
Last edited by thejahooli on Thu Dec 23, 2010 6:50 pm, edited 1 time in total.
I'll make your software hardware.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Platformer Collision Help

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Platformer Collision Help

Post 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 ;)
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Platformer Collision Help

Post 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.
I'll make your software hardware.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Platformer Collision Help

Post 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:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Platformer Collision Help

Post 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
Image
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Platformer Collision Help

Post 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.
Post Reply