Why isn't my sprite / entity moving in a straight line?

My essence should move in a straight line of the mouse. It's close, but not quite yet. Here's a working demo to show you what I mean.

And here's a screenshot: Entity not moving in a straight line. Red represents the path the mouse has chosen. As you can see, the object does not take the same path.

Relevant code:

EntityPlayer = ig.Entity.extend({

    movementspeed: 400,

    update: function() {
        this.parent();
        this.move_toward_coord(ig.input.mouse.x, ig.input.mouse.y);
    },

    move_toward_coord: function(x, y) {
        var distance_to_target_x = x - this.pos.x - this.size.x / 2;
        var distance_to_target_y = y - this.pos.y - this.size.y / 2;
        if(Math.abs(distance_to_target_x) > 1 || Math.abs(distance_to_target_y) > 1) {
            this.vel.x = (distance_to_target_x > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_x) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
            this.vel.y = (distance_to_target_y > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_y) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
        } else {
            this.vel.y = 0;
            this.vel.x = 0;
        }
    }

});

      

I suspect there is something wrong with the method move_to_coord

, but after setting up for too many hours, I'm still not sure what it is.

Why isn't the ship moving in a straight line?

+3


source to share


1 answer


Ughh !! I figured it out literally a few seconds after I posted it. Sorry, I'm to blame. This was due to a property called maxVel

, which limited the speed to a speed x

or y

sometimes to a different speed than a different ..> & L;



+4


source







All Articles