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.

Beginners > checking obj and tiles collision

#172298 - genecyst - Fri Jan 29, 2010 4:12 pm

hi, i'm working on a new action puzzle game that use small maps 32x32 for any level.
i use an array loaded with the values in the tilemap, and for any tile, i've got some properties about the direction they're walkable (for example tile UP, tile DOWN, tile RIGHT, tile LEFT? and so on)

the real problem is that i can't find a way to check collision between tiles and sprites, without checking any single tile positions. this is not a problem till i've got only the player character on screen but having a lot of enemies the number of checks for collision became too high.

i've thought about dividing maps in various parts or more to decrease the number of checks, but i'm not sure it will work well?

what's the quickier way to do it?
thanks a lot!
frnkz - genecyst

#172300 - Azenris - Fri Jan 29, 2010 4:56 pm

If the player and enemies cannot occupy the same grid-location you can have an array of mobiles. That way you check collision by checking first you can move in that direction and then checking what's in the location your about to move, if its an enemy, don't move and take damage (or whatever).

If there can be multiple objects within 1 location (which my game is like) I have just been checking collision of the object in question with all other objects and seeing if they occupy the same space. Collision checks are only done during movement.

Not sure if any of that helps or not :p
_________________
My Homebrew Games

#172301 - genecyst - Fri Jan 29, 2010 5:18 pm

mmmh...you're right, i haven't explained very well... i need to check collision between player and map, not between player and enemies, that part i already know how to handle.

i'm looking for the fastest way to select the tiles that stand around player and enemies and perform collision only between them, so i will be able to move a lot of enemies and bullets without excessive slow down...
starting to check any tile i'll have 1 check x 32x32tiles x any obj i have on the screen.
just with 1 enemy and a bullet on the screen i go up to 768 collision detection and it doesn't seem a good way to perform it... :D

thanks anyway!

#172303 - gauauu - Fri Jan 29, 2010 6:28 pm

When checking a character against tiles, you should only have to check a very few tiles for collision, and that check should be very fast.

By looking at your character's world coordinates, dividing by the size of a tile, you should have the primary tile position of that character. Then, based on the size of that character, and whether they are partially on that tile and other, you can easily compute which other tiles need to be checked for collision.

Generally, if the character is 1 tile in size, you'll have to check at most 4 tiles, and if the character is 2 tiles, then you'll have to check at most 6 tiles.

Once you've computed which tiles the player is on, checking those tiles for properties (in your case the walkable direction) should generally be really fast, by looking up the properties in an array based on either the type of tile at point x,y, or merely just at the point x,y.

Now that I've said all that, I probably misunderstood your question and answered it wrong :)

#172349 - genecyst - Tue Feb 02, 2010 12:00 pm

you understand what i meant instead, i'm now doing collision detection around my 16x16 character, so i've got 8 collisions to check any time.

i'm still having some problems because my character rotate 360? and it's not so easy to give it the right x,y correction for collision check (this because i suck at math just like how i suck at coding...). and considering the final result i've got in mind i'm thinking about giving it a grid lock to simplify a lot the code.

thanks!