How to use the matter-js.Vector.sub function in matter-js

To help you get started, we’ve selected a few matter-js examples, based on popular ways it is used in public projects.

github cyclegtx / colyseus-iog-state-sync / Games / Example / Entities / Character.ts View on Github external
// if(this.body.velocity.x != 0 || this.body.velocity.y != 0){
        //     console.log(this.body.velocity)

        // }
        if(this.action == "dead"){
            return;
        }

        if(this.mlb === true && this.entityInHand != null){
            this.action = "hide";
        }else if(this.mlb === false && this.action == "hide"){
            this.action = "idle";
        }

        this.mouseDistance = Vector.sub(this.mousePosition,this.body.position);
        // Body.setAngle(this.body, Vector.angle(Vector.create(0, 1), this.mouseDistance));

        if(this.action != "hide"){
            if (this.dirX != 0 || this.dirY != 0) {
                Body.translate(this.body, Vector.mult(Vector.create(this.dirX, this.dirY), this.speed));
                // Body.setVelocity(this.body,Vector.create(this.dirX*20,this.dirY*20));
                this.action = "move";
            } else {
                this.action = "idle";
            }
        }

        if (this.mrb === true && this.entityInHand != null) {
            this.throw();
        }
github cyclegtx / colyseus-iog-state-sync / Games / Example / Entities / Character.ts View on Github external
throw() {
        if (this.entityInHand != null) {
            let b: Body = this.entityInHand.body;
            let dir = Vector.normalise(Vector.sub(this.mousePosition, this.body.position));


            (this.entityInHand as PhysicsEntity).enablePhysics();

            this.entityInHand.action = "throw";
            Body.setPosition(b, Vector.add(this.body.position, Vector.mult(dir, 10)));
            // Body.setVelocity(b, Vector.mult(dir,3));
            Body.applyForce(b, b.position, Vector.mult(dir, 0.5));
            this.entityInHand = null;
            this.action = "idle";
        }
    }
github cyclegtx / colyseus-iog-state-sync / Games / Example / Entities / AIPlayer.ts View on Github external
bodiesInView.sort((a: Body, b: Body) => {
            return Vector.magnitudeSquared(Vector.sub(a.position, this.character.body.position)) - Vector.magnitudeSquared(Vector.sub(b.position, this.character.body.position));
        });
github cyclegtx / colyseus-iog-state-sync / Games / Example / Entities / AIPlayer.ts View on Github external
if(this.AIAction == "idle"){
            this.findSomethingToDo();
        }else if(this.AIAction == "wander"){
            this.dirction = Vector.sub(this.mousePosition,this.character.body.position);
            if(Vector.magnitudeSquared(this.dirction) > 1){
                this.dirction = Vector.normalise(this.dirction);
                this.character.dirX = this.dirction.x;
                this.character.dirY = this.dirction.y;
            }else{
                this.character.dirX = 0;
                this.character.dirY = 0;
            }
        }else if(this.AIAction == "walkToTarget"){
            if(this.targetPosition != null){
                this.dirction = Vector.sub(this.targetPosition, this.character.body.position);
                if (Vector.magnitudeSquared(this.dirction) > 1) {
                    this.dirction = Vector.normalise(this.dirction);
                    this.character.dirX = this.dirction.x;
                    this.character.dirY = this.dirction.y;
                } else {
                    this.character.dirX = 0;
                    this.character.dirY = 0;
                }
            }
        }

    }
github cyclegtx / colyseus-iog-state-sync / Games / Example / Entities / AIPlayer.ts View on Github external
update(dt){
        super.update(dt);
        if(this.timeSinceLastThink < this.timeBetweenThink){
            this.timeSinceLastThink += dt;
        }else{
            this.AIAction = "idle";
        }

        if(this.character.action == "dead"){
            return;
        }

        if(this.AIAction == "idle"){
            this.findSomethingToDo();
        }else if(this.AIAction == "wander"){
            this.dirction = Vector.sub(this.mousePosition,this.character.body.position);
            if(Vector.magnitudeSquared(this.dirction) > 1){
                this.dirction = Vector.normalise(this.dirction);
                this.character.dirX = this.dirction.x;
                this.character.dirY = this.dirction.y;
            }else{
                this.character.dirX = 0;
                this.character.dirY = 0;
            }
        }else if(this.AIAction == "walkToTarget"){
            if(this.targetPosition != null){
                this.dirction = Vector.sub(this.targetPosition, this.character.body.position);
                if (Vector.magnitudeSquared(this.dirction) > 1) {
                    this.dirction = Vector.normalise(this.dirction);
                    this.character.dirX = this.dirction.x;
                    this.character.dirY = this.dirction.y;
                } else {