Update physics playground (restore grab with both jolt and bullet)

This commit is contained in:
SirLynix 2023-03-30 13:24:17 +02:00 committed by Jérôme Leclercq
parent efc81ff508
commit 74bfd8fa4e
1 changed files with 13 additions and 10 deletions

View File

@ -272,7 +272,11 @@ int main()
NazaraSlot(Nz::WindowEventHandler, OnMouseMoved, cameraMove);
NazaraSlot(Nz::WindowEventHandler, OnMouseMoved, grabbedObjectMove);
//std::optional<Nz::PivotConstraint3D> grabConstraint;
#if USE_JOLT
std::optional<Nz::JoltPivotConstraint3D> grabConstraint;
#else
std::optional<Nz::BulletPivotConstraint3D> grabConstraint;
#endif
auto mouseMoveCallback = [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
{
@ -299,21 +303,20 @@ int main()
Nz::Vector3f from = cameraComponent.Unproject({ float(event.x), float(event.y), 0.f });
Nz::Vector3f to = cameraComponent.Unproject({ float(event.x), float(event.y), 1.f });
#if USE_JOLT
Nz::JoltPhysics3DSystem::RaycastHit lastHitInfo;
auto callback = [&](const Nz::JoltPhysics3DSystem::RaycastHit& hitInfo) -> std::optional<float>
#else
Nz::BulletPhysics3DSystem::RaycastHit lastHitInfo;
#endif
auto callback = [&](const decltype(lastHitInfo)& hitInfo) -> std::optional<float>
{
if (hitInfo.hitEntity == boxEntity)
{
Nz::Vector3f dirToCenter = Nz::Vector3f::Zero() - hitInfo.hitPosition;
dirToCenter.Normalize();
#ifdef USE_JOLT
if (Nz::Vector3f::DotProduct(dirToCenter, (hitInfo.hitPosition - from).Normalize()) < 0.f)
return std::nullopt;
#else
if (Nz::Vector3f::DotProduct(dirToCenter, hitInfo.hitNormal) < 0.f)
return std::nullopt;
#endif
}
lastHitInfo = hitInfo;
@ -325,7 +328,7 @@ int main()
{
if (lastHitInfo.hitBody && lastHitInfo.hitEntity != boxEntity)
{
//grabConstraint.emplace(*lastHitInfo.hitBody, lastHitInfo.hitPosition);
grabConstraint.emplace(*lastHitInfo.hitBody, lastHitInfo.hitPosition);
//grabConstraint->SetImpulseClamp(30.f);
grabbedObjectMove.Connect(eventHandler.OnMouseMoved, [&, body = lastHitInfo.hitBody, distance = Nz::Vector3f::Distance(from, lastHitInfo.hitPosition)](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
@ -334,7 +337,7 @@ int main()
Nz::Vector3f to = cameraComponent.Unproject({ float(event.x), float(event.y), 1.f });
Nz::Vector3f newPosition = from + (to - from).Normalize() * distance;
//grabConstraint->SetSecondAnchor(newPosition);
grabConstraint->SetSecondAnchor(newPosition);
});
}
else
@ -349,7 +352,7 @@ int main()
{
if (event.button == Nz::Mouse::Left)
{
//grabConstraint.reset();
grabConstraint.reset();
cameraMove.Disconnect();
grabbedObjectMove.Disconnect();
}