Added physics function to control sleeping behavior

This commit is contained in:
Lynix
2019-12-19 21:33:56 +01:00
parent 86fbae554c
commit 3b43f57192
9 changed files with 64 additions and 0 deletions

View File

@@ -338,6 +338,14 @@ namespace Nz
m_maxStepCount = maxStepCount;
}
void PhysWorld2D::SetSleepTime(float sleepTime)
{
if (sleepTime > 0)
cpSpaceSetSleepTimeThreshold(m_handle, cpFloat(sleepTime));
else
cpSpaceSetSleepTimeThreshold(m_handle, std::numeric_limits<cpFloat>::infinity());
}
void PhysWorld2D::SetStepSize(float stepSize)
{
m_stepSize = stepSize;

View File

@@ -194,6 +194,15 @@ namespace Nz
cpBodyEachArbiter(m_handle, RealCallback, &callback);
}
void RigidBody2D::ForceSleep()
{
m_world->RegisterPostStep(this, [](Nz::RigidBody2D* body)
{
if (cpBodyGetType(body->GetHandle()) == CP_BODY_TYPE_DYNAMIC)
cpBodySleep(body->GetHandle());
});
}
Rectf RigidBody2D::GetAABB() const
{
if (m_shapes.empty())
@@ -564,6 +573,17 @@ namespace Nz
cpBodyUpdateVelocity(m_handle, cpv(gravity.x, gravity.y), damping, deltaTime);
}
void RigidBody2D::Wakeup()
{
m_world->RegisterPostStep(this, [](Nz::RigidBody2D* body)
{
if (cpBodyGetType(body->GetHandle()) != CP_BODY_TYPE_STATIC)
cpBodyActivate(body->GetHandle());
else
cpBodyActivateStatic(body->GetHandle(), nullptr);
});
}
RigidBody2D& RigidBody2D::operator=(const RigidBody2D& object)
{
RigidBody2D physObj(object);