feat(physics): hinge joints

This commit is contained in:
maelstrom 2025-09-01 20:00:54 +02:00
parent 2f0f507051
commit 3dfc3abaf8
7 changed files with 29 additions and 20 deletions

View file

@ -20,7 +20,7 @@ void Rotate::buildJoint() {
part1.lock()->cframe = newFrame; part1.lock()->cframe = newFrame;
// Do NOT use Abs() in this scenario. For some reason that breaks it // Do NOT use Abs() in this scenario. For some reason that breaks it
PhysJointHingeInfo jointInfo((part0.lock()->cframe * c0).Position(), -(part0.lock()->cframe * c0).LookVector().Unit()); PhysHingeJointInfo jointInfo(c0, c1);
this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock()); this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock());
jointWorkspace = workspace; jointWorkspace = workspace;
} }

View file

@ -21,7 +21,7 @@ void RotateV::buildJoint() {
CFrame newFrame = part0.lock()->cframe * (c0 * c1.Inverse()); CFrame newFrame = part0.lock()->cframe * (c0 * c1.Inverse());
part1.lock()->cframe = newFrame; part1.lock()->cframe = newFrame;
// Do NOT use Abs() in this scenario. For some reason that breaks it // Do NOT use Abs() in this scenario. For some reason that breaks it
PhysJointMotorInfo jointInfo((part0.lock()->cframe * c0).Position(), -(part0.lock()->cframe * c0).LookVector().Unit()); PhysMotorJointInfo jointInfo((part0.lock()->cframe * c0).Position(), -(part0.lock()->cframe * c0).LookVector().Unit());
this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock()); this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock());
jointWorkspace = workspace; jointWorkspace = workspace;

View file

@ -23,7 +23,7 @@ void Snap::buildJoint() {
CFrame newFrame = part0.lock()->cframe * (c0 * c1.Inverse()); CFrame newFrame = part0.lock()->cframe * (c0 * c1.Inverse());
part1.lock()->cframe = newFrame; part1.lock()->cframe = newFrame;
PhysJointSnapInfo jointInfo((c0.Inverse() * c1).Position()); PhysFixedJointInfo jointInfo(c0, c1);
this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock()); this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock());
jointWorkspace = workspace; jointWorkspace = workspace;
} }

View file

@ -23,7 +23,7 @@ void Weld::buildJoint() {
CFrame newFrame = part0.lock()->cframe * (c0 * c1.Inverse()); CFrame newFrame = part0.lock()->cframe * (c0 * c1.Inverse());
part1.lock()->cframe = newFrame; part1.lock()->cframe = newFrame;
PhysJointWeldInfo jointInfo((c0.Inverse() * c1).Position()); PhysFixedJointInfo jointInfo(c0, c1);
this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock()); this->joint = workspace->CreateJoint(jointInfo, part0.lock(), part1.lock());
jointWorkspace = workspace; jointWorkspace = workspace;
} }

View file

@ -285,6 +285,10 @@ void BasePart::MakeJoints() {
joint->part1 = otherPart->shared<BasePart>(); joint->part1 = otherPart->shared<BasePart>();
joint->c0 = contact0; joint->c0 = contact0;
joint->c1 = contact1; joint->c1 = contact1;
// If both parts touch directly, this can cause friction in Rotate and RotateV joints, so we leave a little extra space
if (joint->IsA("Rotate") || joint->IsA("RotateV"))
joint->c1 = joint->c1 + joint->c1.LookVector() * 0.02f,
joint->c0 = joint->c0 - joint->c0.LookVector() * 0.02f;
dataModel()->GetService<JointsService>()->AddChild(joint); dataModel()->GetService<JointsService>()->AddChild(joint);
joint->UpdateProperty("Part0"); joint->UpdateProperty("Part0");
} }

View file

@ -1,5 +1,4 @@
#include "world.h" #include "world.h"
#include "Jolt/Geometry/AABox.h"
#include "datatypes/vector.h" #include "datatypes/vector.h"
#include "enum/part.h" #include "enum/part.h"
#include "logger.h" #include "logger.h"
@ -27,6 +26,7 @@
#include <Jolt/Physics/Collision/Shape/ScaledShape.h> #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
#include <Jolt/Physics/Collision/Shape/ConvexHullShape.h> #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
#include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h> #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
#include <Jolt/Physics/Constraints/ConeConstraint.h>
#include <Jolt/Physics/EActivation.h> #include <Jolt/Physics/EActivation.h>
#include <Jolt/Physics/PhysicsSettings.h> #include <Jolt/Physics/PhysicsSettings.h>
#include <Jolt/RegisterTypes.h> #include <Jolt/RegisterTypes.h>
@ -37,7 +37,6 @@
#include <Jolt/Physics/Body/BodyLockInterface.h> #include <Jolt/Physics/Body/BodyLockInterface.h>
#include <Jolt/Physics/Collision/NarrowPhaseQuery.h> #include <Jolt/Physics/Collision/NarrowPhaseQuery.h>
#include <Jolt/Physics/Constraints/FixedConstraint.h> #include <Jolt/Physics/Constraints/FixedConstraint.h>
#include <cstdio>
#include <memory> #include <memory>
static JPH::TempAllocator* allocator; static JPH::TempAllocator* allocator;
@ -208,17 +207,24 @@ PhysJoint PhysWorld::createJoint(PhysJointInfo& type, std::shared_ptr<BasePart>
) { Logger::fatalError("Failed to create joint between two parts due to the call being invalid"); panic(); }; ) { Logger::fatalError("Failed to create joint between two parts due to the call being invalid"); panic(); };
JPH::TwoBodyConstraint* constraint; JPH::TwoBodyConstraint* constraint;
if (PhysJointGlueInfo* _ = dynamic_cast<PhysJointGlueInfo*>(&type)) { if (PhysFixedJointInfo* info = dynamic_cast<PhysFixedJointInfo*>(&type)) {
JPH::FixedConstraintSettings settings; JPH::FixedConstraintSettings settings;
settings.mAutoDetectPoint = true; // TODO: Replace this with anchor point settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
settings.mPoint1 = convert<JPH::Vec3>(info->c0.Position());
settings.mAxisX1 = convert<JPH::Vec3>(info->c0.RightVector());
settings.mAxisY1 = convert<JPH::Vec3>(info->c0.UpVector());
settings.mPoint2 = convert<JPH::Vec3>(info->c1.Position());
settings.mAxisX2 = convert<JPH::Vec3>(info->c1.RightVector());
settings.mAxisY2 = convert<JPH::Vec3>(info->c1.UpVector());
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl); constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl);
} else if (PhysJointWeldInfo* _ = dynamic_cast<PhysJointWeldInfo*>(&type)) { } else if (PhysHingeJointInfo* info = dynamic_cast<PhysHingeJointInfo*>(&type)) {
JPH::FixedConstraintSettings settings; JPH::ConeConstraintSettings settings;
settings.mAutoDetectPoint = true; // TODO: Replace this with anchor point settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl); settings.mPoint1 = convert<JPH::Vec3>(info->c0.Position());
} else if (PhysJointSnapInfo* _ = dynamic_cast<PhysJointSnapInfo*>(&type)) { settings.mTwistAxis1 = convert<JPH::Vec3>(info->c0.LookVector());
JPH::FixedConstraintSettings settings; settings.mPoint2 = convert<JPH::Vec3>(info->c1.Position());
settings.mAutoDetectPoint = true; // TODO: Replace this with anchor point settings.mTwistAxis2 = convert<JPH::Vec3>(info->c1.LookVector());
settings.mHalfConeAngle = 0.0f;
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl); constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl);
} else { } else {
panic(); panic();

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "datatypes/cframe.h"
#include "datatypes/vector.h" #include "datatypes/vector.h"
#include "enum/part.h" #include "enum/part.h"
#include "utils.h" #include "utils.h"
@ -16,11 +17,9 @@ class BasePart;
class PhysWorld; class PhysWorld;
struct PhysJointInfo { virtual ~PhysJointInfo() = default; protected: PhysJointInfo() = default; }; struct PhysJointInfo { virtual ~PhysJointInfo() = default; protected: PhysJointInfo() = default; };
struct PhysJointGlueInfo : PhysJointInfo { Vector3 anchorPoint; inline PhysJointGlueInfo(Vector3 anchorPoint) : anchorPoint(anchorPoint) {} }; struct PhysFixedJointInfo : PhysJointInfo { CFrame c0; CFrame c1; inline PhysFixedJointInfo(CFrame c0, CFrame c1) : c0(c0), c1(c1) {} };
struct PhysJointWeldInfo : PhysJointInfo { Vector3 anchorPoint; inline PhysJointWeldInfo(Vector3 anchorPoint) : anchorPoint(anchorPoint) {} }; struct PhysHingeJointInfo : PhysJointInfo { CFrame c0; CFrame c1; inline PhysHingeJointInfo(CFrame c0, CFrame c1) : c0(c0), c1(c1) {} };
struct PhysJointSnapInfo : PhysJointInfo { Vector3 anchorPoint; inline PhysJointSnapInfo(Vector3 anchorPoint) : anchorPoint(anchorPoint) {} }; struct PhysMotorJointInfo : PhysJointInfo { Vector3 anchorPoint; Vector3 rotationAxis; inline PhysMotorJointInfo(Vector3 anchorPoint, Vector3 rotationAxis) : anchorPoint(anchorPoint), rotationAxis(rotationAxis) {} };
struct PhysJointHingeInfo : PhysJointInfo { Vector3 anchorPoint; Vector3 rotationAxis; inline PhysJointHingeInfo(Vector3 anchorPoint, Vector3 rotationAxis) : anchorPoint(anchorPoint), rotationAxis(rotationAxis) {} };
struct PhysJointMotorInfo : PhysJointInfo { Vector3 anchorPoint; Vector3 rotationAxis; inline PhysJointMotorInfo(Vector3 anchorPoint, Vector3 rotationAxis) : anchorPoint(anchorPoint), rotationAxis(rotationAxis) {} };
class PhysWorld; class PhysWorld;
struct PhysJoint { struct PhysJoint {