refactor(joint): abstracted jointinstance into its own class away from snap

This commit is contained in:
maelstrom 2025-04-23 17:39:30 +02:00
parent 587629fcdd
commit 6a461143a4
13 changed files with 219 additions and 158 deletions

View file

@ -148,6 +148,12 @@ void Instance::Destroy() {
parentLocked = true;
}
bool Instance::IsA(std::string className) {
const InstanceType* cur = GetClass();
while (cur && cur->className != className) { cur = cur->super; }
return cur != nullptr;
}
static std::shared_ptr<Instance> DUMMY_INSTANCE;
DescendantsIterator Instance::GetDescendantsStart() {
return DescendantsIterator(GetChildren().size() > 0 ? GetChildren()[0] : DUMMY_INSTANCE);

View file

@ -41,7 +41,7 @@ struct InstanceType {
typedef std::pair<std::shared_ptr<Instance>, std::string> _RefStatePropertyCell;
class DescendantsIterator;
class Snap;
class JointInstance;
// Base class for all instances in the data model
// Note: enable_shared_from_this HAS to be public or else its field will not be populated
@ -60,7 +60,7 @@ private:
bool ancestryContinuityCheck(std::optional<std::shared_ptr<Instance>> newParent);
void updateAncestry(std::optional<std::shared_ptr<Instance>> child, std::optional<std::shared_ptr<Instance>> newParent);
friend Snap; // This isn't ideal, but oh well
friend JointInstance; // This isn't ideal, but oh well
protected:
bool parentLocked = false;
std::unique_ptr<MemberMap> memberMap;
@ -92,6 +92,9 @@ public:
bool IsParentLocked();
inline const std::vector<std::shared_ptr<Instance>> GetChildren() { return children; }
void Destroy();
// Determines whether this object is an instance of, or an instance of a subclass of the sepcified type's class name
bool IsA(std::string className);
template <typename T> bool IsA() { return IsA(T::TYPE.className); }
DescendantsIterator GetDescendantsStart();
DescendantsIterator GetDescendantsEnd();

View file

@ -0,0 +1,94 @@
#include "jointinstance.h"
#include "datatypes/cframe.h"
#include "datatypes/ref.h"
#include "objects/datamodel.h"
#include "objects/jointsservice.h"
#include "objects/part.h"
#include "objects/workspace.h"
#include <memory>
#include <reactphysics3d/constraint/FixedJoint.h>
#include <reactphysics3d/engine/PhysicsWorld.h>
#include "ptr_helpers.h"
const InstanceType JointInstance::TYPE = {
.super = &Instance::TYPE,
.className = "JointInstance",
};
const InstanceType* JointInstance::GetClass() {
return &TYPE;
}
JointInstance::JointInstance(const InstanceType* type): Instance(type) {
this->memberMap = std::make_unique<MemberMap>(MemberMap {
.super = std::move(this->memberMap),
.members = {
{ "Part0", {
.backingField = &part0,
.type = &Data::InstanceRef::TYPE,
.codec = fieldCodecOf<Data::InstanceRef, std::weak_ptr<Instance>>(),
.updateCallback = memberFunctionOf(&JointInstance::onUpdated, this),
}}, { "Part1", {
.backingField = &part1,
.type = &Data::InstanceRef::TYPE,
.codec = fieldCodecOf<Data::InstanceRef, std::weak_ptr<Instance>>(),
.updateCallback = memberFunctionOf(&JointInstance::onUpdated, this),
}}, { "C0", {
.backingField = &c0,
.type = &Data::CFrame::TYPE,
.codec = fieldCodecOf<Data::CFrame>(),
.updateCallback = memberFunctionOf(&JointInstance::onUpdated, this),
}}, { "C1", {
.backingField = &c1,
.type = &Data::CFrame::TYPE,
.codec = fieldCodecOf<Data::CFrame>(),
.updateCallback = memberFunctionOf(&JointInstance::onUpdated, this),
}},
}
});
}
JointInstance::~JointInstance() {
}
void JointInstance::OnAncestryChanged(std::optional<std::shared_ptr<Instance>>, std::optional<std::shared_ptr<Instance>>) {
// Destroy and rebuild the joint, it's the simplest solution that actually works
breakJoint();
buildJoint();
}
void JointInstance::onUpdated(std::string property) {
// Add ourselves to the attached parts, or remove, if applicable
// Parts differ, delete
if (part0 != oldPart0 && !oldPart0.expired()) {
oldPart0.lock()->untrackJoint(shared<JointInstance>());
}
if (part1 != oldPart1 && !oldPart1.expired()) {
oldPart1.lock()->untrackJoint(shared<JointInstance>());
}
// Parts differ, add
if (part0 != oldPart0 && !part0.expired()) {
part0.lock()->trackJoint(shared<JointInstance>());
}
if (part1 != oldPart1 && !part1.expired()) {
part1.lock()->trackJoint(shared<JointInstance>());
}
// Destroy and rebuild the joint, if applicable
breakJoint();
buildJoint();
oldPart0 = part0;
oldPart1 = part1;
}
std::optional<std::shared_ptr<Workspace>> JointInstance::workspaceOfPart(std::shared_ptr<Part> part) {
return part->workspace();
}

View file

@ -7,20 +7,19 @@
class Part;
class Workspace;
class Snap : public Instance {
rp::FixedJoint* joint = nullptr;
class JointInstance : public Instance {
std::weak_ptr<Part> oldPart0;
std::weak_ptr<Part> oldPart1;
protected:
// The workspace the joint was created in, if it exists
std::weak_ptr<Workspace> jointWorkspace;
protected:
void OnAncestryChanged(std::optional<std::shared_ptr<Instance>>, std::optional<std::shared_ptr<Instance>>) override;
std::optional<std::shared_ptr<Workspace>> workspaceOfPart(std::shared_ptr<Part>);
void onUpdated(std::string property);
void buildJoint();
void breakJoint();
virtual void buildJoint() = 0;
virtual void breakJoint() = 0;
public:
const static InstanceType TYPE;
@ -29,10 +28,8 @@ public:
Data::CFrame c0;
Data::CFrame c1;
Snap();
~Snap();
JointInstance(const InstanceType*);
~JointInstance();
static inline std::shared_ptr<Snap> New() { return std::make_shared<Snap>(); };
static inline std::shared_ptr<Instance> Create() { return std::make_shared<Snap>(); };
virtual const InstanceType* GetClass() override;
};

View file

@ -0,0 +1,59 @@
#include "snap.h"
#include "datatypes/cframe.h"
#include "objects/datamodel.h"
#include "objects/joint/jointinstance.h"
#include "objects/jointsservice.h"
#include "objects/part.h"
#include "objects/workspace.h"
#include <memory>
#include <reactphysics3d/constraint/FixedJoint.h>
#include <reactphysics3d/engine/PhysicsWorld.h>
const InstanceType Snap::TYPE = {
.super = &JointInstance::TYPE,
.className = "Snap",
.constructor = &Snap::Create,
};
const InstanceType* Snap::GetClass() {
return &TYPE;
}
Snap::Snap(): JointInstance(&TYPE) {
}
Snap::~Snap() {
}
void Snap::buildJoint() {
// Only if both parts are set, are not the same part, are part of a workspace, and are part of the same workspace, we build the joint
if (part0.expired() || part1.expired() || part0.lock() == part1.lock() || !workspaceOfPart(part0.lock()) || workspaceOfPart(part0.lock()) != workspaceOfPart(part1.lock())) return;
// Don't build the joint if we're not part of either a workspace or JointsService
if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return;
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
// used to be rather than specifying an anchor rotation, so whatever.
Data::CFrame newFrame = part0.lock()->cframe * (c1.Inverse() * c0);
part1.lock()->cframe = newFrame;
workspace->SyncPartPhysics(part1.lock());
// printf("c1.Rotation: ");
// printVec(c1.ToEulerAnglesXYZ());
rp::FixedJointInfo jointInfo(part0.lock()->rigidBody, part1.lock()->rigidBody, (c0.Inverse() * c1).Position());
this->joint = dynamic_cast<rp::FixedJoint*>(workspace->physicsWorld->createJoint(jointInfo));
jointWorkspace = workspace;
}
// !!! 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;
jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint);
this->joint = nullptr;
}

View file

@ -0,0 +1,25 @@
#pragma once
#include "objects/base/instance.h"
#include "objects/joint/jointinstance.h"
#include <memory>
#include <optional>
class Part;
class Workspace;
class Snap : public JointInstance {
rp::FixedJoint* joint = nullptr;
virtual void buildJoint() override;
virtual void breakJoint() override;
public:
const static InstanceType TYPE;
Snap();
~Snap();
static inline std::shared_ptr<Snap> New() { return std::make_shared<Snap>(); };
static inline std::shared_ptr<Instance> Create() { return std::make_shared<Snap>(); };
virtual const InstanceType* GetClass() override;
};

View file

@ -2,12 +2,9 @@
#include "objects/base/service.h"
class Snap;
class JointsService : public Service {
private:
std::optional<std::shared_ptr<Workspace>> jointWorkspace();
friend Snap;
protected:
void InitService() override;
bool initialized = false;

View file

@ -1,7 +1,8 @@
#include "meta.h"
#include "objects/joint/jointinstance.h"
#include "objects/jointsservice.h"
#include "objects/part.h"
#include "objects/snap.h"
#include "objects/joint/snap.h"
#include "objects/workspace.h"
std::map<std::string, const InstanceType*> INSTANCE_MAP = {
@ -10,5 +11,6 @@ std::map<std::string, const InstanceType*> INSTANCE_MAP = {
{ "Workspace", &Workspace::TYPE },
{ "DataModel", &DataModel::TYPE },
{ "Snap", &Snap::TYPE },
{ "JointInstance", &JointInstance::TYPE },
{ "JointsService", &JointsService::TYPE },
};

View file

@ -7,7 +7,8 @@
#include "datatypes/vector.h"
#include "objects/base/member.h"
#include "objects/jointsservice.h"
#include "objects/snap.h"
#include "objects/joint/jointinstance.h"
#include "objects/joint/snap.h"
#include "rendering/surface.h"
#include <memory>
#include <optional>
@ -227,12 +228,12 @@ Vector3 Part::GetAABB() {
}
void Part::BreakJoints() {
for (std::weak_ptr<Snap> joint : primaryJoints) {
for (std::weak_ptr<JointInstance> joint : primaryJoints) {
if (joint.expired()) continue;
joint.lock()->Destroy();
}
for (std::weak_ptr<Snap> joint : secondaryJoints) {
for (std::weak_ptr<JointInstance> joint : secondaryJoints) {
if (joint.expired()) continue;
joint.lock()->Destroy();
}
@ -335,7 +336,7 @@ void Part::MakeJoints() {
}
}
void Part::trackJoint(std::shared_ptr<Snap> joint) {
void Part::trackJoint(std::shared_ptr<JointInstance> joint) {
if (!joint->part0.expired() && joint->part0.lock() == shared_from_this()) {
for (auto it = primaryJoints.begin(); it != primaryJoints.end();) {
// Clean expired refs
@ -369,7 +370,7 @@ void Part::trackJoint(std::shared_ptr<Snap> joint) {
}
}
void Part::untrackJoint(std::shared_ptr<Snap> joint) {
void Part::untrackJoint(std::shared_ptr<JointInstance> joint) {
for (auto it = primaryJoints.begin(); it != primaryJoints.end();) {
// Clean expired refs
if (it->expired() || it->lock() == joint) {

View file

@ -29,17 +29,17 @@ class Snap;
class Part : public Instance {
protected:
// Joints where this part is Part0
std::vector<std::weak_ptr<Snap>> primaryJoints;
std::vector<std::weak_ptr<JointInstance>> primaryJoints;
// Joints where this part is Part1
std::vector<std::weak_ptr<Snap>> secondaryJoints;
std::vector<std::weak_ptr<JointInstance>> secondaryJoints;
void trackJoint(std::shared_ptr<Snap>);
void untrackJoint(std::shared_ptr<Snap>);
void trackJoint(std::shared_ptr<JointInstance>);
void untrackJoint(std::shared_ptr<JointInstance>);
SurfaceType surfaceFromFace(NormalId);
bool checkJointContinuinty(std::shared_ptr<Part>);
friend Snap;
friend JointInstance;
void OnAncestryChanged(std::optional<std::shared_ptr<Instance>> child, std::optional<std::shared_ptr<Instance>> newParent) override;
void onUpdated(std::string);

View file

@ -1,123 +0,0 @@
#include "snap.h"
#include "datatypes/cframe.h"
#include "datatypes/ref.h"
#include "objects/datamodel.h"
#include "objects/jointsservice.h"
#include "objects/part.h"
#include "workspace.h"
#include <memory>
#include <reactphysics3d/constraint/FixedJoint.h>
#include <reactphysics3d/engine/PhysicsWorld.h>
#include "ptr_helpers.h"
const InstanceType Snap::TYPE = {
.super = &Instance::TYPE,
.className = "Snap",
.constructor = &Snap::Create,
};
const InstanceType* Snap::GetClass() {
return &TYPE;
}
Snap::Snap(): Instance(&TYPE) {
this->memberMap = std::make_unique<MemberMap>(MemberMap {
.super = std::move(this->memberMap),
.members = {
{ "Part0", {
.backingField = &part0,
.type = &Data::InstanceRef::TYPE,
.codec = fieldCodecOf<Data::InstanceRef, std::weak_ptr<Instance>>(),
.updateCallback = memberFunctionOf(&Snap::onUpdated, this),
}}, { "Part1", {
.backingField = &part1,
.type = &Data::InstanceRef::TYPE,
.codec = fieldCodecOf<Data::InstanceRef, std::weak_ptr<Instance>>(),
.updateCallback = memberFunctionOf(&Snap::onUpdated, this),
}}, { "C0", {
.backingField = &c0,
.type = &Data::CFrame::TYPE,
.codec = fieldCodecOf<Data::CFrame>(),
.updateCallback = memberFunctionOf(&Snap::onUpdated, this),
}}, { "C1", {
.backingField = &c1,
.type = &Data::CFrame::TYPE,
.codec = fieldCodecOf<Data::CFrame>(),
.updateCallback = memberFunctionOf(&Snap::onUpdated, this),
}},
}
});
}
Snap::~Snap() {
}
void Snap::OnAncestryChanged(std::optional<std::shared_ptr<Instance>>, std::optional<std::shared_ptr<Instance>>) {
// Destroy and rebuild the joint, it's the simplest solution that actually works
breakJoint();
buildJoint();
}
void Snap::onUpdated(std::string property) {
// Add ourselves to the attached parts, or remove, if applicable
// Parts differ, delete
if (part0 != oldPart0 && !oldPart0.expired()) {
oldPart0.lock()->untrackJoint(shared<Snap>());
}
if (part1 != oldPart1 && !oldPart1.expired()) {
oldPart1.lock()->untrackJoint(shared<Snap>());
}
// Parts differ, add
if (part0 != oldPart0 && !part0.expired()) {
part0.lock()->trackJoint(shared<Snap>());
}
if (part1 != oldPart1 && !part1.expired()) {
part1.lock()->trackJoint(shared<Snap>());
}
// Destroy and rebuild the joint, if applicable
breakJoint();
buildJoint();
oldPart0 = part0;
oldPart1 = part1;
}
void Snap::buildJoint() {
// Only if both parts are set, are not the same part, are part of a workspace, and are part of the same workspace, we build the joint
if (part0.expired() || part1.expired() || part0.lock() == part1.lock() || !part0.lock()->workspace() || part0.lock()->workspace() != part1.lock()->workspace()) return;
// Don't build the joint if we're not part of either a workspace or JointsService
if ((!GetParent() || GetParent().value()->GetClass() != &JointsService::TYPE) && !workspace()) return;
std::shared_ptr<Workspace> workspace = part0.lock()->workspace().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.
Data::CFrame newFrame = part0.lock()->cframe * (c1.Inverse() * c0);
part1.lock()->cframe = newFrame;
workspace->SyncPartPhysics(part1.lock());
// printf("c1.Rotation: ");
// printVec(c1.ToEulerAnglesXYZ());
rp::FixedJointInfo jointInfo(part0.lock()->rigidBody, part1.lock()->rigidBody, (c0.Inverse() * c1).Position());
this->joint = dynamic_cast<rp::FixedJoint*>(workspace->physicsWorld->createJoint(jointInfo));
jointWorkspace = workspace;
}
// !!! 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;
jointWorkspace.lock()->physicsWorld->destroyJoint(this->joint);
this->joint = nullptr;
}

View file

@ -1,7 +1,7 @@
#include "workspace.h"
#include "objects/base/instance.h"
#include "objects/jointsservice.h"
#include "objects/snap.h"
#include "objects/joint/jointinstance.h"
#include "physics/util.h"
#include <reactphysics3d/engine/PhysicsCommon.h>
@ -45,8 +45,8 @@ void Workspace::InitService() {
// Sync all parts
for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) {
InstanceRef obj = *it;
if (obj->GetClass()->className != "Part") continue; // TODO: Replace this with a .IsA call instead of comparing the class name directly
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(obj);
if (!obj->IsA<Part>()) continue;
std::shared_ptr<Part> part = obj->CastTo<Part>().expect();
this->SyncPartPhysics(part);
part->MakeJoints();
}
@ -54,14 +54,14 @@ void Workspace::InitService() {
// Activate all joints
for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) {
InstanceRef obj = *it;
if (obj->GetClass()->className != "Snap") continue; // TODO: Replace this with a .IsA call instead of comparing the class name directly
std::shared_ptr<Snap> joint = std::dynamic_pointer_cast<Snap>(obj);
if (!obj->IsA<JointInstance>()) continue;
std::shared_ptr<JointInstance> joint = obj->CastTo<JointInstance>().expect();
joint->UpdateProperty("Part0");
}
for (auto obj : dataModel().value()->GetService<JointsService>()->GetChildren()) {
if (obj->GetClass()->className != "Snap") continue; // TODO: Replace this with a .IsA call instead of comparing the class name directly
std::shared_ptr<Snap> joint = std::dynamic_pointer_cast<Snap>(obj);
if (!obj->IsA<JointInstance>()) continue;
std::shared_ptr<JointInstance> joint = obj->CastTo<JointInstance>().expect();
joint->UpdateProperty("Part0");
}
}

View file

@ -3,7 +3,7 @@
#include "common.h"
#include "logger.h"
#include "objects/jointsservice.h"
#include "objects/snap.h"
#include "objects/joint/snap.h"
#include <map>
#include <memory>
#include <qclipboard.h>