Moving Physics to its own system

- Moved Physx to a singular Physics system
- Removed old Physx test code from application.cpp
This commit is contained in:
Nigel Barink 2022-12-24 02:00:11 +01:00
parent 1f1a776686
commit fd68c5dde3
5 changed files with 212 additions and 56 deletions

View File

@ -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();
}

View File

@ -1,45 +1,13 @@
#include <YoggieEngine.h>
#include "Application.h"
#include <PxPhysicsAPI.h>
#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();
}
}
Application::~Application() {}
}

View File

@ -0,0 +1,132 @@
#include <YoggieEngine.h>
#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);
}
}
}

View File

@ -0,0 +1,66 @@
#pragma once
#include <PxPhysicsAPI.h>
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;
};
}

View File

@ -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"