[SOLVED] Reset position after impulse

Hi there,

I was trying to achieve a destroy effect on my character(a bird, made out of cubes). Every cube gets destroyed by:

this.destroyableParts.forEach(function(entity) {
    entity.collision.enable = true;
    entity.rigidbody.enabled = true;

    entity.rigidbody.applyImpulse(rnd(-this.inpulse, this.inpulse), rnd(-this.inpulse, this.inpulse), rnd(-this.inpulse, this.inpulse));
}.bind(this));

All good, but, I am not able to reset the cubes position/rotation.

I tried with setLocalPosition(), setPosition(), teleport() but none seemed to work.
NOTE: Because I use this as a death effect, the rigid body gets enabled only when the player dies. It gets deactivated back when player respawns. All cubes have 0 velocity on respawn so it’s not like they keep moving.

Here is the restore function:

this.destroyableParts.forEach(function(entity) {
    entity.rigidbody.enabled = false;
    entity.collision.enabled = true;

    //entity.rigidbody.teleport(entity.script.destroyable_part.initialPos);
    entity.setLocalPosition(entity.script.destroyable_part.initialPos);
    entity.setLocalEulerAngles(entity.script.destroyable_part.initialRot);
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;

    console.log('x: ' + entity.script.destroyable_part.initialPos.x + ',y: ' + entity.script.destroyable_part.initialPos.y + ',z: ' + entity.script.destroyable_part.initialPos.z);
}.bind(this));

Initial Position/Rotation are stored like this:

this.initialPos = this.entity.getPosition();
this.initialRot = this.entity.getLocalEulerAngles();

Any thoughts? Thanks!

The problem is that you are storing a reference to the position which means initialPos is not the original position but the final position when you do your reset.

Try this instead

this.initialPos = this.entity.getPosition().clone();
this.initialRot = this.entity.getLocalEulerAngles();.clone();

In addition, you should use teleport if you are using the rigidbodies as you need to set the body position not the entity position.

Finally there is a spelling mistake ...collision.enabled = true.

Hope that helps.

Hey Dave, gonna give this a shot, didn’t realize that vectors gets referenced too. That spelling mistake happened while I was shortening to the code to post it here.

EDIT:
Cloning did the job, thanks! I had to use local position, teleport was kinda off.

entity.rigidbody.enabled = false;

entity.setLocalPosition(entity.script.destroyable_part.initialPos);
entity.setLocalEulerAngles(entity.script.destroyable_part.initialRot);
entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
entity.rigidbody.angularVelocity = pc.Vec3.ZERO;

Docs says that teleport is recommended, otherwise physics may get messy, but I suppose it works with local position because I am disabling rigidbody before that. Maybe teleport would’ve worked with rigidbody still enabled, but it already works this way and I need to disable rigidbody at that point anyway.