10/15/2014

enemies and collision

The dungeon crawler I showed to you in the last post is still missing many vital parts, one of them is enemies.

So for now I introduced this guy (and some friends):

evil clone
They are of course quickly reskinned versions of the player avatar. But they do the trick for now.

Making them move towards the player was easy - because the player could already move towards a target. For the enemies the player is the target (instead of the cursor).

Collision is more interesting: It doesn't look very good when the enemies run through each other and through the player. So now every object (enemies and the player) has a collision radius.

It took me this evening to figure out how to handle the collisions exactly - first approach was to just stop movement as soon as the spheres overlap any other sphere. But this way both spheres are stuck in each other.
Improvement one: Not only stop, but also move back the last movement. (substract the movement vector from the current position). This way the spheres stay outside of each other.

This kind of works, but as soon as there are more enemies than one approaching from the same direction, the first one reaches the player, the second one stucks himself behind the first one.

I played around with different ideas, calculate the angle to the target and to the collision target and trying to figure out how to move the enemy around the other. But I had to throw stuff away again and again, until I came up with this simple solution:

var collisionDirection = collisionObject.position.clone().sub(this.position);
collisionDirection.normalize().multiplyScalar(this.speed);
this.mesh.position.add(this.speedVector.clone().sub(collisionDirection));


Instead of subtracting the complete Movementvector in the case of collision, I only substract the "part" of the vector that goes into the sphere. This way  (if the enemy does not plan to go straight through the sphere) there is still movement and it looks like they go around the sphere.

The effect will be demonstrated in the next "show-and-tell"-Video.







As a small teaser, I leave you with this:

Yes, there is a leaf on the stick and yes this was a request by my wife.



No comments:

Post a Comment