Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by DerDree

I'm very new with impactjs. Is there an easy way to check if an entity does not collide with any other entity? I figured out that the check-function is only fired when there is at least one collision, but is there any method to check for the opposite?

Thanks in advance :)

1 decade ago by StuartTresadern

Not sure I understand your question but the .collidewith only gets fired when the entity collides with another entity http://impactjs.com/documentation/class-reference/entity#collidewith. So by default if this is not called your entity is not in a collision.

1 decade ago by DerDree

Thanks. I try to explain it a little more:

I want to spawn an entity and if it doesnt collide with any other entity at spawn it shall be removed immediatly.

At the moment I spawn the entity and check in the update-function after a short fixed time if the check-function was fired and toggled a flag. If this is not the case it is removed.
This works but I dont like to wait a fixed time, so a function to check for the not-colliding would work immediatly.

1 decade ago by alexandre

IOW--correct me if I'm wrong-- you only want to spawn this specific type of entity if there is another (any kind of) entity at the eventual spawn coordinates? In that case, and assuming you spawn from within your game subclass, you can do a conditional spawn (spawnIf):

// main.js

spawnIf: function(x, y)
{
	if (this.getEntitiesAt(x,y).length > 0)
		this.spawnEntity('EntityX', x, y);
},

getEntitiesAt: function(x, y)
{
	var n = ig.game.entities.length;
	var ents = [];
	for (var i=0; i<n; i++)
	{
		var ent = ig.game.entities[i],
			x0 = ent.pos.x,
			x1 = x0 + ent.size.x,
			y0 = ent.pos.y,
			y1 = y0 + ent.size.y;
			
		if (x0 <= x && x1 > x && y0 <= y && y1 > y)
			ents.push(e);
	}
	return ents;
}

1 decade ago by DerDree

No I want to spawn the entity without any condition. But immediatly after the spawn the entity shall be removed, if there is no other entity colliding with it.

But your code is very helpful, because your getEntitiesAt-function is exactly what I need to achieve this, thanks :)
Page 1 of 1
« first « previous next › last »