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 coreysnyder

In my game Zombie Ops I'm not at all happy with how the zombies movement is done. I want it to be more natural and not have zombies walking 1 direction and then on their directionChange() method doing a 180. They currently move using the basic entity this.velocity.x and this.vel.y;

The count at which they change direction is a using a timer every 2 seconds.

What I want when this method is called is to...
* Determine the current angle based on Velocity X/Y
* Modify that angle by a random number of degrees between say.. 10 and 30
* Convert that angle back into X/Y Velocities

I believe the end result would be my zombies walking more... zombie like. Where once they start moving in a direction they would only making some changes to that direction. They should still be able to pull a 180, but not in a single step, but rather after many steps.

To anyone who can help, I'd be happy to provide iOS promo codes for some of my games. Also a huge "Thank you!"

Cheers,
Corey

1 decade ago by Joncom

First of all:
// Get the angle in radians based on entity current velocity.
getAngleFromVelocity: function() {
    var angle = Math.atan2(this.vel.y, this.vel.x);
    return angle;
}

Then:
var angle = this.getAngleFromVelocity();
// Add random degrees between 10-30.
var degreesToChange = Math.floor(Math.random()*20) + 10;
// Add to current angle, converting degrees to radians.
angle += (degreesToChange * Math.PI / 180);

And finally:
// Set the velocity using an angle to define direction.
setVelocityByAngle: function(angle, velocity) {
    this.vel.x = Math.cos(angle) * velocity;
    this.vel.y = Math.sin(angle) * velocity;
}

Good luck with your game!

1 decade ago by coreysnyder

@Joncom - Thanks for the help man. You saved me lots of debug time. I noticed initially that the zombies were walking in clockwise circles so I implemented a direction randomness seen here:

// Add random degrees between 10-30.
var degreesToChange = Math.floor(Math.random()*60) + 10;
// Add to current angle, converting degrees to radians.
var direction = (Math.ceil(Math.random()*2) % 2 == 0) ? -1 : +1;
angle = angle + (direction * (degreesToChange * Math.PI / 180) );

I also have a mode where the zombies are on attack so they need to move directly towards the player, and evade where they move away from the player so I implemented this:
getAngleToPlayer: function(){
            var playerPos =  ig.game.player.pos;

            var awayFromPlayer = (this.state == EVADE) ? true : false;

            var dx = (playerPos.x - this.pos.x) * (awayFromPlayer ? -1 : 1);
            var dy = (playerPos.y - this.pos.y) * (awayFromPlayer ? -1 : 1);


            var angleToPlayer = Math.atan2(dy, dx);

            return angleToPlayer;
        },


Then I can pass that instead of angle to 'setVelocityByAngle()' and it results in the desired effect.

1 decade ago by Joncom

Optionally you could use angleTo as well.

// Within zombie entity.
var angleToPlayer = this.angleTo(ig.game.player);
// Add 180 degrees only if state == EVADE.
var finalAngle = angleToPlayer + ( this.state == EVADE ? Math.PI : 0 );
Page 1 of 1
« first « previous next › last »