1 decade ago by momander
Two questions:
1. Has anyone tried using distanceTo() to detect collisions, instead of using the default collision boxes? Any advice?
2. Is there a bug in getEntitiesByType() or am I using it wrong?
My game features mostly round entities, so rectangular collision boxes did not work well. Two round entities could look like they didn't touch on screen, but the corners of their collision boxes could overlap, causing a collision. Instead I did this:
Then I detect the distance between all entities in entities' update(). If the distance is less the radius of the two entities, an explosion occurs. This in effect creates circular collision zones around all entities. Here is the first draft of my code:
This caused an error:
I played with the code, and tested the version below. To my great surprise it worked. Explosions are caused when an entity gets into another entity's circular collision zone.
Why did the second code snippet work, and not the first one? And is this a good way of doing circular collision zones? I'd appreciate any feedback.
Martin
1. Has anyone tried using distanceTo() to detect collisions, instead of using the default collision boxes? Any advice?
2. Is there a bug in getEntitiesByType() or am I using it wrong?
My game features mostly round entities, so rectangular collision boxes did not work well. Two round entities could look like they didn't touch on screen, but the corners of their collision boxes could overlap, causing a collision. Instead I did this:
collides: ig.Entity.COLLIDES.NEVER
Then I detect the distance between all entities in entities' update(). If the distance is less the radius of the two entities, an explosion occurs. This in effect creates circular collision zones around all entities. Here is the first draft of my code:
var vessels = ig.game.getEntitiesByType(EntityVessel); for (var vessel in vessels) { if (vessel != this) { if (this.distanceTo(vessel) < (this.size.x/2 + vessel.size.x/2)) { ig.game.spawnEntity(EntityExplosion, this.pos.x, this.pos.y); this.kill(); vessel.kill(); } } }
This caused an error:
Uncaught TypeError: Cannot read property 'x' of undefined /space/lib/impact/entity.js:192
I played with the code, and tested the version below. To my great surprise it worked. Explosions are caused when an entity gets into another entity's circular collision zone.
var length = ig.game.getEntitiesByType(EntityVessel).length; for (i=0; i<length; i++) { var vessel = ig.game.getEntitiesByType(EntityVessel)[i]; if (vessel != this) { if (this.distanceTo(vessel) < (this.size.x/2 + vessel.size.x/2)) { ig.game.spawnEntity(EntityExplosion, this.pos.x, this.pos.y); this.kill(); vessel.kill(); } } }
Why did the second code snippet work, and not the first one? And is this a good way of doing circular collision zones? I'd appreciate any feedback.
Martin