Dynamically inserting entities issues?

hello,

I just added some entities as children of an entity and it seems that transformations on parent are not applied to the newly created childrens.

In the exemple here, I’m creating x cylinders around a wheel

for (var i = 0; i < this.nbPies; ++i) {

    //computing location
    var angle = (i * this.increments) + (this.increments / 2.0);
    var v = new pc.Vec3(Math.cos(angle)*this.radius, Math.sin(angle)*this.radius, 0);

    //creating entity
    var entity = new pc.Entity();
    entity.setPosition(v);
 
    // Debugging visuals
    // add a new Model Component and add it to the Entity.
    entity.setLocalScale(0.1, 0.1, 0.1);
    entity.rotateLocal(90, 0, 0);
    entity.addComponent("model", {
        type: 'cylinder',
    });
    
    this.entity.root.addChild(entity);    
}

Then if call :

this.entity.rotateLocal(0, 0, this.rotationSpeed);

The created entities whill not rotate around the center.

Same thing if I disable the wheel.

app.root.findByName(‘Game’).findByName(‘Wheel’).enabled = false;

created children are still visible.

Thank you for your help

Hi @memleak,

I think your issue is that you aren’t adding them to the entity as children but to the Root of the hierarchy.

Try changing this:

this.entity.root.addChild(entity);

To this:

this.entity.addChild(entity);
1 Like

I tried it, but then I dont the children at all.

no it actually work! Thanks!!

1 Like

Now that I have my wheel turning, seems like I have issues with the physics.

I have a plane, with stretched box collision and a trigger script attached to it.

My dynamically inserted items have sphere collision and kinematic component attached

this.increments = Math.PI*2.0 / this.nbPies;
//create sphere to deal with pin
for (var i = 0; i < this.nbPies; ++i) {

    //computing location
    var angle = (i * this.increments) + (this.increments / 2.0);
    var v = new pc.Vec3(Math.cos(angle)*this.radius, Math.sin(angle)*this.radius, 0);

    //creating entity
    var entity = new pc.Entity();
    entity.setPosition(v);
    entity.addComponent("collision", {
        type: 'sphere',
        radius: '0.05',
    });
    
    entity.addComponent("rigidbody", {
        type: 'kinematic'
    });
    
    // Debugging visuals
    // add a new Model Component and add it to the Entity.
    entity.setLocalScale(0.1, 0.1, 0.1);
    entity.rotateLocal(90, 0, 0);
    entity.addComponent("model", {
        type: 'cylinder',
    });
    
    this.entity.addChild(entity);    
}

here is my script :
var Collider = pc.createScript(‘collider’);

// initialize code called once per entity
Collider.prototype.initialize = function() {

this.entity.collision.on('collisionstart', this.onCollisionStart, this);
this.entity.collision.on('collisionend', this.onCollisionEnd, this);
this.entity.collision.on('triggerenter', this.onTriggerEnter, this);

};

Collider.prototype.onCollisionStart = function (result) {
console.log(“you hit me!!”);
app.fire(‘pin:hit’);
};

Collider.prototype.onCollisionEnd = function (result) {
console.log(“you left me!!”);
app.fire(‘pin:free’);
};

Collider.prototype.onTriggerEnter = function(entity) {
console.log(“you triggered me!!”);
app.fire(‘pin:free’);
};

any ideas? Thanks

Just realized ammo wasnt installed. Works fine

1 Like