diff options
author | Lars Müller <34514239+appgurueu@users.noreply.github.com> | 2022-10-30 16:53:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-30 16:53:14 +0100 |
commit | 077627181ee2eac3c0dacc3d8dc49825837e474c (patch) | |
tree | a8a6298198738f2edb30bd7e733a7a0d2005affa /src/raycast.cpp | |
parent | b8292319924994352d56d6111faa73fe315d149a (diff) | |
download | minetest-077627181ee2eac3c0dacc3d8dc49825837e474c.tar.xz |
Allow rotating entity selectionboxes (#12379)
Diffstat (limited to 'src/raycast.cpp')
-rw-r--r-- | src/raycast.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/raycast.cpp b/src/raycast.cpp index ebc40235d..ead024dd1 100644 --- a/src/raycast.cpp +++ b/src/raycast.cpp @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "raycast.h" #include "irr_v3d.h" #include "irr_aabb3d.h" +#include <quaternion.h> #include "constants.h" bool RaycastSort::operator() (const PointedThing &pt1, @@ -68,7 +69,7 @@ RaycastState::RaycastState(const core::line3d<f32> &shootline, bool boxLineCollision(const aabb3f &box, const v3f &start, - const v3f &dir, v3f *collision_point, v3s16 *collision_normal) + const v3f &dir, v3f *collision_point, v3f *collision_normal) { if (box.isPointInside(start)) { *collision_point = start; @@ -135,3 +136,23 @@ bool boxLineCollision(const aabb3f &box, const v3f &start, } return false; } + +bool boxLineCollision(const aabb3f &box, const v3f &rotation, + const v3f &start, const v3f &dir, + v3f *collision_point, v3f *collision_normal, v3f *raw_collision_normal) +{ + // Inversely transform the ray rather than rotating the box faces; + // this allows us to continue using a simple ray - AABB intersection + core::quaternion rot(rotation * core::DEGTORAD); + rot.makeInverse(); + + bool collision = boxLineCollision(box, rot * start, rot * dir, collision_point, collision_normal); + if (!collision) return collision; + + // Transform the results back + rot.makeInverse(); + *collision_point = rot * *collision_point; + *raw_collision_normal = *collision_normal; + *collision_normal = rot * *collision_normal; + return collision; +} |