A Flash Developer Resource Site

# Thread: Help with Tile Based Collisions

1. ## Help with Tile Based Collisions

ok so i want to do set up my level with tiles, and then do pixel perfect collissions. but for as long as i can remember ive been unable to get past the collission side of things. and it has annoyed me for a long time.

so im asking for your help. this is how im doing the tiles:
Actionscript Code:
`var tWidth:Number = 32; // tile widthvar tHeight:Number = 32; // tile heightvar w:Number; // controls the individual value for the indexs for mapWidth.var h:Number; // controls the individual value for the indexs for mapHeight.// this is the layout of level. look at tile movieclip for graphics.// 0 = nothing// 1 = blockvar myMap:Array = [ [1,1,1,1,1],          [1,0,0,0,1],          [1,0,1,0,1],          [1,0,0,0,1],          [1,1,1,1,1] ];// function which draws up the mapfunction drawIt(){    var mapWidth:Number = myMap[0].length;    var mapHeight:Number = myMap.length;    for(h=0; h<mapHeight; h++){        for(w=0; w<mapWidth; w++){            var tile:t = new t();            addChild(tile);            tile.x = w*tWidth;            tile.y = tHeight*h;            tile.gotoAndStop(myMap[h][w]+1);            /*            this is how you'd add a character:            if (h==0 && w==5){                this.addChildAt(p1,2);                p1.x=w*tWidth;                p1.y=h*tHeight+160;            }            */        }    }}drawIt(); // calls the map drawing function we just made.`

im open to feedback, on better ways of setting-up/creating the tiles.

over the years, ive made countless attempts at finally making or at the very least using a system that wasn't so math heavy. i mean seriously were not talking about rocket science, just collisions!

ive tried gaming books, tutorials such as tonyPA or whatever his name is. outsideSociety and heaps of others off google. all resulting in the same hopeless failure.

over the years ive gave this my best shot trying to understand it. but with everytime resulting in fail. i feel like if i don't get it this time, i'll simply quit and not try this ever again. which isn't my usuall attitude. but after this long i think many would have already cracked.

2. And what exactly is the "same hopeless failure" you're referring to? What's the wall you're running into (no pun intended)? You have to give us a little something to give you feedback on. All you have now is the very standard tile creation method.

How involved you have to get is dictated by how involved your game is. Pixel perfect, you say? I would do a bounding-box-type of detection, wherein you would find the "corners" of your sprite and compare the underlying tiles for walkable, sticky, ladder, etc. From there you can adjust your player's location or state to your liking. If you have more code examples, try posting us a few; it might help us more clearly understand the route you're taking to achieving the collision detection you want and also to learn how you code to relay information in a way that you can both understand and relate to.

3. i didn't add the code for collission in what i posted because ive tried so many things that didn't work at all, i didn't see any reason in posting it.

"same hopeless failure" is simple. i mean it purely didn't work. it either didn't recognize the tile was there, or it didn something that was just as bad. i recall a few times it wouldn't let my character move and another time it made my character move in one direction, and i couldn't stop it or do anything to change what direction it went in.

ive tried alot of different methods. but the mothodyou deescribed is the only one i haven't. and the reason for that is because i never really understood how it worked or even how to do it. im sure if someone were to explain it so almost anyone could understand it, id probably pick it up and have it working...

4. I do believe the bounding-box method was laid out in TonyPA's tutorials, but in a nutshell it's quite simple:
• Offset these coordinates to obtain each "corner" coordinate of your sprite (i.e. top-left, bottom-left, etc)
• Translate each of these coordinates into the two-dimensional array by dividing each coordinate axis by the corresponding width or height of your tiles.
• Check each of these tile objects for the necessary traits such as walkable, sticky, etc.
• Offset your sprites original coordinate as necessary.

As a side-bar, this works ideally for sprites that are smaller than a tile's size, but it's not impossible to use sprites larger; it would require extra work but all you're really doing is determining all the tiles that your character sprite overlaps with and obtaining these through simple maths.

5. so perhaps this would be the best way to do it:
- dynamically find how many tiles the sprite could be touching at any one given time. (if its smaller then the tile size it will always be 4)

- find the players coordinates and divide by tilesize. to find the current tile the player is on. and then check all the tiles surrounding it for collissions.

i guess saying is easier then doing. but thanks for the help so far ImprisonedPride you've been a great help.

6. Originally Posted by x-death
(if its smaller then the tile size it will always be 4)
Just to clarify this point: it's not always four if the sprite is smaller than a tile; it could be 1, 2 or 4.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap