[SOLVED] Rigidbody Collisions not being detected on cloned entities

https://playcanvas.com/editor/scene/1201252

Hopefully a simple problem, I’m still learning the API and messing around here, so it shouldn’t be too difficult.

I start with a box in my scene which I clone in a 10x10 square to make a ground, the original box has a collision component and a rigidbody component, and I’ve checked and the cloned boxes also appear to start with collision components and rigidbody components. Then when the ball falls on the boxes it doesn’t collide or detect a collision, which I’ve checked with console.log()'s.

Here’s the code that’s meant to clone the boxes

   if(this.entity.parent.children.length == 7){
        var blocks = [];
        for(var x = 0; x < 10; x++){
            blocks[x] = [];
            for(var z = 0; z < 10; z++){
                blocks[x][z] = this.entity.clone();
                this.entity.parent.addChild(blocks[x][z]);
                blocks[x][z].setPosition(x, 1, z);
            }
        }
        this.entity.destroy();
    }

I’ve tried adding a new collision component and rigidbody component and I got warning messages that those components were already a part of the cloned entities, and also they didn’t work. My working theory is that while the entities are cloned and moved visibly the rigidbodies are for some reason not being moved.

I could use any help I can get, thanks.

Hi @Ethan_Hughes and welcome,

Your code looks quite fine, the issue I think is that when cloning an entity the physics body of that entity isn’t added to the physics simulation. I am starting to feel that this is a PlayCanvas bug and shouldn’t happen (@yaustar what do you think? ).

The workaround right now is to trigger a state change on the rigidbody component, that is as simple as setting it to false and immediately back to true. Here is me doing that in the console and it’s working:

In your code you can do this:

const newEntity = blocks[x][z];
newEntity = this.entity.clone();
this.entity.parent.addChild(newEntity);
newEntity.setPosition(x, 1, z);

newEntity.rigidbody.enabled = false;
newEntity.rigidbody.enabled = true;

Strange, it’s working in this project: https://playcanvas.com/editor/scene/480301

Try changing:

newEntity.setPosition(x, 1, z);

To

newEntity.rigidbody.teleport(x, 1, z);
2 Likes

Confirmed, it’s not a PlayCanvas bug, the issue is that a static rigidbody is created as soon as the entity is added to the scene hierarchy and since it’s static you can’t really move it using setPosition().

Either using teleport() per @yaustar’s suggestion or changing the order the code executes fixes that:

                blocks[x][z] = this.entity.clone();
                blocks[x][z].setPosition(x, 1, z);                
                this.entity.parent.addChild(blocks[x][z]);
2 Likes

Thanks, that has worked. Thank you for the help.

1 Like