Rigid Body -> Why "Box2" dont stop when hit "Box"

Hello, and sorry for asking i am a newbie to playcanvas.

I can move the entity “Box2” but when he hit “Box” goes through it, why doesn’t it stop?

https://playcanvas.com/project/937318

“Box” and “Box2” are rigid bodys and collisions.

Thank you for help!

“Box2” → Script

var Movement = pc.createScript('movement');

Movement.attributes.add('speed', { type: 'number', default: 1 });

// initialize code called once per entity
Movement.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);

   
};

// update code called every frame
Movement.prototype.update = function(dt) {

    var left = this.app.keyboard.isPressed(pc.KEY_LEFT);
    var right = this.app.keyboard.isPressed(pc.KEY_RIGHT);
    var up = this.app.keyboard.isPressed(pc.KEY_UP);
    var down = this.app.keyboard.isPressed(pc.KEY_DOWN);

    if (left) {
        this.entity.translate(-this.speed * dt, 0, 0);
    }
    if (right) {
        this.entity.translate(this.speed * dt, 0, 0);
    }
    if (up) {
        this.entity.translate(0, 0, -this.speed * dt);
    }
    if (down) {
        this.entity.translate(0, 0, this.speed * dt);
    }
};

// collision logic
Movement.prototype.onCollisionStart = function (result) {
    if (result.other.name === 'Box') {
        console.log('collision success');
        this.entity.destroy();
    }
};

// swap method called for script hot-reloading
// inherit your script state here
Movement.prototype.swap = function(old) {

};

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/

Hello, and welcome to PlayCanvas!

It is because both of your boxes are set as static in the physics world. I would recommend to read the guide on how to use physics in your project:

https://developer.playcanvas.com/en/user-manual/physics/physics-basics/

Thank you very much, i change “Box2” to Kinematic and now working. But how can i make the “Box2” stop when hit “Box”? Now → When “Box2” hit “Box”, the “Box” go on right i want the “Box2” stop in “Box”.

You could add a trigger volume for a static box and listen for the triggerenter event. Once the box enters the volume, it will fire that event. You would catch it, and stop moving the box then. You can study this example, to learn how to use trigger volumes and react to physics events:

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

1 Like