From fd68c5dde3742f29a56bb22ca0142386c4e8501f Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Sat, 24 Dec 2022 02:00:11 +0100 Subject: [PATCH] Moving Physics to its own system - Moved Physx to a singular Physics system - Removed old Physx test code from application.cpp --- Editor/src/app.cpp | 10 +- YoggieEngine/src/Application.cpp | 57 +----------- YoggieEngine/src/Physics/Physics.cpp | 132 +++++++++++++++++++++++++++ YoggieEngine/src/Physics/Physics.h | 66 ++++++++++++++ YoggieEngine/src/YoggieEngine.h | 3 + 5 files changed, 212 insertions(+), 56 deletions(-) create mode 100644 YoggieEngine/src/Physics/Physics.cpp create mode 100644 YoggieEngine/src/Physics/Physics.h diff --git a/Editor/src/app.cpp b/Editor/src/app.cpp index f875845..643141d 100644 --- a/Editor/src/app.cpp +++ b/Editor/src/app.cpp @@ -101,6 +101,12 @@ public: MainMenuBar menuBar= MainMenuBar(); menuBar.ApplicationMenu(CurrentProject); menuBar.SceneMenu(CurrentProject, ActiveScene); + /*menuBar.SelectMenu(); + menuBar.WindowMenu(); + menuBar.DebugMenu(); + menuBar.Help(); + */ + } { @@ -126,11 +132,11 @@ public: } { - Settings(); + Settings settings(); } { - AssetFinder assetsView = AssetFinder(); + // AssetFinder assetsView = AssetFinder(); } diff --git a/YoggieEngine/src/Application.cpp b/YoggieEngine/src/Application.cpp index 6cbdcd0..dbd4274 100644 --- a/YoggieEngine/src/Application.cpp +++ b/YoggieEngine/src/Application.cpp @@ -1,45 +1,13 @@ #include #include "Application.h" -#include -#define PVD_HOST "127.0.0.1" - -using namespace physx; namespace YoggieEngine { - static PxDefaultErrorCallback gDefaultErrorCallback; - static PxDefaultAllocator gDefaultAllocatorCallback; - static PxFoundation* mFoundation; - static PxPvd* mPvd; - static PxPvdTransport* transport; - static PxPhysics* mPhysics; - bool recordMemoryAllocations = true; - - - Application::Application(const std::string& name ) + Application::Application(const std::string& name) : m_AppName(name) { EngineInstrumentation::PerfomanceSamplerInit(); - // startup PhysX - mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback); - if (!mFoundation) { - spdlog::critical("PxCreateFoundation failed!"); - } - - mPvd = physx::PxCreatePvd(*mFoundation); - if (!mPvd) { - spdlog::critical("pxCreatPvd failed!"); - } - transport = PxDefaultPvdSocketTransportCreate(PVD_HOST, 5425, 10); - mPvd->connect(*transport, PxPvdInstrumentationFlag::eALL); - - - mPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, PxTolerancesScale(), recordMemoryAllocations, mPvd); - if (!mPhysics) { - spdlog::critical("pxCreatePhysics failed!"); - } - } @@ -48,24 +16,5 @@ namespace YoggieEngine { } - Application::~Application(){ - - if (mPhysics) - mPhysics->release(); - - if (mPvd) { - if (mPvd->isConnected()) { - mPvd->disconnect(); - } - mPvd->release(); - } - - if (transport) - transport->release(); - - - if (mFoundation) - mFoundation->release(); - } - -} \ No newline at end of file + Application::~Application() {} +} diff --git a/YoggieEngine/src/Physics/Physics.cpp b/YoggieEngine/src/Physics/Physics.cpp new file mode 100644 index 0000000..fba135f --- /dev/null +++ b/YoggieEngine/src/Physics/Physics.cpp @@ -0,0 +1,132 @@ +#include +#include "Physics.h" + +namespace YoggieEngine { + + Physics::Physics() + { + // startup PhysX + mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback); + if (!mFoundation) { + spdlog::critical("PxCreateFoundation failed!"); + } + + mPvd = physx::PxCreatePvd(*mFoundation); + if (!mPvd) { + spdlog::critical("pxCreatPvd failed!"); + } + transport = PxDefaultPvdSocketTransportCreate(PVD_HOST, 5425, 10); + mPvd->connect(*transport, PxPvdInstrumentationFlag::eALL); + + + mPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, PxTolerancesScale(), recordMemoryAllocations, mPvd); + if (!mPhysics) { + spdlog::critical("pxCreatePhysics failed!"); + } + gDispatcher = nullptr; + gMaterial = nullptr; + mScene = nullptr; + + + } + + Physics::~Physics() + { + if (mScene) + mScene->release(); + + if (gDispatcher) + gDispatcher->release(); + + if (mPhysics) + mPhysics->release(); + + if (mPvd) { + if (mPvd->isConnected()) { + mPvd->disconnect(); + } + mPvd->release(); + } + + if (transport) + transport->release(); + + + if (mFoundation) + mFoundation->release(); + } + + void Physics::Step(float dt) + { + mScene->simulate(dt); + mScene->fetchResults(true); + } + + void Physics::Demo() + { + createScene(); + + SetupPvdDebug(); + + createGroundPlane(); + + createStack(PxTransform(PxVec3(0, 0, stackZ -= 10.0f)), 10, 2.0f); + } + + PxRigidDynamic* Physics::createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity = PxVec3(0)) { + PxRigidDynamic* dynamic = PxCreateDynamic(*mPhysics, t, geometry, *gMaterial, 10.0f); + dynamic->setAngularDamping(0.5f); + dynamic->setLinearVelocity(velocity); + mScene->addActor(*dynamic); + return dynamic; + } + + void Physics::createStack(const PxTransform& t, PxU32 size, PxReal halfextent) + { + PxShape* shape = mPhysics->createShape(PxBoxGeometry(halfextent, halfextent, halfextent), *gMaterial); + for (PxU32 i = 0; i < size; i++) { + for (PxU32 j = 0; j < size - i; j++) { + PxTransform localTm(PxVec3(PxReal(j * 2) - PxReal(size - i), PxReal(i * 2 + 1), 0) * halfextent); + PxRigidDynamic* body = mPhysics->createRigidDynamic(t.transform(localTm)); + body->attachShape(*shape); + PxRigidBodyExt::updateMassAndInertia(*body, 10.0f); + mScene->addActor(*body); + } + } + + shape->release(); + + } + + void Physics::createScene() + { + PxSceneDesc sceneDesc(mPhysics->getTolerancesScale()); + sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f); + gDispatcher = PxDefaultCpuDispatcherCreate(2); + sceneDesc.cpuDispatcher = gDispatcher; + sceneDesc.filterShader = PxDefaultSimulationFilterShader; + mScene = mPhysics->createScene(sceneDesc); + + } + + void Physics::createGroundPlane() + { + gMaterial = mPhysics->createMaterial(0.5f, 0.5f, 0.6f); + PxRigidStatic* groundPlane = PxCreatePlane(*mPhysics, PxPlane(0,1,0,0), *gMaterial); + mScene->addActor(*groundPlane); + + } + + void Physics::SetupPvdDebug() + { + PxPvdSceneClient* pvdClient = mScene->getScenePvdClient(); + if (pvdClient) { + pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONSTRAINTS, true); + pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONTACTS, true); + pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_SCENEQUERIES, true); + } + + + } + +} \ No newline at end of file diff --git a/YoggieEngine/src/Physics/Physics.h b/YoggieEngine/src/Physics/Physics.h new file mode 100644 index 0000000..ab29857 --- /dev/null +++ b/YoggieEngine/src/Physics/Physics.h @@ -0,0 +1,66 @@ +#pragma once + +#include +using namespace physx; // For now will just focus on getting Physx working in its most basic form + +#define PVD_HOST "127.0.0.1" +#define MAX_IP_CHAR_COUNT 16 + +namespace YoggieEngine { + + struct PhysicsConfig { + + bool recordMemoryAllocations; + const char PvdHost[MAX_IP_CHAR_COUNT]; + float StackZ; + + }; + + class Physics { + public: + + Physics(); + ~Physics(); + + void Step(float dt); + + void Demo(); + + + private: + + PxRigidDynamic* createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity); + void createStack(const PxTransform& t, PxU32 size, PxReal halfextent); + void createScene(); + void createGroundPlane(); + + void SetupPvdDebug(); + + + // Memory Management + bool recordMemoryAllocations = true; + PxDefaultErrorCallback gDefaultErrorCallback; + PxDefaultAllocator gDefaultAllocatorCallback; + PxDefaultCpuDispatcher* gDispatcher; + + // Basics + PxFoundation* mFoundation; + PxPhysics* mPhysics; + + // Simulation + PxScene* mScene; + PxMaterial* gMaterial; + + PxReal stackZ = 10.0f; + + // DEBUG + PxPvd* mPvd; + PxPvdTransport* transport; + + }; + +} + + + + diff --git a/YoggieEngine/src/YoggieEngine.h b/YoggieEngine/src/YoggieEngine.h index fb8dd97..6e6b940 100644 --- a/YoggieEngine/src/YoggieEngine.h +++ b/YoggieEngine/src/YoggieEngine.h @@ -39,8 +39,11 @@ extern "C" #include "Graphics/Primitives/Material.h" #include "Graphics/Renderer.h" +#include "Physics/Physics.h" + #include "EventSystem/EventEmitter.h" #include "EventSystem/EventListener.h" + #include "Scene/Scene.h" #include "Application.h"