Blocking character movements

Started by
2 comments, last by GameDev.net 24 years, 6 months ago
Not sure what you mean by top down game...but i think ill be able to help you out a bit hopefully. All most all 2d games (metroid,megaman,rpg's,zelda,ect) use tiles to make up the worlds.
With tiles its real easy to do collision detection. All you do is in your tile structure you have something like a int terran modification;

then when you move you can do something bout like this.

if(map[playerx][playery].type=1)playerx = old.x,playery=old.y;

Hope this helps some if you need more info i can send you some examples.

Advertisement
COuld you show me some examples...I got basic collision detection but its not that good
I'm creating a 2D top down game, and I was wondering what is the best way to block the character from moving through walls, and help or links is appreciated
ok here is somewhat of a example if you need more email me at lordvar@in-tch.com and ill see what i can do.

typedef struct map{
char tile; //tile id(pic of tile to draw)
char type; // any flags for the tile.(up to 255)
}

map CurMap[200][200]; //array of struct map.

Now what you do for collision detection is like this
logic(){
int oldx = playerx;
int oldy = playery;
now
you do your movment checks like
if(key_up)playerx--;
and so on.
now after that we go
if(CurMap[playerx/tilesize][playery/tilesize].type = 1){playerx = oldx,playery=oldy;}

Hope this helps. You might have to add some stuff if your player sprite is bigger or smaller than your tile size.

This topic is closed to new replies.

Advertisement