gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > BG\Sprite Collision

#19633 - dagamer34 - Fri Apr 23, 2004 12:42 am

I have been working on tile-sprite collision for a while and have finally gotten it to respond to collision nicely. The only problem is that my collision detection system isn't accurate enough.

I have my algorithm check 4 corners of a sprite against the tiles, and the middle borders of the sprite. Collision is almost accurate, but sometimes it fails because it misses parts of the sprite.

To show you what I mean, run my little demo.

www.geocities.com/dagamer34/ColTest.zip

Press B to run and jump higher (when running). Now, if you get to the left box, and decide to walk off the box to the right, you get stuck when you should be falling down. My algorithm is accurate only for a small sprite, but since I only check 3 times on each side, mishaps occur.

My question is, is there a better way to check collision besides tracing all along the sprite itself?
_________________
Little kids and Playstation 2's don't mix. :(

#19636 - sajiimori - Fri Apr 23, 2004 1:04 am

Well, the link doesn't work, and I don't know what you mean by "middle borders", but it sounds like you aren't checking all the tiles that are covered by the rectangle.
Code:

bool rect_bg_collision(Rect* r)
{
  int top    = point_to_tile(r->y);
  int bottom = point_to_tile(top + r->h);
  int left   = point_to_tile(r->x);
  int right  = point_to_tile(left + r->w);

  for(int y = top; y < bottom; ++y)
    for(int x = left; x < right; ++x)
      if(tile_is_solid(x, y))
        return true;

  return false;
}

#19644 - dagamer34 - Fri Apr 23, 2004 2:41 am

So, you have to check all the tiles then; there's no avoiding it?

In your code, wouldn't you only need to check the first and last row completely, and then only the first and last tiles in the other rows? I guess I will optimize your code then.

Thanks for your help anyway, it was really useful.
_________________
Little kids and Playstation 2's don't mix. :(

#19650 - sajiimori - Fri Apr 23, 2004 3:47 am

Maybe I didn't understand your problem. I thought you said your collision detection wasn't accurate enough; that is, it's not detecting all collisions.

For a rectangle that is covering a 3x3 area of tiles, it is quite possible for there to be a collision against the middle tile but not the outer ones. Could the object ever get into that situation? Sure, if it were moving more than a tile at a time.

If you want to ignore some of the tiles, you'd better be sure it's impossible for a collision to happen there, or else your "optimizations" could come back to haunt you.

#19651 - Miked0801 - Fri Apr 23, 2004 4:00 am

WIth a little vector math you can check for line/line collisions if you need. This is what we do for BG collisions as it allows for angled BG collisions tiles - and is pretty fast to boot. There's an older article on here about that somewhere...

#19732 - dagamer34 - Sat Apr 24, 2004 12:09 am

My code is pretty accurate with what it checks, I just don't check everything because of the recursive looping involved. I wanted to see if there was another way, instead of looping through every tile, since that is VERY time-consuming, and I was trying to optimize it for the fewest checks. I guess that bit me in the back.
_________________
Little kids and Playstation 2's don't mix. :(