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 nuktas

Hi,

I am trying to get some very basic pathfinding to work in a 2d topdown game.
I want the enemies to walk along the x or y axis towards the player, whichever is the shortest. That is, if the enemy is 200 units away horizontally and 120 units away vertically, it should first move along the x axis until 120 units away, and from then on zig-zag towards the player.

Now, this works some of the time, but the monsters move very strangely - they sort of "overshoot" the player and move further along a diagonal axis than they should and then move like a pendulum behind my player while he moves away. I have
attached the relevant code below.

I would share my game for you too see, but I don't know how I can do that, really. Any help with regards to that would be appreciated.

Pathfinding logic
        getAction: function(entity) {
            this.entity = entity;
            var player = ig.game.getEntitiesByType('EntityPlayer')[0];
            var distance = this.entity.distanceTo(player);
            var x_dist = Math.abs(player.pos.x - this.entity.pos.x);
            var y_dist = Math.abs(player.pos.y - this.entity.pos.y);
            
            if (distance < 15) {
                return this.doAction(ig.ai.ACTION.Explode);
            }
            
            if (distance < 150) {
                if (x_dist > y_dist) {
                    if (this.entity.pos.x > player.pos.x) {
                        return this.doAction(ig.ai.ACTION.MoveLeft);
                    } else {
                        return this.doAction(ig.ai.ACTION.MoveRight);
                    }
                } else {
                    if (this.entity.pos.y > player.pos.y) {
                        return this.doAction(ig.ai.ACTION.MoveUp);
                    } else {
                        return this.doAction(ig.ai.ACTION.MoveDown);
                    }                        
                }
            }
            
            return this.doAction(ig.ai.ACTION.Rest);
        },
        doAction: function(action){
            this.lastAction = action;
            return action;
        }

Movement logic
        update: function(){
            var action = ai.getAction(this);
            switch (action) {
                case ig.ai.ACTION.Rest:
                    this.vel.x = 0;
                    this.vel.y = 0;
                    break;
                case ig.ai.ACTION.MoveLeft:
                    this.vel.x = -this.maxVel.x;
                    break;
                case ig.ai.ACTION.MoveRight:
                    this.vel.x = this.maxVel.x;
                    break;
                case ig.ai.ACTION.MoveUp:
                    this.vel.y = -this.maxVel.y;
                    break;
                case ig.ai.ACTION.MoveDown:
                    this.vel.y = this.maxVel.y;
                    break;
                case ig.ai.ACTION.Explode:
                    this.explode();
                    break;
                default:
                    this.vel.x = 0;
                    this.vel.y = 0;
                    break;
            }
            this.parent();
        }        

1 decade ago by nuktas

Apparently my edit created a new thread. Please disregard this one.

1 decade ago by Joncom

How to bake your game so that you can upload it online for us to have a look.

1 decade ago by nuktas

Thanks! I managed to get my code working, but I will take a look at the link.

Cheers!
Page 1 of 1
« first « previous next › last »