Sprite Collision VS Environment Collision (Help!!!!)

Started by
2 comments, last by Xorcist 23 years, 3 months ago
OK, I''m a bit new to this. So maybe someone can help me out. I''m working on a Pacman clone, and I have the basic concepts down for sprite collision using bounding boxes, and I can handle eating pelettes and getting killed by ghosts, but what about the board? Do I just treat all my 16x16 wall tiles as individual sprites? Is there a better way? And if I do treat them as sprites how do I keep my character from going into the walls (do I just define a larger bounding box for each wall tile)? I was thinking of defining a path array with values that corrispond to valid positions on the board and checking that against each new move to see whether that move can be done or not (but on average that''s 18773 values for a 352x320 map). Any advice would be appreciated. P.S. I''m not using OOP, I''m using standard records/structures to define my sprites/objects.
Advertisement
this method isn''t very flexible (for use in other games and whatnot) but should work fairly well for pacman.

Define an array of the board which will hold all the walls and paths. You can make it simply ints or a whole structure defining the sprite for each tile, special effects, whether its passable or a wall, etc. We really just need to know if the tile is passable or not, so simple ints will do just fine. (However, a struct that holds a pointer to the sprite to use would be useful when drawing the board)

Anyway, fill the array with 0s for passable tiles and 1s for impassable tiles (or however you like). Also for each ghost and pacman himself, hold which tile they are currently in. When a ghost or pacman is about to enter a new tile, check if it is passable or not. If so, then let the sprite go, if not stop the sprite.

simple. There might be something i overlooked though, i''m doing too many things at once right now and don''t have a clear head...
meh
Ian Halliday yoPWEENED
Yes and as clear code that would be like:

    unsigned char map[16][16]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}  Now to check the collision, do this:      float pac_size=0.3f;float pac_x=10.4f; //xfloat pac_y=7.4f; //and y coordinatesvoid mainloop(){float oldx=pac_x,oldy=pac_y;if (key[left]) { pac_x-=0.1f; if (hit()) pac_x=oldx; }if (key[right]){ pac_x+=0.1f; if (hit()) pac_x=oldx; }if (key[up])   { pac_y-=0.1f; if (hit()) pac_y=oldy; }if (key[down]) { pac_y+=0.1f; if (hit()) pac_y=oldy; }}bool hit(){if (map[int(pac_y-pac_size)][int(pac_x-pac_size)]==1) return true; //hits the wallif (map[int(pac_y-pac_size)][int(pac_x+pac_size)]==1) return true; //hits the wallif (map[int(pac_y+pac_size)][int(pac_x-pac_size)]==1) return true; //hits the wallif (map[int(pac_y+pac_size)][int(pac_x+pac_size)]==1) return true; //hits the wallreturn false;}    



Edited by - civguy on January 15, 2001 5:59:07 PM
Thanks guys.

This topic is closed to new replies.

Advertisement