Diddy Simple Tile Collision
Monkey Forums/Monkey Code/Diddy Simple Tile Collision
| ||
quick n dirty. Works with diddy's tile system, just input your tilemap and a layername from the XML. Obviously not perfect, since it's not using ray collisions. [monkeycode] '' in screen pixel X,Y Function TileCollision:Int(map:TileMap,layername:String, playerx%, playery%, playerw%, playerh%, offx%, offy%) If Not layername Or map.tileWidth=0 Or map.tileHeight=0 Return 0 '' string search could be removed for faster lookups Local layer:TileMapTileLayer For Local tl:TileMapLayer = Eachin map.layers If TileMapTileLayer(tl) If tl.name = layername Then layer = TileMapTileLayer(tl) ; Exit Endif Next If layer.name <> layername Then Return 0 ''convert x,y,w,h to grid Local gx%, gy%, gw%, gh% playerx=playerx+offx playery=playery+offy gx = (playerx / map.tileWidth) gy = (playery / map.tileHeight) gw = (playerw / map.tileWidth) gh = (playerh / map.tileHeight) 'Print gx+"+"+gy Local coll:Int=0 For Local y:= gy To gy+gh If coll Then Exit If y<0 Or y>map.height Then Continue For Local x:= gx To gx+gw If x<0 Or x>map.width Then Continue If layer.mapData.Get(x,y) Then coll=1 ; Exit Next Next 'If coll Then Print gx+":"+gy+" "+coll Return coll End [/monkeycode] Example of usage, for a centered top-down perspective for rpg map. used in OnUpdate(): |