Platformer Collision Help
Moderator: PC Supremacists
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Platformer Collision Help
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 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.
- dandymcgee
- 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
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!
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: Platformer Collision Help
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.
Re: Platformer Collision Help
Yeah, its usually mixing up x,y or a messed up variable or a / somewhere it shouldn't be :Pthejahooli 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.
- MadPumpkin
- 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
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
For God so loved the world that he blessed the thugs with rock
Re: Platformer Collision Help
I'm fairly sure that this should be efficient for a platformer.....
http://elysianshadows.com/phpBB3/viewto ... art=999999
http://elysianshadows.com/phpBB3/viewto ... art=999999
- MrDeathNote
- 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
This mite help http://elysianshadows.com/phpBB3/viewto ... f=6&t=5119
http://www.youtube.com/user/MrDeathNote1988
"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
"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
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: Platformer Collision Help
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.
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: Platformer Collision Help
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* 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.
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* 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.
- dandymcgee
- 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
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.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.
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!
Re: Platformer Collision Help
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 wrongthejahooli 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.
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: Platformer Collision Help
Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.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
I'll make your software hardware.
- dandymcgee
- 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
How do you collide vertically with a wall?thejahooli wrote:Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.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
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: Platformer Collision Help
vertically with the ground you mean? most walls are verticaldandymcgee wrote:How do you collide vertically with a wall?thejahooli wrote:Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.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
Re: Platformer Collision Help
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.thejahooli wrote:Yes, as he comes down he detects a collision with the wall, but thinks it's the ground as it is colliding vertically.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