Advertisement

How to do collision detection in a 2D Jump'n Run?

Started by July 26, 2000 10:06 AM
6 comments, last by Nazghul 24 years, 4 months ago
Well, the Subject is exactly what I need to know. I tried different methods but I never got a good result. Maybe somebody around knows how to implement collision detection for a "Super Mario World"-Style game. Thanks for any suggestions!
Do you mean between the platforms and the character or between the character and the targets in the game?
Advertisement
I don''t know the official implementation but this is mine, I used it in Gen''s Gold, and works pretty good.

First of all make a function which returns whether a point in your world is Solid or not e.g:

where:

MAP_COLS = Number of Columns of Tiles. (128)
MAP_ROWS = Number of Rows of Tiles. (128)
MAP_W = Width of a Tile in Pixels. (16)
MAP_H = Height of a Tile in Pixels. (16)
BYTE map[MAP_ROWS][MAP_COLS]; // Array of MAP Blocks (0-255)
BYTE tile_solid[256]; // Solid/Trans/Slope1/Slope2/Top etc definition.

BYTE MAP_SOLID(DWORD x,DWORD y)
{
DWORD tx,ty;
BYTE dx,dy;
// Detect against the 4 boundaries (TOP/BOTTOM,LEFT/RIGHT)
if(x>=MAP_COLS*MAP_W) return 1; // OOB, Solid
if(y>=MAP_ROWS*MAP_H) return 1; // OOB, Solid
// Obtain Tile Square / Delta Point.
tx = x/MAP_W;
ty = y/MAP_H;
dx = x%MAP_W;
dy = y%MAP_H;
switch(tile_solid[map[ty][tx]])
{
case TILE_TRANSPARENT:return 0; // Always Trans regardless of dx/dy
case TILE_SOLID:return 1; // Always solid regardless of dx/dy
case TILE_BOTTOM:if(dy>=8) return 1;
break;
case TILE_TOP:if(dy<=7) return 1;
break;
}

return 0; // Just to keep all control paths returning a value
}

Basically thats the detection system for all point-pixel detections. This way you can find out if a point is solid or not, e.g for firing a bullet against the wall/slope. You would need to make up defines like:

#define TILE_TRANSPARENT 0
.
.
.
#define TILE_BOTTOM 3

then make each of your blocks e.g the Slope blocks could be tile #''s 5,6,7:

tile_solid[5] = TILE_SLOPE1;
tile_solid[6] = TILE_SLOPE1;
tile_solid[7] = TILE_SLOPE1;

That would be for one type of slope say a 45'' angle slope. You would prolly have 4 of these. And you might want to have 30'' slopes.

Once you''ve got that basic part done you can do the detection for the guy, which has to be smooth

// PROCESS MAN LOOP ///
// detect at the feet of the man either side.
c = MAP_SOLID(man.x-6,man.y);
d = MAP_SOLID(man.x+6,man.y);
safety = 16; // safety, prevents infinte loop/crashing.
while(c||d) // Theres Something solid at my feet.
{
man.vy=0; // Kill the Vertical Velocity.
man.y--; // Raise man up by one pixel
// detect the feet again.
c = MAP_SOLID(man.x-6,man.y);
d = MAP_SOLID(man.x+6,man.y);
safety--;
if(!safety) break;
}
///////////////////////

Basically the code above checks if you character''s feet are touching a Solid block. If they are then raise them up by one pixel, and check again. Keep doing this until the man is not touching the solid block, hence he''ll seem like he''s walking on the contour.

I have the feeling that professional developers didn''t do this the same way as me huh? - Well if it works, then it works - who cares about the method.

I also invented my own method for 3D detections, most probably not the ''official'' way of doing that either. And I doubt you wanna hear the crazy method.

Oh yeah, you have to detect against the Side-walls aswell, I think you should be able to sort that out yourself

/Memir
Flash Pool - Pool/Billiards game for Facebook (developed by Memir).
Wow, I never expected to get an reply in such a short time!

Whirlwind:
I''m looking for collision detection between the
actors and the platforms.

Memir:
Well, that looks really good. I must confess that I never
tried to implement a jump''n run as a tile-based game.
With the information and code you provided i could instantly
start to program. But i need to implement thinks like rotating
platforms etc. I wonder how to do that in a tile-based level?

Thanks again for your fast reply!
Ah, this reminds me of my action replay days in mario bros 3. These rotating platforms aren''t tiles, but are entities just like enemies (that''s what I found out in mario 3, before I started programming).

Sludge Software
www.sludgesoft.com
Developing a secret of mana style role-playing-game
Sludge Softwarewww.sludgesoft.comDeveloping a secret of mana style role-playing-game
For the rotating platforms, what Sludge said. For the rest of the platforms, you just us invisible tokens to define where a platform is at on a screen. If I knew where to get it, I''d tell you to look in the book ''Action Arcade Adventure Set''. I think you can get an digital copy here > http://www.makegames.com/

It basically covers how to write a basic side scroller in 2D.
Advertisement
you could probably find an article somewhere if you searched ''bounding box collision detection''. that''s the ''official'' name for what you''re trying to do.

JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Well,

I found the book online,
got my figure run around in a level
and I think that I''ll get those platforms implemented!

Thank you all a lot!

Nazghul

This topic is closed to new replies.

Advertisement