A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Tile-based collision detection

Threaded View

  1. #1
    Junior Member
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    6

    Lightbulb Tile-based collision detection

    Hello guys,

    I've been working on a little game project of mine where I have a tile-based engine.
    I've got a lot so far... But the collision detection is not working for me.
    As far as I can see, it should work, but it just doesn't somehow.

    I've got a demo here:
    http://murtada.nl/experiments/tiles/

    Move with WASD keys.

    The edge function for collidable tiles:
    Code:
    function get_edges(object):Array
    {
    	var r = Math.floor(Math.round(object.x+object.speedX+object.width*0.5)/WORLD.tileWidth);
    	var l = Math.floor(Math.round(object.x+object.speedX-object.width*0.5)/WORLD.tileWidth);
    	var b = Math.floor(Math.round(object.y+object.speedY+object.height*0.5)/WORLD.tileHeight);
    	var t = Math.floor(Math.round(object.y+object.speedY-object.height*0.5)/WORLD.tileHeight);
    	
    	var tr = currentLevel[t][r];
    	var tl = currentLevel[t][l];
    	var bl = currentLevel[b][l];
    	var br = currentLevel[b][r];
    	
    	var finalArray:Array = new Array();
    	finalArray["tr"] = tr;
    	finalArray["br"] = br;
    	finalArray["bl"] = bl;
    	finalArray["tl"] = tl;
    	finalArray["b"] = b;
    	finalArray["t"] = t;
    	finalArray["l"] = l;
    	finalArray["r"] = r;
    	return finalArray;
    }
    and here's the code for the actual collision detection and collision resolving:

    Code:
    function CollisionObjectTile(object)
    {
    	var edges:Array = get_edges(object);
    
    	// GOING DOWN
    	if (object.speedY > 0)
    	{
    		if (in_array(edges["br"],collidables) || in_array(edges["bl"],collidables))
    		{
    			object.speedY = 0;
    			object.y = edges["b"] * WORLD.tileHeight - object.height * 0.5;
    			object.onGround = true;
    
    			if (! object.walking)
    			{
    				object.speedX *=  WORLD.groundFriction;
    			}
    		}
    	}
    	// GOING UP
    	else if (object.speedY < 0)
    	{
    		if (in_array(edges["tr"],collidables) || in_array(edges["tl"],collidables))
    		{
    			object.speedY = 0;
    			object.y = (edges["t"]+1) * WORLD.tileHeight + object.height * 0.5;
    			if (! object.walking)
    			{
    				object.speedX *=  WORLD.groundFriction;
    			}
    		}
    	}
    
    	edges = get_edges(object);
    	// GOING LEFT
    	if (object.speedX < 0)
    	{
    		if (in_array(edges["tl"],collidables) || in_array(edges["bl"],collidables))
    		{
    			object.x = (edges["l"]+1) * WORLD.tileWidth + object.width * 0.5;
    			object.speedX = 0;
    		}
    
    		
    	}
    	// GOING RIGHT
    	else if (object.speedX > 0)
    	{
    		if (in_array(edges["tr"],collidables) || in_array(edges["br"],collidables))
    		{
    			object.x = edges["r"] * WORLD.tileWidth - object.width * 0.5;
    			object.speedX = 0;
    
    		}
    	}
    }
    I'd greatly appreciate it if someone could point me in the right direction or figure out what exactly I am doing wrong.

    Thanks!
    Last edited by Murtada-King; 08-12-2011 at 08:49 PM.
    -

Tags for this Thread

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center