Problems with restart button

Dear,
I have a problem, it happens that I have a box that crashes against a wall and other boxes, this is through an impulse applied to the box.
My problem is that by applying the same impulse, the box does not always end up in the same place.
What can be?

My code is as follows:

For the box

var Dado1 = pc.createScript(‘dado1’);

// initialize code called once per entity
Dado1.prototype.initialize = function() {
var app = this.app;
this.initialPos = this.entity.getPosition().clone();
this.initialRot = this.entity.getRotation().clone();

app.on('dado1:reset', function () {
    this.entity.setPosition(this.initialPos);
}, this);

app.on('dado1:activarImpulso', function () {
    this.entity.rigidbody.applyImpulse(0, 0, -20);
}, this);

};

Dado1.prototype.update = function(dt) {

};

To activate the pulse:

var BtnStates = pc.createScript(‘btnStates’);

// initialize code called once per entity
BtnStates.prototype.initialize = function() {
var app = this.app;

this.entity.element.on('mouseup', this.onRelease, this);
this.entity.element.on('touchstart', this.onPress, this);

app.on('btnStates:ocultar', function () {
    this.entity.enabled = false;
}, this);
app.on('btnStates:visible', function () {
    this.entity.enabled = true;
}, this);

};

BtnStates.prototype.onRelease = function (event) {
var app = this.app;
app.fire(‘dado1:activarImpulso’);
};

Thank you

I’m guessing that it looks similar but not exactly the same? Physics uses a lot of randomness when working out what to do in the case of collisions, especially multiple collisions occurring at the same time. At the end of the day you would use Ammo (like here) or PhysX (in Unity) to do real world scientific simulations with stable outputs, they run quickly for game engines and produce a realistic output.

Does that sound like it might be the case or is it more extreme? Do you need it to be exactly the same for some reason, or could we come up with a way of circumventing that?

Hmm… it’s possible that ammo.js isn’t deterministic and/or there is something in the engine where a fixed timestep isn’t used (potentially here?)

If your entity has a rigidbody your should use teleport instead of setPosition

1 Like

thank you very much for your help, I worked perfect

I have another problem and I want to see if you can help me.

I have 3 cubes in a high base, I throw them to a base below using a impulse and these each time falls in different places.

What can be?

impulse apply:

this.entity.rigidbody.applyImpulse(0, 0, -20);

I’m not sure about that. Are all the conditions exactly the same when you try it? Are there any other objects in a slightly different position?

If ammo is deterministic which I’m not sure it is, then you have to make sure that everything is exactly the same every time.

Also you probably need to make sure that there are no other forces or velocities on these objects from previous frames. Might even be worth trying to clone these entities and destroy the old ones.

Thank you very much for your help, I did everything and it does not work for me.
Each time I throw the dice they are different.
I attach the link so that you can see it

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

@vaios @will Is there a reason that _updateKinematic is not called with this.fixedTimeStep here and stepSimulation is called with this.fixedTimeStep?

https://github.com/playcanvas/engine/blob/master/src/framework/components/rigid-body/system.js#L510

If you look into _updateKinematic you’ll see it’s just PlayCanvas updating the transform of an Entity using regular dt. It doesn’t do any fixed timestep steps in there.

On the other hand stepSimulation is called on ammo which I believe performs additional update loops internally depending on the difference between the dt that is passed to it vs the fixed dt.

In any case I don’t think that’s the problem here because the dice are not kinematic but dynamic. My guess is that ammo is not deterministic.

Well judging from your example there since it’s different everytime you refresh the application I’m guessing that ammo is not deterministic. Ammo is a javascript port of the Bullet physics engine. You could do some research into how you can make Bullet deterministic and if you find a method then we have to see if that’s exposed in ammo.