Entities Overlap


(Gil) #1

In my project, copies of the same entity are created every few seconds and travel across the screen at a set speed. The problem is, their hitboxes are at a certain size that is crucial to gameplay. These hitboxes are smaller than the actual image of the entity, which makes it so that if two of the same entity collide, their hitboxes might not overlap but their images do overlap.

On top of that, I wouldn’t know how to put in a collision check between two of the same entity, because it would create a paradox where both colliding entities will behave the same way. (E.g., if (collide(“obstacle”,x,y) this.x += 40, both of the colliding entities will move towards same direction and at the same speed, which makes it so that they still overlap, the same way).

I made a quick picture to describe the scenario, and what i’d like my entitie’s behavior to be like.

Please and thank you!


(John Andersson) #2

Maybe go about doing it like this?

Variables

[Embed(source="assets/block.png")]private const BLOCK:Class;

graphicfx:Image = new image(BLOCK)

Constructor

type = "block"

Update

if(collide("block", x + graphicfx.width, y + graphicfx.height)
{
    //stop
}

I have no idea if this even works, just want to try to help anyway :stuck_out_tongue:

EDIT:

Maybe use the collideRect function? I don’t understand it completely, but it seems to be a good way to do it…


(Gil) #3

I was thinking I’d create another “invisible” entity and make it follow this one. It would have it’s own hitbox and I can plug in behavior for that one. Each time there would be a collision it would be between the original entity’s hitbox and this pseudo hitbox. I’m wondering if there’s still an easier more efficient method to this though.


(billy2000) #4

I think u can use moveBy() function :smile: You can try something like this:

private var xSpeed:Number;//the speed on x axis
private var ySpeed:Number;//the speed on y axis

in constructor:

type="object";

in update:

moveBy(xSpeed, ySpeed, "object");

And also for moveBy there are some handy functions, 2 of them are moveCollideX and moveCollideY. so we will override 1 of them like this:

override public function moveCollideX(e:Entity):Boolean {//e its the instance this instance collide with
		if(e.x < x ) x+=40;
                else x-=40;
		return true;
	}

Well pretty much that is. I hope you will get this working :smile:


(billy2000) #5

Ah actually there is even a easier way of doing this:

var otherObject:YourEntity = collide("theEntityType", x, y) as YourEntity;
if(otherObjec.x < x ) x+=40;
else x-=40;