diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 9abf60362..11cc1d5ed 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -228,7 +228,7 @@ void EditorComponent::Load() Scene& scene = wiRenderer::GetScene(); cinemaModeCheckBox = new wiCheckBox("Cinema Mode: "); - cinemaModeCheckBox->SetSize(XMFLOAT2(20, 20)); + cinemaModeCheckBox->SetSize(XMFLOAT2(18, 18)); cinemaModeCheckBox->SetPos(XMFLOAT2(screenW - 55 - 860 - 120, 0)); cinemaModeCheckBox->SetTooltip("Toggle Cinema Mode (All HUD disabled). Press ESC to exit."); cinemaModeCheckBox->OnClick([&](wiEventArgs args) { @@ -242,6 +242,17 @@ void EditorComponent::Load() }); GetGUI().AddWidget(cinemaModeCheckBox); + wiCheckBox* physicsEnabledCheckBox = new wiCheckBox("Physics Enabled: "); + physicsEnabledCheckBox->SetSize(XMFLOAT2(18, 18)); + physicsEnabledCheckBox->SetPos(XMFLOAT2(screenW - 55 - 860 - 120, 22)); + physicsEnabledCheckBox->SetTooltip("Toggle Physics Engine On/Off"); + physicsEnabledCheckBox->OnClick([&](wiEventArgs args) { + Scene& scene = wiRenderer::GetScene(); + scene.SetPhysicsEnabled(args.bValue); + }); + physicsEnabledCheckBox->SetCheck(wiRenderer::GetScene().IsPhysicsEnabled()); + GetGUI().AddWidget(physicsEnabledCheckBox); + wiComboBox* renderPathComboBox = new wiComboBox("Render Path: "); renderPathComboBox->SetSize(XMFLOAT2(100, 20)); diff --git a/WickedEngine/wiPhysicsEngine_Bullet.cpp b/WickedEngine/wiPhysicsEngine_Bullet.cpp index c7d26d79e..ca50281c3 100644 --- a/WickedEngine/wiPhysicsEngine_Bullet.cpp +++ b/WickedEngine/wiPhysicsEngine_Bullet.cpp @@ -21,7 +21,7 @@ using namespace wiSceneSystem; namespace wiPhysicsEngine { - btVector3 gravity(0, -110, 0); + btVector3 gravity(0, -10, 0); int softbodyIterationCount = 5; btCollisionConfiguration* collisionConfiguration = nullptr; btCollisionDispatcher* dispatcher = nullptr; @@ -39,17 +39,18 @@ namespace wiPhysicsEngine void Initialize() { // collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration. - collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration(); + collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration; // use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) dispatcher = new btCollisionDispatcher(collisionConfiguration); // btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep. - overlappingPairCache = new btDbvtBroadphase(); + overlappingPairCache = new btDbvtBroadphase; // the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) solver = new btSequentialImpulseConstraintSolver; + //dynamicsWorld = new btSimpleDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration); //dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration); dynamicsWorld = new btSoftRigidDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration, softBodySolver); @@ -59,12 +60,14 @@ namespace wiPhysicsEngine dynamicsWorld->setGravity(gravity); - ((btSoftRigidDynamicsWorld*)dynamicsWorld)->getWorldInfo().air_density = (btScalar)0.0; - ((btSoftRigidDynamicsWorld*)dynamicsWorld)->getWorldInfo().water_density = 0; - ((btSoftRigidDynamicsWorld*)dynamicsWorld)->getWorldInfo().water_offset = 0; - ((btSoftRigidDynamicsWorld*)dynamicsWorld)->getWorldInfo().water_normal = btVector3(0, 0, 0); - ((btSoftRigidDynamicsWorld*)dynamicsWorld)->getWorldInfo().m_gravity.setValue(gravity.x(), gravity.y(), gravity.z()); - ((btSoftRigidDynamicsWorld*)dynamicsWorld)->getWorldInfo().m_sparsesdf.Initialize(); + btSoftRigidDynamicsWorld* softRigidWorld = (btSoftRigidDynamicsWorld*)dynamicsWorld; + btSoftBodyWorldInfo& softWorldInfo = softRigidWorld->getWorldInfo(); + softWorldInfo.air_density = (btScalar)0.0; + softWorldInfo.water_density = 0; + softWorldInfo.water_offset = 0; + softWorldInfo.water_normal = btVector3(0, 0, 0); + softWorldInfo.m_gravity.setValue(gravity.x(), gravity.y(), gravity.z()); + softWorldInfo.m_sparsesdf.Initialize(); } void CleanUp() @@ -114,7 +117,7 @@ namespace wiPhysicsEngine switch (physicscomponent.shape) { case RigidBodyPhysicsComponent::CollisionShape::BOX: - shape = new btBoxShape(S * 0.5f); + shape = new btBoxShape(S); break; case RigidBodyPhysicsComponent::CollisionShape::SPHERE: @@ -176,9 +179,10 @@ namespace wiPhysicsEngine if (shape != nullptr) { - shape->setMargin(btScalar(0.01)); + // Use default margin for now + //shape->setMargin(btScalar(0.01)); - btScalar mass(physicscomponent.mass); + btScalar mass = physicscomponent.mass; bool isDynamic = (mass != 0.f && !physicscomponent.IsKinematic()); @@ -455,7 +459,8 @@ namespace wiPhysicsEngine } } - dynamicsWorld->stepSimulation(dt, 10); + // Scale dt, otherwise simulation is just too slow (todo: review) + dynamicsWorld->stepSimulation(dt * 60, 10); wiProfiler::GetInstance().EndRange(); // Physics } diff --git a/WickedEngine/wiSceneSystem.cpp b/WickedEngine/wiSceneSystem.cpp index 90f5c1808..b594545bb 100644 --- a/WickedEngine/wiSceneSystem.cpp +++ b/WickedEngine/wiSceneSystem.cpp @@ -835,7 +835,10 @@ namespace wiSceneSystem RunAnimationUpdateSystem(animations, transforms, dt); - wiPhysicsEngine::RunPhysicsUpdateSystem(weather, transforms, meshes, objects, rigidbodies, softbodies, dt); + if (IsPhysicsEnabled()) + { + wiPhysicsEngine::RunPhysicsUpdateSystem(weather, transforms, meshes, objects, rigidbodies, softbodies, dt); + } RunTransformUpdateSystem(transforms); diff --git a/WickedEngine/wiSceneSystem.h b/WickedEngine/wiSceneSystem.h index 9fe4a4721..f93933ba8 100644 --- a/WickedEngine/wiSceneSystem.h +++ b/WickedEngine/wiSceneSystem.h @@ -894,6 +894,17 @@ namespace wiSceneSystem XMFLOAT4 waterPlane = XMFLOAT4(0, 1, 0, 0); WeatherComponent weather; + enum FLAGS + { + EMPTY = 0, + PHYSICS_ENABLED = 1 << 0, + }; + uint32_t _flags = PHYSICS_ENABLED; + + inline void SetPhysicsEnabled(bool value) { if (value) { _flags |= PHYSICS_ENABLED; } else { _flags &= ~PHYSICS_ENABLED; } } + + inline bool IsPhysicsEnabled() const { return _flags & PHYSICS_ENABLED; } + // Update all components by a given timestep (in seconds): void Update(float dt); // Remove everything from the scene that it owns: diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 41e6db95b..fea4d1683 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 21; // minor bug fixes, alterations, refactors, updates - const int revision = 10; + const int revision = 11; long GetVersion() diff --git a/models/physics_test.wiscene b/models/physics_test.wiscene new file mode 100644 index 000000000..0c573fdbe Binary files /dev/null and b/models/physics_test.wiscene differ