Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Collision detection in mappy

This thread is locked; no one can reply to it. rss feed Print
Collision detection in mappy
scrub0bk
Member #12,543
February 2011

What I'm trying to do is recreate a pacman style game. Right now i want my player (circle) to go through my maze but I want him to only be able to move on the maze, not on every tile. I set the collisions for the tile in the Mappy Editor and i tried implementing the function that they used int my book for their horizontal scroller, and it didn't work the way i wanted. It detected some collision and stopped my player from moving but not on the actual maze.

Anyone have any ideas on what im doing wrong? Heres the function i used from my book:

int collided(int x, int y)
{
  BLKSTR* blockdata;
  blockdata = MapGetBlock(x/mapblockwidth, y/mapblockheight);
  return blockdata->tl;
}

Thank you.

Neil Walker
Member #210
April 2000
avatar

You do you there's a MapGetBlockInPixels, but that's beside the point.

I assume you've set blocks tl flag to be a wall?

If so, then you need to take into account the direction/movement of pacman. Your pacman's x/y is probably top-left of the bitmap and so your pacman won't register a block to your right or below until you are in the wall. Do you follow?

I'd draw a picture, but I'm lazy. Pretent your pacman and tiles are 32x32 and you are at the very top/left of the map on an open block. Your x/y is 0,0 and the map is 0,0 so if you are moving right you won't be at the next block to the right until your pacman moves another 32 pixels to the right.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

scrub0bk
Member #12,543
February 2011

Alright, so should i also create functions that do the same thing for the tr, bl, and br to allow for collision of each of those?

I tried doing the same thing function wise for each and the values only changes simultaneously.

Neil Walker
Member #210
April 2000
avatar

No, those are just flags that are not used by mappy, they are there for you to use in whatever way you want. They are called 'collision' in the editor simply for no reason other than giving them a name. They aren't really collision bits. Mappy gives you a set of user definable data items that you can use in whatever way:

- bit values (what you call collision)
- long data values
- short data values
- int data values

So you pick a value suited for your need. e.g. you want to store a flag to say the player can walk on it, use a bit value. Or if you have 8 different types of block types (e.g. can walk on if carrying a sword, cannot walk on, triggers another block, etc) then you'd maybe pick one of the short types.

For your collision you need to determine which direction the user is going (assuming no diagnal):

- RIGHT: get the block using the 'x+pacmanwidth',y
- LEFT: get the block using x,y
- UP: get the block using x,y
- DOWN: get the block using x,'y+pacmanheight'

Kind of thing.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

scrub0bk
Member #12,543
February 2011

I dont think i understand how the flags work. If i set them in mappy, how would i tell if my player goes into those blocks?

In other words, how would i ulilize the function i posted to allow it to tell me when the player was inside blocks, cause once i can tell if hes there, i can just set the x or y value to what it was before he entered.

The flags are confusing me to no end

Neil Walker
Member #210
April 2000
avatar

I think I explained everything. all of the user variables and flags are there for your own use, mappy does not use them, it knows nothing about your sprites, only the map. From your example, for all your tiles you cannot move into (i.e. pacman wall) set the block's 'tl' to be ticked.

In your code get the block you wish to move onto (i.e. check before you move) and check 'tl', exactly as your code. Your code is fine.

However, what you haven't taken into account is the width of pacman. When your pacman x,y is at say 0,0 (and assuming pacman is 32x32 in size) then the right hand edge is at position 31,0.

So when you move right you need to check the block at pixel location x+31 not x.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

scrub0bk
Member #12,543
February 2011

i understand what im trying to do. my call is like this:

if (Collision (player.x-player.height/2, player.y)){
      player.x = oldpx;
    }

I'm trying to check the left edge of my pacman, and what happens when i move is he moves forward (to the right) and then he stops and i can only move up and down.

I'm just really confused on how the call is working because of this. I know its simple and i'm probably pretty close but theres something thats jsut throwing me off

Neil Walker
Member #210
April 2000
avatar

you need to post your code for anyone to help.

If it's small enough, then just use the <code> tags

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Go to: