From 18985e6f86221b51201756266168d123931dab27 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Mon, 31 Mar 2025 21:48:27 +0200 Subject: [PATCH] fix(editor): unsink dragged objects --- core/src/datatypes/vector.h | 4 ++++ editor/mainglwidget.cpp | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/core/src/datatypes/vector.h b/core/src/datatypes/vector.h index a9650d8..1a21724 100644 --- a/core/src/datatypes/vector.h +++ b/core/src/datatypes/vector.h @@ -51,3 +51,7 @@ namespace Data { bool operator ==(Data::Vector3) const; }; } + +inline void printVec(Data::Vector3 vec) { + printf("(%f, %f, %f)\n", vec.X(), vec.Y(), vec.Z()); +} \ No newline at end of file diff --git a/editor/mainglwidget.cpp b/editor/mainglwidget.cpp index 809e5c1..ccd65de 100644 --- a/editor/mainglwidget.cpp +++ b/editor/mainglwidget.cpp @@ -141,17 +141,18 @@ void MainGLWidget::handleObjectDrag(QMouseEvent* evt) { Data::CFrame targetFrame = partFromBody(rayHit->body)->cframe; Data::Vector3 surfaceNormal = targetFrame.Inverse().Rotation() * rayHit->worldNormal; // The part being dragged's frame local to the hit target's frame, but without its position component - Data::CFrame localFrame = (targetFrame.Inverse() * draggingObject->lock()->cframe).Rotation(); + // To find a world vector local to the new frame, use newFrame, not localFrame, as localFrame is localFrame is local to targetFrame in itself + Data::CFrame localFrame = (targetFrame.Inverse() * (draggingObject->lock()->cframe.Rotation() + vec)); // Snap axis localFrame = snapCFrame(localFrame); + Data::CFrame newFrame = targetFrame * localFrame; // Unsink the object // Get the normal of the surface relative to the part's frame, and get the size along that vector - Data::Vector3 partOffset = localFrame * ((localFrame.Inverse() * rayHit->worldNormal) * draggingObject->lock()->size / 2); + Data::Vector3 unsinkOffset = newFrame.Rotation() * ((newFrame.Rotation().Inverse() * rayHit->worldNormal) * draggingObject->lock()->size / 2); - Data::CFrame newFrame = targetFrame.Rotation() * localFrame; - draggingObject->lock()->cframe = newFrame + vec + partOffset; + draggingObject->lock()->cframe = newFrame + unsinkOffset; syncPartPhysics(draggingObject->lock()); }