Collision Detection for side scrolling games

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Collision Detection for side scrolling games

Post by ajtgarber »

Not to sound like an idiot or anything but... how do you handle collisions on side scrollers? So far I've only had trouble when the level scrolled
but when it had no offsets it worked fine :evil: . Right now the game is a 15x15 grid of 32x32 tiles, and I've tried about 10 ways in the past month
to get this to work and so far they've failed.

Ps. Sorry if I sound retarded, I'm just frustrated and tired
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Collision Detection for side scrolling games

Post by GroundUpEngine »

If your doing collision e.g. player vs tile, then you will need to test the object against the player's position + the offset ;)

Something like this will work->

Code: Select all

bool Player::IsCollide(Tile tile, int offsetX, int offsetY)
{
    // add the offsetx    
    _x = x + offsetX;
    _y = y + offsetY;    

    if( (_x >= tile.x) && (_x <= tile.x + tile.w) && (_y >= tile.y) && (_y <= tile.y + tile.h) )
        return true;
}
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Collision Detection for side scrolling games

Post by eatcomics »

yeah it isn't easy to figure out how to help without some code, or an explanation of how you're doing things, but GUE's advice should help :D

In layman's terms: give us more info, so we can better help you...
Image
ClassA
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Tue Feb 23, 2010 1:48 pm

Re: Collision Detection for side scrolling games

Post by ClassA »

ajtgarber wrote:So far I've only had trouble when the level scrolled but when it had no offsets it worked fine :evil:
Generally, I will always store player position and calculate collisions using "world" coordinates (i.e. positions relative to the level). I store the "camera" position separately and use it as an offset to convert "world" to "screen" coordinates only when drawing. This means that scrolling has no effect on game logic/collision code.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Collision Detection for side scrolling games

Post by Falco Girgis »

ajtgarber wrote:Not to sound like an idiot or anything but... how do you handle collisions on side scrollers? So far I've only had trouble when the level scrolled
but when it had no offsets it worked fine :evil: . Right now the game is a 15x15 grid of 32x32 tiles, and I've tried about 10 ways in the past month
to get this to work and so far they've failed.

Ps. Sorry if I sound retarded, I'm just frustrated and tired
It should be the exact same as with nonscrolling. The only "offsets" that scrolling is applying is only for RENDERING, not to the actual location of the entities. So I'm not exactly sure how you're handling it.

But like ClassA said, it shouldn't make any difference how things are "rendered."

Imagine if my collision checks changed when we zoomed in/out and rotated the world in ES. :shock:
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Collision Detection for side scrolling games

Post by GroundUpEngine »

Soz ignore my post, I haven't got enough sleep recently :lol:
But ye only the tile rendering gets affected by the offsets, whereas the collision checks are normal just like non-scrollers.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Collision Detection for side scrolling games

Post by ajtgarber »

Thanks for the replies! I think I figured out how to get it working *crosses fingers*, thanks everyone! Falco, great work on LibGyro btw!
Post Reply