refactor(workspace): physicsWorld is now never null

This commit is contained in:
maelstrom 2025-07-01 01:57:29 +02:00
parent f2a7662b08
commit b1c7eca289
7 changed files with 12 additions and 15 deletions

View file

@ -20,7 +20,6 @@ void Rotate::buildJoint() {
if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return;
std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value(); std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value();
if (!workspace->physicsWorld) return;
// Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it
// used to be rather than specifying an anchor rotation, so whatever. // used to be rather than specifying an anchor rotation, so whatever.
@ -41,7 +40,7 @@ void Rotate::buildJoint() {
// !!! REMINDER: This has to be called manually when parts are destroyed/removed from the workspace, or joints will linger // !!! REMINDER: This has to be called manually when parts are destroyed/removed from the workspace, or joints will linger
void Rotate::breakJoint() { void Rotate::breakJoint() {
// If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything // If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything
if (!this->joint || jointWorkspace.expired() || !jointWorkspace.lock()->physicsWorld) return; if (!this->joint || jointWorkspace.expired()) return;
jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint); jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint);
this->joint = nullptr; this->joint = nullptr;

View file

@ -22,7 +22,6 @@ void RotateV::buildJoint() {
if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return;
std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value(); std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value();
if (!workspace->physicsWorld) return;
// Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it
@ -47,7 +46,7 @@ void RotateV::buildJoint() {
void RotateV::breakJoint() { void RotateV::breakJoint() {
// If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything // If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything
if (!this->joint || jointWorkspace.expired() || !jointWorkspace.lock()->physicsWorld) return; if (!this->joint || jointWorkspace.expired()) return;
jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint); jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint);
this->joint = nullptr; this->joint = nullptr;

View file

@ -24,7 +24,6 @@ void Snap::buildJoint() {
if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return;
std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value(); std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value();
if (!workspace->physicsWorld) return;
// Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it
// used to be rather than specifying an anchor rotation, so whatever. // used to be rather than specifying an anchor rotation, so whatever.
@ -40,7 +39,7 @@ void Snap::buildJoint() {
// !!! REMINDER: This has to be called manually when parts are destroyed/removed from the workspace, or joints will linger // !!! REMINDER: This has to be called manually when parts are destroyed/removed from the workspace, or joints will linger
void Snap::breakJoint() { void Snap::breakJoint() {
// If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything // If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything
if (!this->joint || jointWorkspace.expired() || !jointWorkspace.lock()->physicsWorld) return; if (!this->joint || jointWorkspace.expired()) return;
jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint); jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint);
this->joint = nullptr; this->joint = nullptr;

View file

@ -24,7 +24,6 @@ void Weld::buildJoint() {
if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return;
std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value(); std::shared_ptr<Workspace> workspace = workspaceOfPart(part0.lock()).value();
if (!workspace->physicsWorld) return;
// Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it // Update Part1's rotation and cframe prior to creating the joint as reactphysics3d locks rotation based on how it
// used to be rather than specifying an anchor rotation, so whatever. // used to be rather than specifying an anchor rotation, so whatever.
@ -40,7 +39,7 @@ void Weld::buildJoint() {
// !!! REMINDER: This has to be called manually when parts are destroyed/removed from the workspace, or joints will linger // !!! REMINDER: This has to be called manually when parts are destroyed/removed from the workspace, or joints will linger
void Weld::breakJoint() { void Weld::breakJoint() {
// If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything // If the joint doesn't exist, or its workspace expired (not our problem anymore), then no need to do anything
if (!this->joint || jointWorkspace.expired() || !jointWorkspace.lock()->physicsWorld) return; if (!this->joint || jointWorkspace.expired()) return;
jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint); jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint);
this->joint = nullptr; this->joint = nullptr;

View file

@ -16,10 +16,11 @@
rp::PhysicsCommon* Workspace::physicsCommon = new rp::PhysicsCommon; rp::PhysicsCommon* Workspace::physicsCommon = new rp::PhysicsCommon;
Workspace::Workspace(): Service(&TYPE), physicsEventListener(this) { Workspace::Workspace(): Service(&TYPE), physicsEventListener(this) {
physicsWorld = physicsCommon->createPhysicsWorld();
} }
Workspace::~Workspace() { Workspace::~Workspace() {
if (physicsWorld && physicsCommon) if (physicsCommon)
physicsCommon->destroyPhysicsWorld(physicsWorld); physicsCommon->destroyPhysicsWorld(physicsWorld);
} }
@ -67,8 +68,6 @@ void Workspace::InitService() {
if (initialized) return; if (initialized) return;
initialized = true; initialized = true;
physicsWorld = physicsCommon->createPhysicsWorld();
physicsWorld->setGravity(rp::Vector3(0, -196.2, 0)); physicsWorld->setGravity(rp::Vector3(0, -196.2, 0));
// world->setContactsPositionCorrectionTechnique(rp3d::ContactsPositionCorrectionTechnique::BAUMGARTE_CONTACTS); // world->setContactsPositionCorrectionTechnique(rp3d::ContactsPositionCorrectionTechnique::BAUMGARTE_CONTACTS);
physicsWorld->setNbIterationsPositionSolver(2000); physicsWorld->setNbIterationsPositionSolver(2000);
@ -103,8 +102,6 @@ void Workspace::InitService() {
} }
void Workspace::SyncPartPhysics(std::shared_ptr<Part> part) { void Workspace::SyncPartPhysics(std::shared_ptr<Part> part) {
if (!physicsWorld) return;
rp::Transform transform = part->cframe; rp::Transform transform = part->cframe;
if (!part->rigidBody) { if (!part->rigidBody) {
part->rigidBody = physicsWorld->createRigidBody(transform); part->rigidBody = physicsWorld->createRigidBody(transform);

View file

@ -2,6 +2,7 @@
#include "objects/annotation.h" #include "objects/annotation.h"
#include "objects/base/service.h" #include "objects/base/service.h"
#include "utils.h"
#include <glm/ext/vector_float3.hpp> #include <glm/ext/vector_float3.hpp>
#include <memory> #include <memory>
#include <reactphysics3d/body/RigidBody.h> #include <reactphysics3d/body/RigidBody.h>
@ -50,7 +51,7 @@ class PhysicsEventListener : public rp::EventListener {
class DEF_INST_SERVICE_(explorer_icon="workspace") Workspace : public Service { class DEF_INST_SERVICE_(explorer_icon="workspace") Workspace : public Service {
AUTOGEN_PREAMBLE AUTOGEN_PREAMBLE
rp::PhysicsWorld* physicsWorld = nullptr; rp::PhysicsWorld* notnull physicsWorld;
static rp::PhysicsCommon* physicsCommon; static rp::PhysicsCommon* physicsCommon;
PhysicsEventListener physicsEventListener; PhysicsEventListener physicsEventListener;

View file

@ -1,9 +1,12 @@
#pragma once #pragma once
#ifdef __clang__ #if defined(__clang__)
#pragma clang diagnostic ignored "-Wnullability-extension" #pragma clang diagnostic ignored "-Wnullability-extension"
#define nullable _Nullable #define nullable _Nullable
#define notnull _Nonnull #define notnull _Nonnull
#elif defined(__GNUC__)
#define nullable
#define notnull __attribute__((nonnull))
#else #else
#define nullable #define nullable
#define notnull #define notnull