Archived

This topic is now archived and is closed to further replies.

2d side sroller Collision Detection

This topic is 5620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I''m creating a 2d side scroller like Duke Nukem II. I''m having a problem with Collision detection though. Here''s my code: for (yadda yadda yadda) { for (yadda) { if (Collision(yadda)) { now what? And This is collision routine: if (x1 >= x2+64) return false; if (x1 <= x2) return false; if (y1 >= y2+64) return false; if (y1 <= y2) return false; Any ideas would be helpful. Thanks alot, Cipher3D Gamedia Studios http://members.tripod.com/gamediastudios Aren''t there any good 2d collision detection algorithms (plus reaction to the collision) on GameDev? Thanks in advance :-)

Share this post


Link to post
Share on other sites
Do something like this (pseudocode):


GetInput();

if (MoveRight)
{
x1 += 5; // however much you wanna move him
// assuming x1 is player x, 64 is sprite width, x2
// is the position you don''t want it to go past
if ((x1+64) > x2) // he''s too far to the right
{
x1 = x2-64; // keep him in a "safe" position
}
}

// if left, etc..

Draw();


That''s a very crude system, but it should work. In that example the character will never be able to move past the object, he''ll stay just to the left of it. Hopefully this helps you a little. You could also not update the x variable (x1+=5 line), until after you know that position is "safe". Anyways, good luck.

Share this post


Link to post
Share on other sites
quote:
Original post by Cipher3D
[...]
if (x1 <= x2)
return false;
[...]
if (y1 <= y2)
return false;
[...]



Shouldn't those be more like

if (x1 + 64 <= x2)
return false;
if (y1 + 64 <= y2)
return false;

?


"All you need to do to learn circular logic is learn circular logic"

[edited by - BlueMonk on July 24, 2002 2:22:16 PM]

Share this post


Link to post
Share on other sites
[sarcasm]
ok, reading your yada yada yada code, here''s my solution (pseudo-code).


for (yada yada yada)
{
if (yada)
{
x = yada;
yada yada;
}
}
[/sarcasm]

ok, now that i posted that crap, i actually have to say something useful, or i''ll kill a white rapper (oh no!).


if (move_right)
{
// check for collision at (cur_x + move_amount, cur_y)

// if it occured, do nothing

// else cux_x += move_amount
}
it shouldn''t be that bad, only if you have low fps, the player might not actually touch the wall (instead he will stop when really close to it). if you figure out how much the player CAN move before hitting a wall (of course, if a collision occured then how_much_you_can_move_before_hitting_wall is < than move_amount), then instead of doing nothing (above code), add that value(how_much_you_can_move_before_hitting_wall). that should fix the problem (but it''s not that noticable anyway, so it''s ok if you keep it for now... untill u get more experience ).

---
shurcool
wwdev

Share this post


Link to post
Share on other sites