Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualAlanSmithee

Posted 20 June 2012 - 06:07 AM

AABB collision detection is pretty straight forward.

You just check if object A is "inside" of object B, and if so, take according action.
What you want to be careful of though is to not iterate through more tiles then you need to. It's a waste of RAM and not needed, you only want to check vs the adjuncent tiles, in other words, only the tiles that your character have any chance of colliding with.

Something like:
var startx = entity.x / tilewidth;
var endx = entity.x / tilewidth + entity.w;
var starty = entity.y / tileheight;
var endy = entity.y / tileheight + entity.h;

for (var y = starty; y <= endy; y++)
{
	for (var x = starx; x <= endx; x++)
	{
		if (map[y][x].type === collidable)
		{
			// do something fun :-)
		}
	}
}

I am at work now but this psuedocode should hopefully get you started Posted Image

Edit* also, I'm unsure of what events you are reffering to outside of collisiondetection but you should be able to invoke any event you want in the loop. Also you can adjust start and end x/y choords if need be.

#2AlanSmithee

Posted 20 June 2012 - 05:33 AM

AABB collision detection is pretty straight forward.

You just check if object A is "inside" of object B, and if so, take according action.
What you want to be careful of though is to not iterate through more tiles then you need to. It's a waste of RAM and not needed, you only want to check vs the adjuncent tiles, in other words, only the tiles that your character have any chance of colliding with.

Something like:
var startx = entity.x / tilewidth;
var endx = entity.x / tilewidth + entity.w;
var starty = entity.y / tileheight;
var endy = entity.y / tileheight + entity.h;

for (var y = starty; y <= endy; y++)
{
	for (var x = starx; x <= endx; x++)
	{
		if (map[y][x].type === collidable)
		{
			// do something fun :-)
		}
	}
}

I am at work now but this psuedocode should hopefully get you started Posted Image

#1AlanSmithee

Posted 20 June 2012 - 05:32 AM

AABB collision detection is pretty straight forward.

You just check if object A is "inside" of object B, and if so, take according action.
What you want to be careful of though is to not iterate through more tiles then you need to. It's a waste of RAM and not needed, you only want to check vs the adjuncent tiles, in other words, only the tiles that your character have any chance of colliding with.

Something like:
var startx = entity.x / tilewidth; 
var endx = entity.x / tilewidth + entity.w;
var starty = entIty.y / tileheight;
var endy = entIty.y / tileheight + entity.h;
 
for (var y = starty; y <= endy; y++)
{
    for (var x = starx; x <= endx; x++)
    {
	    if (map[y][x].type === collidable object)
	    {
		    // do something
	    }
    }
}

I am at work now but this psuedocode should hopefully get you started :)

PARTNERS