Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RigidBody::setIsActive / RigidBody::setIsSleeping behaviour #284

Closed
Casqade opened this issue Oct 28, 2022 · 6 comments
Closed

RigidBody::setIsActive / RigidBody::setIsSleeping behaviour #284

Casqade opened this issue Oct 28, 2022 · 6 comments
Assignees
Labels

Comments

@Casqade
Copy link

Casqade commented Oct 28, 2022

I'm slightly confused with sleeping/active body state concepts in the context of their implementation.
e.g. RigidBody::setIsActive should either:

  • Put the body to sleep (via RigidBody::setIsSleeping), then deactivate it
  • Or awake the body, then activate it

But both the code and the comment in RigidBody::setIsSleeping suggest that "If the body is not active, do nothing (it is sleeping)". That means when we try to activate the body with body->setIsActive(true), the simplified call flow would be as follows:

  1. RigidBody::setIsSleeping(false) (RigidBody.cpp:991 - body is not yet active, so the code returns without awaking it)
  2. CollisionBody::setIsActive(true) (finally activate the body)

In result, our body has been activated, but not awaken. Is it an intended behaviour or I'm missing something ?

@DanielChappuis
Copy link
Owner

Hello,

Sorry for my late answer. The idea is that a body is "active" when it is simulated by the engine (the engine will do something with the body) and "not active" when it is not simulated. On the other side, a body is "sleeping" when it simulated (active) but is not moving for some time and has no forces or constraints on it. Therefore, it's an engine optimization not to compute things for 'sleeping' bodies.

Therefore, a body should be deactivated when you do want the physics engine to updated anything with your body (it's like removing it from the world for some time). This feature is probably not used a lot.

Putting a body to sleep of waking it up is automatically done by the physics engine most of the time. You should not have to do it yourself but it happens that you want to awake a body that is sleeping a some point. To do that you can wake up a body using the RigidBody::setIsSleeping(false) method.

I hope that it is more clear.

@Casqade
Copy link
Author

Casqade commented Nov 14, 2022

I hope that it is more clear.

Well, it is clear from logical point of view, but my question is more related to implementation details. I expect RigidBody::setIsActive(true) to automatically wake up the body. However, that is not the case:

rp3d::RigidBody* body = world.createRigidBody({});

body->setIsActive(false);
assert( body->isActive() == false );
assert( body->isSleeping() == true ); <-- ok

body->setIsActive(true);
assert( body->isActive() == true );
assert( body->isSleeping() == false ); <-- assertion failure

In my initial post I described why it is happening, but I'm still not sure whether this is the intended behaviour. Looks like a bug or 'logical inconsistency' to me, actually...

@DanielChappuis
Copy link
Owner

In my opinion, this is correct behavior.

A body is 'inactive' means that the engine should not consider the body at all (like it has been temporarily removed from the world). Sleeping means that the body was not moving and there are no forces or constraints on it at the moment and therefore should not currently move. Consider an body that is sleeping (a body resting on the floor for instance) that has be made inactive. If we set the inactive body to be active again, it simply means the physics engine should start considering this body again but it doesn't necessarily means that the body has now constraints and forces on it. When we set this body as being active again, we do not want the physics engine to wake it up directly because it is sill resting alone on the floor but we only want the physics engine to start considering this body again. It means that the engine will check, the next frame, if the body is still sleeping or not and wake it up accordingly.

@Casqade
Copy link
Author

Casqade commented Nov 17, 2022

the engine will check, the next frame, if the body is still sleeping or not

If you're completely sure it works as described, then I don't have any problems with it. The confusion arose from this (probably unfixed?) issue and my own experience.

@DanielChappuis
Copy link
Owner

Yes it is the theory of how it should work at least and yes the issue #235 is not fixed yet.

@DanielChappuis DanielChappuis added this to the Release v1.0.0 milestone Dec 1, 2023
@DanielChappuis
Copy link
Owner

The issue #235 is now fixed in version v0.10.0 of the library. Thanks a lot for reporting the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants