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

9 years ago by jminor

I'd like to detect when an entity is being crushed between a moving platform and the collision map (and maybe also between two moving platforms) so I can apply damage when this happens.

My first intuition is to use handleMovementTrace and look for large differences between my position+vel*tick and the res.pos passed in - but that seems prone to other cases like just falling hard onto the floor. Any other ideas?

9 years ago by dominic

You could use the entities .collideWith() method. This method is called on both entities after the collision between them has been resolved. If they are still overlapping, there was no room to completely separate the two entities.

9 years ago by jminor

Thanks that seems to work great. I didn't realize that collideWith was called after the collision map was taken into account.

Here is what I ended up with:

	collideWith: function( other, axis ) {
		// check for crushing damage from a moving platform (or any FIXED entity)
		if (other.collides == ig.Entity.COLLIDES.FIXED && this.touches(other)) {
			// we're still overlapping, but by how much?
			var overlap;
			var size;
			if (axis == 'y') {
				size = this.size.y;
				if (this.pos.y < other.pos.y) overlap = this.pos.y+this.size.y - other.pos.y;
				else overlap = this.pos.y - (other.pos.y+other.size.y);
			}else{
				size = this.size.x;
				if (this.pos.x < other.pos.x) overlap = this.pos.x+this.size.x - other.pos.x;
				else overlap = this.pos.x - (other.pos.x+other.size.x);
			}
			overlap = Math.abs(overlap);
//			if (overlap > 0) {
//				console.log(this.name, "collided with", other.name, "on the", axis, "axis, but they are still overlapping by", overlap, "pixels");
//			}
			// overlapping by more than 1/2 of our size?
			if (overlap > size/2) {
				// we're being crushed - this is damage per-frame, so not 100% the same at different frame rates
				this.receiveDamage(1, other);
			}
		}
	},
Page 1 of 1
« first « previous next › last »