fix(physics): motor6d relies on constraints

This commit is contained in:
maelstrom 2025-09-06 23:56:38 +02:00
parent 2a62884993
commit 73c5bc5a26
2 changed files with 8 additions and 20 deletions

View file

@ -44,21 +44,6 @@ void Motor6D::OnPhysicsStep(float deltaTime) {
// Shouldn't in theory be necessary, but just in case.
if (part0.expired() || part1.expired()) return;
if (!part1.lock()->anchored) {
CFrame anchorPoint = part0.lock()->cframe * c0;
Vector3 angles = anchorPoint.ToEulerAnglesXYZ();
CFrame rotatedAnchor = CFrame::FromEulerAnglesXYZ({angles.X(), angles.Y(), currentAngle}) + anchorPoint.Position();
CFrame newFrame = rotatedAnchor * c1.Inverse();
part1.lock()->cframe = newFrame;
part1.lock()->UpdateNoBreakJoints();
} else if (!part0.lock()->anchored) {
CFrame anchorPoint = part1.lock()->cframe * c1;
Vector3 angles = anchorPoint.ToEulerAnglesXYZ();
CFrame rotatedAnchor = CFrame::FromEulerAnglesXYZ({angles.X(), angles.Y(), currentAngle}) + anchorPoint.Position();
CFrame newFrame = rotatedAnchor * c0.Inverse();
part0.lock()->cframe = newFrame;
part0.lock()->UpdateNoBreakJoints();
}
// TODO: Re-add rotating only one part when both are unanchored, maybe?
joint.setTargetAngle(currentAngle);
}

View file

@ -234,14 +234,17 @@ PhysJoint PhysWorld::createJoint(PhysJointInfo& type, std::shared_ptr<BasePart>
settings.mPoint2 = convert<JPH::Vec3>(info->c1.Position());
settings.mNormalAxis2 = convert<JPH::Vec3>(info->c1.RightVector());
settings.mHingeAxis2 = convert<JPH::Vec3>(info->c1.LookVector());
// settings for Motor6D
settings.mMotorSettings.mSpringSettings.mFrequency = 20;
settings.mMotorSettings.mSpringSettings.mDamping = 1;
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl);
if (PhysMotorizedJointInfo* info = dynamic_cast<PhysMotorizedJointInfo*>(&type)) {
static_cast<JPH::HingeConstraint*>(constraint)->SetMotorState(JPH::EMotorState::Velocity);
static_cast<JPH::HingeConstraint*>(constraint)->SetTargetAngularVelocity(-info->initialVelocity);
} else if (PhysStepperJointInfo* info = dynamic_cast<PhysStepperJointInfo*>(&type)) {
static_cast<JPH::HingeConstraint*>(constraint)->SetMotorState(JPH::EMotorState::Velocity);
static_cast<JPH::HingeConstraint*>(constraint)->SetTargetAngularVelocity(0);
} else if (PhysStepperJointInfo* _ = dynamic_cast<PhysStepperJointInfo*>(&type)) {
static_cast<JPH::HingeConstraint*>(constraint)->SetMotorState(JPH::EMotorState::Position);
}
} else {
panic();