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 > Collision Response

#8598 - night_ - Wed Jul 16, 2003 5:33 am

Hi guys,

I am currently working on a tile based jump'n'run game. The scrollengine and character control works fine so far. Right now I am trying to find a solution for collision detection with the background (tiles). I am familiar with methods of detecting collision and I read the discussions in this forum and a lot of articles about CD. What caused me a lot of headache over the last couple of days is collision response. How do I react to a collision? Where do I place the sprite when a collision occured?

A friend of mine told me to check before a move if a collision will occure and if yes, to just leave the sprite where it is. The problem
however is, that this is not possible in all cases and it gets quite inprecise.

All articles I found only describe determining that a collision occured but not the response. I need a complete solution for collision detection. I know this is not something new and it was done 100s of times on 16 bit systems and GBA. Isn't there any document or source code which exactly describes the whole thing? How did people do that in SNES and Genesis games?
Is there sourcecode of a more complex, maybe even commercial (jump n run) game available?


I would greatly appreciate your help.
Thanks,
night

#8601 - Touchstone - Wed Jul 16, 2003 9:04 am

Well, what do you want to happen when collision occurs? A rocket for instance would spawn an explosion and perhaps inflict damage. A grenade would bounce away. A character colliding with a sloping floor would perhaps slide off, especially if the floor is slippery like ice. If the character collides with a lava floor you might want him to receive damage and jump away. If the character just collides with a regular wall/floor you probably want the acceleration of the character to align with the surface he collides with. There you have a couple of examples to do when a collision have been detected.
_________________
You can't beat our meat

#8602 - Lord Graga - Wed Jul 16, 2003 11:13 am


* LordGraga is too sexy for his hat

#9980 - sajiimori - Sat Aug 23, 2003 11:51 pm

It sounds like you want to know what to do if a character runs into a wall, and you want him to stop there.

Here's a simple approach in pseudo-code:
Code:

struct Actor
{
   int x, y;      // current position
   int xv, yv;      // velocity
   int w, h;      // width and height
};

collide actor with wall
where (wx, wy) = coordinates of block collided with
   what direction is actor moving? (check xv and yv)
      up:
         actor.y = wy + block_height
      down:
         actor.y = wy - actor.h
      left:
         actor.x = wx + block_width
      right:
         actor.x = wx - actor.w