fix(part): hinge joint only made if other part intersects the hinge

This commit is contained in:
maelstrom 2025-04-30 17:20:04 +02:00
parent dcc1f64354
commit df6e2ac98d
3 changed files with 7 additions and 5 deletions

View file

@ -28,7 +28,7 @@ void Rotate::buildJoint() {
part1.lock()->cframe = newFrame; part1.lock()->cframe = newFrame;
workspace->SyncPartPhysics(part1.lock()); workspace->SyncPartPhysics(part1.lock());
// 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
rp::HingeJointInfo jointInfo(part0.lock()->rigidBody, part1.lock()->rigidBody, newFrame.Position(), -(part0.lock()->cframe * c0).LookVector().Unit()); rp::HingeJointInfo jointInfo(part0.lock()->rigidBody, part1.lock()->rigidBody, (part0.lock()->cframe * c0).Position(), -(part0.lock()->cframe * c0).LookVector().Unit());
this->joint = dynamic_cast<rp::HingeJoint*>(workspace->physicsWorld->createJoint(jointInfo)); this->joint = dynamic_cast<rp::HingeJoint*>(workspace->physicsWorld->createJoint(jointInfo));
jointWorkspace = workspace; jointWorkspace = workspace;

View file

@ -183,8 +183,7 @@ bool Part::checkJointContinuityUp(std::shared_ptr<Part> otherPart) {
return true; return true;
} }
bool Part::checkSurfacesTouching(CFrame surfaceFrame, Vector3 myFace, Vector3 otherFace, std::shared_ptr<Part> otherPart) { bool Part::checkSurfacesTouching(CFrame surfaceFrame, Vector3 size, Vector3 myFace, Vector3 otherFace, std::shared_ptr<Part> otherPart) {
// Vector3 otherPartCenterToMine = surfaceFrame.Inverse() * otherPart->cframe.Position();
Vector3 farCorner0 = surfaceFrame.Inverse() * otherPart->cframe * (Vector3::ONE * (otherPart->size / 2.f)); Vector3 farCorner0 = surfaceFrame.Inverse() * otherPart->cframe * (Vector3::ONE * (otherPart->size / 2.f));
Vector3 farCorner1 = surfaceFrame.Inverse() * otherPart->cframe * (-Vector3::ONE * (otherPart->size / 2.f)); Vector3 farCorner1 = surfaceFrame.Inverse() * otherPart->cframe * (-Vector3::ONE * (otherPart->size / 2.f));
@ -251,12 +250,15 @@ void Part::MakeJoints() {
if (dot > -0.99) continue; // Surface is pointing opposite to ours if (dot > -0.99) continue; // Surface is pointing opposite to ours
if (abs(surfacePointLocalToMyFrame.Z()) > 0.05) continue; // Surfaces are within 0.05 studs of one another if (abs(surfacePointLocalToMyFrame.Z()) > 0.05) continue; // Surfaces are within 0.05 studs of one another
if (!checkSurfacesTouching(surfaceFrame, myFace, otherFace, otherPart)) continue; // Surface do not overlap if (!checkSurfacesTouching(surfaceFrame, size, myFace, otherFace, otherPart)) continue; // Surface do not overlap
if (!checkJointContinuity(otherPart)) continue; if (!checkJointContinuity(otherPart)) continue;
SurfaceType mySurface = surfaceFromFace(faceFromNormal(myFace)); SurfaceType mySurface = surfaceFromFace(faceFromNormal(myFace));
SurfaceType otherSurface = surfaceFromFace(faceFromNormal(otherFace)); SurfaceType otherSurface = surfaceFromFace(faceFromNormal(otherFace));
// If it is a hinge, only attach if actually touching the "hinge"
if (mySurface == SurfaceHinge && !checkSurfacesTouching(surfaceFrame, Vector3(0.4, 0.4, 0.4), myFace, otherFace, otherPart)) continue;
// Create contacts // Create contacts
// Contact always occurs at the center of Part0's surface (even if that point does not overlap both surfaces) // Contact always occurs at the center of Part0's surface (even if that point does not overlap both surfaces)
// Contact 0 is Part0's contact relative to Part0. It should point *opposite* the direction of its surface normal // Contact 0 is Part0's contact relative to Part0. It should point *opposite* the direction of its surface normal

View file

@ -43,7 +43,7 @@ protected:
bool checkJointContinuity(std::shared_ptr<Part>); bool checkJointContinuity(std::shared_ptr<Part>);
bool checkJointContinuityUp(std::shared_ptr<Part>); bool checkJointContinuityUp(std::shared_ptr<Part>);
bool checkJointContinuityDown(std::shared_ptr<Part>); bool checkJointContinuityDown(std::shared_ptr<Part>);
bool checkSurfacesTouching(CFrame surfaceFrame, Vector3 myFace, Vector3 otherFace, std::shared_ptr<Part> otherPart); bool checkSurfacesTouching(CFrame surfaceFrame, Vector3 size, Vector3 myFace, Vector3 otherFace, std::shared_ptr<Part> otherPart);
friend JointInstance; friend JointInstance;