From 281c424b879d587cbc8fa07a7c429a35e9f09303 Mon Sep 17 00:00:00 2001 From: Linloir <3145078758@qq.com> Date: Tue, 20 Dec 2022 08:29:44 +0800 Subject: [PATCH] [CORE][FIX] Surface sticking fixed --- FinalProject/sceneviewer.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/FinalProject/sceneviewer.cpp b/FinalProject/sceneviewer.cpp index 79f0811..9f8f2ba 100644 --- a/FinalProject/sceneviewer.cpp +++ b/FinalProject/sceneviewer.cpp @@ -567,21 +567,20 @@ void SceneViewer::moveOperatingObject(const Ray& ray) { // Move the object so that the bottom center of the object is at the hit point else if (_stickToSurface) { // Set the bottom center of the model at local origin - glm::vec3 bottomCenter = _operatingObject->boundary().bottomCenterPoint(); + glm::vec3 bottomCenter = _operatingObject->modelMatrix() * glm::vec4(_operatingObject->model()->boundBox().bottomCenterPoint(), 1.0f); glm::vec3 modelCenter = _operatingObject->modelMatrix() * glm::vec4(glm::vec3(0.0f), 1.0f); // model center in world space // Rotate the model to align with the surface normal glm::vec3 normal = _hitRecord.normal(); glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); - glm::vec3 axis = glm::cross(up, normal); - float angle = glm::acos(glm::dot(up, normal)); - _operatingObject->setRotation(axis, angle); + glm::vec3 axis = glm::normalize(glm::cross(up, normal)); + float deg = glm::degrees(glm::acos(glm::dot(up, normal))); + _operatingObject->setRotation(axis, deg); // Move the model to the hit point - glm::vec3 hitPoint = _hitRecord.position(); - glm::vec3 newCenter = hitPoint + normal * (modelCenter.y - bottomCenter.y); + glm::vec3 target = _hitRecord.position(); - // Move the model to the new center + glm::vec3 newCenter = target + modelCenter - bottomCenter; _operatingObject->setPosition(newCenter); // Update the boundary