[Solved] Entity collideWith, bounding boxes


(Ultima2876) #1

Hello all, my turn to ask a question!

So I have an entity that needs to check if it is within anther entity, and if it is, block off a certain ability (several entities actually, in a tilemap). I’m using the following;

for each(var e: Region in entities) //entities is a list of all Regions
{
	if(Global.player.collideWith(e, Global.player.x, Global.player.y) != null)
	{
		trace("COLLIDE");
		//if there's a collision, we can't change
		canChange = false;
	}
}

My regions just use regular hitboxes (setHitbox(width, height)). The problem I’m having is that although it stops the player from changing if he overlaps the edge of a Region, if he is fully contained within it he can still change. I’m suspecting that the bounding box collision code in FlashPunk only checks edges of bounding boxes and not containment… in which case is there a way to check for containment? Or am I just doing something completely weird?

I’ve never done a platformer in FlashPunk before so all this collision stuff is actually pretty new to me!


(azrafe7) #2

You probably have a different setup, but testing with this simple code seems to work as expected:

FPTestContainment.swf (86.9 KB)

public class TestWorld extends World
{
	private var text:Text;
	private var player:Entity;
	private const NUM_REGIONS:int = 13;
	private var regions:Vector.<Entity>;
	
	override public function begin():void {
		text = new Text("info");
		var textEntity:Entity = new Entity(5, 15, text);
		text.scale = .5;
		add(textEntity);
		
		var playerImg:Image = Image.createRect(26, 32, 0xFFFFFF, .9);
		player = new Entity(FP.halfWidth, FP.halfHeight, playerImg);
		player.setHitboxTo(playerImg);
		add(player);
		
		regions = new Vector.<Entity>();
		for (var i:int = 0; i < NUM_REGIONS; i++) {
			var regionImg:Image = Image.createRect(40, 40, 0xFFFFFF, .7);
			var region:Entity = new Entity(Math.random() * FP.width, Math.random() * FP.height, regionImg);
			region.setHitboxTo(regionImg);
			regions.push(region);
			add(region);
		}
		
		FP.log("ARROWS - move player | R - reset");
	}
	
	override public function update():void 
	{
		super.update();
		
		// ESC to exit
		if (Input.pressed(Key.ESCAPE)) System.exit(1);
		
		// R to reset
		if (Input.pressed(Key.R)) FP.world = new TestWorld();
		
		var horzMove:int = Input.check(Key.LEFT) ? -1 : Input.check(Key.RIGHT) ? 1 : 0;
		var vertMove:int = Input.check(Key.UP) ? -1 : Input.check(Key.DOWN) ? 1 : 0;
		
		player.x += horzMove;
		player.y += vertMove;
		
		var nCollisions:int = 0;
		for each(var region:Entity in regions) {
			var regionImg:Image = Image(region.graphic);
			
			if (player.collideWith(region, player.x, player.y) != null) {
				nCollisions++;
				regionImg.color = 0xFF0000;
			} else {
				regionImg.color = 0xFFFFFF;
			}
		}
		
		var playerImg:Image = Image(player.graphic);
		playerImg.color = nCollisions > 0 ? 0xFF0000 : 0x00FF00;
		
		text.text = "collisions: " + nCollisions;
	}
}

What’s showing in the console for you? Are the hitboxes where they’re supposed to be?


(Ultima2876) #3

I don’t even have the console, apparently - this is a super old version o_O I should update it and try again - for some reason I didn’t think of that!

Thanks for the nice demo code! :slight_smile:

EDIT: Ah, yeah, now that I have the console I realise it’s just me making a silly mistake. My bounding boxes are not covering the whole area I thought they were! That’s what I get for spending a year doing engine code and no time at all making games…

Thanks again for your help!


(Ultima2876) #4