feat(physics): hinges 1

This commit is contained in:
maelstrom 2025-09-01 20:31:49 +02:00
parent bea19b21d7
commit 219ca94ded
5 changed files with 23 additions and 16 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
PhysHingeJointInfo jointInfo(c0, c1); PhysRotatingJointInfo 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
PhysMotorJointInfo jointInfo((part0.lock()->cframe * c0).Position(), -(part0.lock()->cframe * c0).LookVector().Unit()); PhysRotatingJointInfo jointInfo(c0, c1, true);
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,10 +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 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")) // if (joint->IsA("Rotate") || joint->IsA("RotateV"))
joint->c1 = joint->c1 + joint->c1.LookVector() * 0.02f, // joint->c1 = joint->c1 + joint->c1.LookVector() * 0.02f,
joint->c0 = joint->c0 - joint->c0.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

@ -26,7 +26,6 @@
#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,6 +36,7 @@
#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 <Jolt/Physics/Constraints/SixDOFConstraint.h>
#include <memory> #include <memory>
static JPH::TempAllocator* allocator; static JPH::TempAllocator* allocator;
@ -217,15 +217,23 @@ PhysJoint PhysWorld::createJoint(PhysJointInfo& type, std::shared_ptr<BasePart>
settings.mAxisX2 = convert<JPH::Vec3>(info->c1.RightVector()); settings.mAxisX2 = convert<JPH::Vec3>(info->c1.RightVector());
settings.mAxisY2 = convert<JPH::Vec3>(info->c1.UpVector()); 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 (PhysHingeJointInfo* info = dynamic_cast<PhysHingeJointInfo*>(&type)) { } else if (PhysRotatingJointInfo* info = dynamic_cast<PhysRotatingJointInfo*>(&type)) {
JPH::ConeConstraintSettings settings; JPH::SixDOFConstraintSettings settings;
settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM; settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
settings.mPoint1 = convert<JPH::Vec3>(info->c0.Position()); settings.mPosition1 = convert<JPH::Vec3>(info->c0.Position());
settings.mTwistAxis1 = convert<JPH::Vec3>(info->c0.LookVector()); settings.mAxisX1 = convert<JPH::Vec3>(info->c0.RightVector());
settings.mPoint2 = convert<JPH::Vec3>(info->c1.Position()); settings.mAxisY1 = convert<JPH::Vec3>(info->c0.UpVector());
settings.mTwistAxis2 = convert<JPH::Vec3>(info->c1.LookVector()); settings.mPosition2 = convert<JPH::Vec3>(info->c1.Position());
settings.mHalfConeAngle = 0.0f; settings.mAxisX2 = convert<JPH::Vec3>(info->c1.RightVector());
settings.mAxisY2 = convert<JPH::Vec3>(info->c1.UpVector());
settings.MakeFixedAxis(JPH::SixDOFConstraintSettings::RotationX);
settings.MakeFixedAxis(JPH::SixDOFConstraintSettings::RotationY);
settings.MakeFixedAxis(JPH::SixDOFConstraintSettings::TranslationX);
settings.MakeFixedAxis(JPH::SixDOFConstraintSettings::TranslationY);
settings.MakeFixedAxis(JPH::SixDOFConstraintSettings::TranslationZ);
settings.mMotorSettings[JPH::SixDOFConstraintSettings::EAxis::RotationZ] = JPH::MotorSettings(5.f, 0.f);
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl); constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl);
// static_cast<JPH::SixDOFConstraint*>(constraint)->SetMotorState(JPH::SixDOFConstraintSettings::EAxis::RotationZ, JPH::EMotorState::Velocity);
} else { } else {
panic(); panic();
} }

View file

@ -18,8 +18,7 @@ class PhysWorld;
struct PhysJointInfo { virtual ~PhysJointInfo() = default; protected: PhysJointInfo() = default; }; struct PhysJointInfo { virtual ~PhysJointInfo() = default; protected: PhysJointInfo() = default; };
struct PhysFixedJointInfo : PhysJointInfo { CFrame c0; CFrame c1; inline PhysFixedJointInfo(CFrame c0, CFrame c1) : c0(c0), c1(c1) {} }; struct PhysFixedJointInfo : PhysJointInfo { CFrame c0; CFrame c1; inline PhysFixedJointInfo(CFrame c0, CFrame c1) : c0(c0), c1(c1) {} };
struct PhysHingeJointInfo : PhysJointInfo { CFrame c0; CFrame c1; inline PhysHingeJointInfo(CFrame c0, CFrame c1) : c0(c0), c1(c1) {} }; struct PhysRotatingJointInfo : PhysJointInfo { CFrame c0; CFrame c1; bool motorized; inline PhysRotatingJointInfo(CFrame c0, CFrame c1, bool motorized = false) : c0(c0), c1(c1), motorized(motorized) {} };
struct PhysMotorJointInfo : PhysJointInfo { Vector3 anchorPoint; Vector3 rotationAxis; inline PhysMotorJointInfo(Vector3 anchorPoint, Vector3 rotationAxis) : anchorPoint(anchorPoint), rotationAxis(rotationAxis) {} };
class PhysWorld; class PhysWorld;
struct PhysJoint { struct PhysJoint {