[SOLVED] How to listen rigidbody movestart / moveend event?

I wonder if there is any way to listen rigidbody’s move start and move end event ?
I can’t find any thing about this in the API docs.

Hmm… The closest I think you can get is to know when a body is ‘sleeping’ (not the same as when a body stops moving though) via the isActive() function.

If you need to know when a body stops moving, chances are that it will be something that you have to detect yourself.

I test isActive just now, but no matter the rigidbody is moving or not, isActive always return 1…

and my playcanvas version is 0.210.0-dev 0cff9d9

Hm… Just tried a test project here: https://playcanvas.com/editor/scene/520993

And it’s returning false when it is sleeping (remember that ‘sleeping’ is NOT the same as ‘not moving’).

You can check if an entity is not moving by just checking the position every frame. If it hasn’t moved beyond a threshold, than it is ‘not moving’. A generic script can be written so it sends an event to the entity if you would like to keep that kind of functionality/structure.

You could have a script on your rigid body entities that, every frame, stores off the position. If the position changes over two frames (and it didn’t the frame before), do:

this.app.fire('bodymovestart', this.entity);

If the position doesn’t change over two frames (and it did the frame before), do:

this.app.fire('bodymoveend', this.entity);

You can detect these events with:

this.app.on('bodymovestart', function (e) {
    console.log('Entity ' + e.name + ' started to move');

Would that work?

I think check the current position and last position will work.

One more question:

In fact, in my use case, I apply force to the rigidbody to stick it on the wall, so that the user can move the rigidbody on the wall.

Is that related to the rigidbody.isActive() always return 1 ?

The PlayCanvas isActive function just calls the same function in Ammo.js:

The code in Ammo (Bullet) that implements isActive is:


So essentially, if the body is not sleeping or disabled, it will return true. A body may come to rest but it can still remain active for some time until it goes to sleep and becomes inactive (the physics engine needs to be confident that it is right to sleep a body).

I think, by default, a body will go to sleep after it comes to rest. But notice how the engine disables this behavior for kinematic bodies:

Basically, if forces continue to act on a body, even if that body is not moving, it might not go to sleep. But I’m not 100% sure about this because this is Ammo/Bullet behavior and not PlayCanvas specifically.

1 Like

If you are always applying a force to a rigidbody, it will not sleep and therefore isActive will return true

1 Like