From 3caad3f3c9e319ca67d63231e8c64b2ace855fff Mon Sep 17 00:00:00 2001 From: Dániel Juhász Date: Sat, 23 Jul 2016 21:11:20 +0200 Subject: Expose getPointedThing to Lua This commit introduces Raycast, a Lua user object, which can be used to perform a raycast on the map. The ray is continuable, so one can also get hidden nodes (for example to see trough glass). --- src/raycast.cpp | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/raycast.cpp') diff --git a/src/raycast.cpp b/src/raycast.cpp index 58102c993..42cc22587 100644 --- a/src/raycast.cpp +++ b/src/raycast.cpp @@ -17,11 +17,47 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "raycast.h" #include "irr_v3d.h" #include "irr_aabb3d.h" +#include "constants.h" + +bool RaycastSort::operator() (const PointedThing &pt1, + const PointedThing &pt2) const +{ + // "nothing" can not be sorted + assert(pt1.type != POINTEDTHING_NOTHING); + assert(pt2.type != POINTEDTHING_NOTHING); + // returns false if pt1 is nearer than pt2 + if (pt1.distanceSq < pt2.distanceSq) { + return false; + } else if (pt1.distanceSq == pt2.distanceSq) { + // Sort them to allow only one order + if (pt1.type == POINTEDTHING_OBJECT) + return (pt2.type == POINTEDTHING_OBJECT + && pt1.object_id < pt2.object_id); + else + return (pt2.type == POINTEDTHING_OBJECT + || pt1.node_undersurface < pt2.node_undersurface); + } + return true; +} + + +RaycastState::RaycastState(const core::line3d &shootline, + bool objects_pointable, bool liquids_pointable) : + m_shootline(shootline), + m_iterator(shootline.start / BS, shootline.getVector() / BS), + m_previous_node(m_iterator.m_current_node_pos), + m_objects_pointable(objects_pointable), + m_liquids_pointable(liquids_pointable) +{ +} + bool boxLineCollision(const aabb3f &box, const v3f &start, - const v3f &dir, v3f *collision_point, v3s16 *collision_normal) { + const v3f &dir, v3f *collision_point, v3s16 *collision_normal) +{ if (box.isPointInside(start)) { *collision_point = start; collision_normal->set(0, 0, 0); -- cgit v1.2.3