diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 458a1b8..aa4fc6b 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -1,4 +1,23 @@ #include "PrecompiledHeader.h" +#include +// We’re not using anything product specific yet. We undef these so we don’t get the usual +// product initialization for the products. +#undef HK_FEATURE_PRODUCT_AI +//#undef HK_FEATURE_PRODUCT_ANIMATION +#undef HK_FEATURE_PRODUCT_CLOTH +#undef HK_FEATURE_PRODUCT_DESTRUCTION_2012 +#undef HK_FEATURE_PRODUCT_DESTRUCTION +#undef HK_FEATURE_PRODUCT_BEHAVIOR +#undef HK_FEATURE_PRODUCT_PHYSICS_2012 +//#undef HK_FEATURE_PRODUCT_PHYSICS +// Also we’re not using any serialization/versioning so we don’t need any of these. +#define HK_EXCLUDE_FEATURE_SerializeDeprecatedPre700 +#define HK_EXCLUDE_FEATURE_RegisterVersionPatches +#define HK_EXCLUDE_FEATURE_RegisterReflectedClasses +#define HK_EXCLUDE_FEATURE_MemoryTracker +// This include generates an initialization function based on the products +// and the excluded features. +#include #include "PhysicsSystem.h" #include "World.h" @@ -7,11 +26,43 @@ Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world) { + hkMemorySystem::FrameInfo finfo(500 * 1024); // Allocate 500KB of Physics solver buffer + hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); + hkBaseSystem::init(memoryRouter, HavokErrorReport); + + { + hkpWorldCinfo worldInfo; + worldInfo.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); + worldInfo.m_gravity = hkVector4(0.0f, -9.8f, 0.0f); + worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_FIX_ENTITY; // just fix the entity if the object falls off too far + + // You must specify the size of the broad phase - objects should not be simulated outside this region + worldInfo.setBroadPhaseWorldSize(1000.0f); + m_PhysicsWorld = new hkpWorld(worldInfo); + } + // Register all collision agents, even though only box - box will be used in this particular example. + // It's important to register collision agents before adding any entities to the world. + hkpAgentRegisterUtil::registerAllAgents(m_PhysicsWorld->getCollisionDispatcher()); + + // + // Initialize the visual debugger so we can connect remotely to the simulation + // The context must exist beyond the use of the VDB instance, and you can make + // whatever contexts you like for your own viewer types. + // + hkpPhysicsContext* context = new hkpPhysicsContext; + hkpPhysicsContext::registerAllPhysicsProcesses(); // all the physics viewers + context->addWorld(m_PhysicsWorld); // add the physics world so the viewers can see it + SetupVisualDebugger(context); + + SetupPhysics(m_PhysicsWorld); } void Systems::PhysicsSystem::Update(double dt) { + m_PhysicsWorld->stepDeltaTime(dt); + // Step the visual debugger + StepVisualDebugger(); } void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -39,3 +90,81 @@ void Systems::PhysicsSystem::TearDownPhysicsState(EntityID entity, EntityID pare } +void Systems::PhysicsSystem::SetupVisualDebugger(hkpPhysicsContext* worlds) +{ + // Setup the visual debugger + hkArray contexts; + contexts.pushBack(worlds); + + m_VisualDebugger = new hkVisualDebugger(contexts); + m_VisualDebugger->serve(); + + // Allocate memory for internal profiling information + // You can discard this if you do not want Havok profiling information + hkMonitorStream& stream = hkMonitorStream::getInstance(); + stream.resize(500 * 1024); // 500K for timer info + stream.reset(); + +} + +void Systems::PhysicsSystem::StepVisualDebugger() +{ + // Step the debugger + m_VisualDebugger->step(); + + // Reset internal profiling info for next frame + hkMonitorStream::getInstance().reset(); +} + +void HK_CALL Systems::PhysicsSystem::HavokErrorReport(const char* msg, void*) +{ + LOG_ERROR("%s", msg); +} + + +void Systems::PhysicsSystem::SetupPhysics(hkpWorld* physicsWorld) +{ + // Create the floor as a fixed box + { + hkpRigidBodyCinfo boxInfo; + hkVector4 boxSize(5.0f, 0.5f, 5.0f); + hkpBoxShape* boxShape = new hkpBoxShape(boxSize); + boxInfo.m_shape = boxShape; + boxInfo.m_motionType = hkpMotion::MOTION_FIXED; + boxInfo.m_position.set(0.0f, 0.0f, 0.0f); + boxInfo.m_restitution = 0.9f; + + hkpRigidBody* floor = new hkpRigidBody(boxInfo); + boxShape->removeReference(); + + physicsWorld->addEntity(floor); + floor->removeReference(); + } + + // Create a moving sphere + { + hkReal sphereRadius = 0.5f; + hkpConvexShape* sphereShape = new hkpSphereShape(sphereRadius); + + hkpRigidBodyCinfo sphereInfo; + sphereInfo.m_shape = sphereShape; + sphereInfo.m_position.set(0.0f, 5.0f, 0.0f); + sphereInfo.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA; + + // Compute mass properties + hkReal sphereMass = 10.0f; + hkMassProperties sphereMassProperties; + hkpInertiaTensorComputer::computeSphereVolumeMassProperties(sphereRadius, sphereMass, sphereMassProperties); + sphereInfo.m_inertiaTensor = sphereMassProperties.m_inertiaTensor; + sphereInfo.m_centerOfMass = sphereMassProperties.m_centerOfMass; + sphereInfo.m_mass = sphereMassProperties.m_mass; + + // Create sphere RigidBody + hkpRigidBody* sphereRigidBody = new hkpRigidBody(sphereInfo); + sphereShape->removeReference(); + + physicsWorld->addEntity(sphereRigidBody); + g_ball = sphereRigidBody; + sphereRigidBody->removeReference(); + } +} \ No newline at end of file diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 1bceb90..d85342c 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -1,10 +1,41 @@ #ifndef PhysicsSystem_h__ #define PhysicsSystem_h__ + + + + #include "System.h" #include "Components/Transform.h" -#include +// Math and base include + +#include +#include +#include +#include +#include +#include +#include +#include + +// Dynamics includes +#include +#include +#include +#include +#include + + + +#include +#include +#include + +// Visual Debugger includes +#include +#include + namespace Systems { @@ -17,11 +48,25 @@ public: void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void OnComponentCreated(std::string type, std::shared_ptr component) override; void OnComponentRemoved(std::string type, Component* component) override; + private: + + hkpWorld* m_PhysicsWorld; + void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); + + hkVisualDebugger* m_VisualDebugger; + void SetupVisualDebugger(hkpPhysicsContext* worlds); + void StepVisualDebugger(); + static void HK_CALL HavokErrorReport(const char* msg, void*); + + void SetupPhysics(hkpWorld* physicsWorld); + + hkpRigidBody* g_ball; }; + } #endif // PhysicsSystem_h__ diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index bc11a09..828d5e9 100644 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -24,7 +24,7 @@ Application false - v120 + v110 true MultiByte @@ -39,14 +39,14 @@ - $(BOOST_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(IncludePath) - $(BOOST_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Debug;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Debug;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Debug;$(SolutionDir)\..\libs\SOIL\lib\Debug;$(LibraryPath) + $(HAVOK_ROOT)\Source;$(BOOST_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(IncludePath) + $(HAVOK_ROOT)\Lib\win32_vs2012_win7\debug;$(BOOST_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Debug;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Debug;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Debug;$(SolutionDir)\..\libs\SOIL\lib\Debug;$(LibraryPath) $(SolutionDir)\..\bin\$(Configuration)\ $(SolutionDir)\..\obj\$(Configuration)\ - $(BOOST_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(IncludePath) - $(BOOST_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Release;$(SolutionDir)\..\libs\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Release;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Release;$(SolutionDir)\..\libs\SOIL\lib\Release;$(LibraryPath) + $(HAVOK_ROOT)\Source;$(BOOST_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(IncludePath) + $(HAVOK_ROOT)\Lib\win32_vs2012_win7\release_dll;$(BOOST_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Release;$(SolutionDir)\..\libs\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Release;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Release;$(SolutionDir)\..\libs\SOIL\lib\Release;$(LibraryPath) $(SolutionDir)\..\bin\$(Configuration)\ $(SolutionDir)\..\obj\$(Configuration)\ @@ -55,14 +55,16 @@ Level3 Disabled true - DEBUG;_CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) + _WINDOWS;WIN32;_WIN32;_DEBUG;HK_DEBUG;HK_DEBUG_SLOW;_XT_STATICLINK;_CONSOLE;_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH;HK_CONFIG_SIMD=1;DEBUG;_CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) Create PrecompiledHeader.h true + MultiThreadedDebug true - OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32d.lib;glfw3.lib;%(AdditionalDependencies) + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32d.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkpVehicle.lib;hkSceneData.lib;%(AdditionalDependencies) + /ignore:4221 @@ -84,7 +86,7 @@ true true true - OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32.lib;glfw3.lib;%(AdditionalDependencies) + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;%(AdditionalDependencies)