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 Jarkko

Ok, I think there must be a simple explanation for this, but I'm just too thick to get it.

I have an entity that follows the mouse. The following is the code from the update() method of the entity:


if (this.follow) {
				var EASING = 0.03;
				var dx = ig.input.mouse.x - this.pos.x;
				var dy = ig.input.mouse.y - this.pos.y;
				//var dx = this.pos.x - ig.input.mouse.x;
				//var dy = this.pos.y - ig.input.mouse.x;
				var rotation = Math.atan2(dy, dx);
				this.currentAnim.angle = rotation;
				
				var distanceSquared = (dx * dx + dy * dy);
				if (distanceSquared >= 9) {
					this.pos.x += dx * EASING;
					this.pos.y += dy * EASING;
				}

The problem is, the mouse-following entity completely ignores the collision map.

But when I replace pos.x, pos.y with vel.x, vel.y, then it collides properly.

I can live with that, but I don't understand why this is the case? Does setting position explicitly like this somehow override the collision testing?

1 decade ago by lTyl

You are setting the position of the entity, so if there is a collision tile directly to the left of the entity, and you set your new position to be in the center, then you end up being directly in the center of the collision tile. You never overrode the collision system perse (The collision methods should still be firing) but you will not see any visual changes to your entity nor will you appear to stop at a solid tile. The collision engine is saying "Hey, there is a collision here!" but it doesn't matter because you are already inside that tile and there is no code to warp you out of the collision tile, because it shouldn't happen. So instead you gracefully float through everything.

1 decade ago by Jarkko

So if I understand correctly, setting the position like this skips some numbers, so you can "tunnel" the entity directly on the tile, whereas manipulating velocity makes the movement continous, so the entity always stops at the tile.

But still there's a mystery (to me at least). When I put this code to the entity:
handleMovementTrace: function( res ){
		this.parent( res );
		if ( res.collision.x || res.collision.y ) {
			console.log("BUMP!");
		}
	}

I don't see anything in the console when I use position. With velocity on the other hand, console reports bumps. So this makes me think there is something else going there than mere appearance.

1 decade ago by Jarkko

All right, the same issue has been discussed before:
http://impactjs.com/forums/help/entity-passing-through-solid-with-mouse

1 decade ago by drhayes

If you read the code in entity.js for update you&039;ll see it call #ig.game.collisionMap.trace with two arguments, mx and my. These are calculated based on the velocity. If vel.x and vel.y are zero then these are too and no collision checks are performed.

You can follow the code path into collision-map.js to see what I mean.
Page 1 of 1
« first « previous next › last »