From b1c7eca2895914ddbe4d28d9db9b9af10c540373 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Tue, 1 Jul 2025 01:57:29 +0200 Subject: [PATCH] refactor(workspace): physicsWorld is now never null --- core/src/objects/joint/rotate.cpp | 3 +-- core/src/objects/joint/rotatev.cpp | 3 +-- core/src/objects/joint/snap.cpp | 3 +-- core/src/objects/joint/weld.cpp | 3 +-- core/src/objects/service/workspace.cpp | 7 ++----- core/src/objects/service/workspace.h | 3 ++- core/src/utils.h | 5 ++++- 7 files changed, 12 insertions(+), 15 deletions(-) diff --git a/core/src/objects/joint/rotate.cpp b/core/src/objects/joint/rotate.cpp index 97a8057..4000ec6 100644 --- a/core/src/objects/joint/rotate.cpp +++ b/core/src/objects/joint/rotate.cpp @@ -20,7 +20,6 @@ void Rotate::buildJoint() { if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; std::shared_ptr 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 // 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 void Rotate::breakJoint() { // 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); this->joint = nullptr; diff --git a/core/src/objects/joint/rotatev.cpp b/core/src/objects/joint/rotatev.cpp index 92fc1b7..6783d44 100644 --- a/core/src/objects/joint/rotatev.cpp +++ b/core/src/objects/joint/rotatev.cpp @@ -22,7 +22,6 @@ void RotateV::buildJoint() { if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; std::shared_ptr 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 @@ -47,7 +46,7 @@ void RotateV::buildJoint() { void RotateV::breakJoint() { // 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); this->joint = nullptr; diff --git a/core/src/objects/joint/snap.cpp b/core/src/objects/joint/snap.cpp index a4e1406..e648744 100644 --- a/core/src/objects/joint/snap.cpp +++ b/core/src/objects/joint/snap.cpp @@ -24,7 +24,6 @@ void Snap::buildJoint() { if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; std::shared_ptr 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 // 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 void Snap::breakJoint() { // 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); this->joint = nullptr; diff --git a/core/src/objects/joint/weld.cpp b/core/src/objects/joint/weld.cpp index 0215c83..a2b1395 100644 --- a/core/src/objects/joint/weld.cpp +++ b/core/src/objects/joint/weld.cpp @@ -24,7 +24,6 @@ void Weld::buildJoint() { if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return; std::shared_ptr 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 // 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 void Weld::breakJoint() { // 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); this->joint = nullptr; diff --git a/core/src/objects/service/workspace.cpp b/core/src/objects/service/workspace.cpp index e23fb40..644d295 100644 --- a/core/src/objects/service/workspace.cpp +++ b/core/src/objects/service/workspace.cpp @@ -16,10 +16,11 @@ rp::PhysicsCommon* Workspace::physicsCommon = new rp::PhysicsCommon; Workspace::Workspace(): Service(&TYPE), physicsEventListener(this) { + physicsWorld = physicsCommon->createPhysicsWorld(); } Workspace::~Workspace() { - if (physicsWorld && physicsCommon) + if (physicsCommon) physicsCommon->destroyPhysicsWorld(physicsWorld); } @@ -67,8 +68,6 @@ void Workspace::InitService() { if (initialized) return; initialized = true; - physicsWorld = physicsCommon->createPhysicsWorld(); - physicsWorld->setGravity(rp::Vector3(0, -196.2, 0)); // world->setContactsPositionCorrectionTechnique(rp3d::ContactsPositionCorrectionTechnique::BAUMGARTE_CONTACTS); physicsWorld->setNbIterationsPositionSolver(2000); @@ -103,8 +102,6 @@ void Workspace::InitService() { } void Workspace::SyncPartPhysics(std::shared_ptr part) { - if (!physicsWorld) return; - rp::Transform transform = part->cframe; if (!part->rigidBody) { part->rigidBody = physicsWorld->createRigidBody(transform); diff --git a/core/src/objects/service/workspace.h b/core/src/objects/service/workspace.h index 1392507..75ea77b 100644 --- a/core/src/objects/service/workspace.h +++ b/core/src/objects/service/workspace.h @@ -2,6 +2,7 @@ #include "objects/annotation.h" #include "objects/base/service.h" +#include "utils.h" #include #include #include @@ -50,7 +51,7 @@ class PhysicsEventListener : public rp::EventListener { class DEF_INST_SERVICE_(explorer_icon="workspace") Workspace : public Service { AUTOGEN_PREAMBLE - rp::PhysicsWorld* physicsWorld = nullptr; + rp::PhysicsWorld* notnull physicsWorld; static rp::PhysicsCommon* physicsCommon; PhysicsEventListener physicsEventListener; diff --git a/core/src/utils.h b/core/src/utils.h index 759dfd3..4a9f33c 100644 --- a/core/src/utils.h +++ b/core/src/utils.h @@ -1,9 +1,12 @@ #pragma once -#ifdef __clang__ +#if defined(__clang__) #pragma clang diagnostic ignored "-Wnullability-extension" #define nullable _Nullable #define notnull _Nonnull +#elif defined(__GNUC__) +#define nullable +#define notnull __attribute__((nonnull)) #else #define nullable #define notnull