aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorDániel Juhász <juhdanad@gmail.com>2016-07-23 21:11:20 +0200
committerparamat <mat.gregory@virginmedia.com>2017-07-07 22:28:23 +0100
commit3caad3f3c9e319ca67d63231e8c64b2ace855fff (patch)
treef3cb283b7aa28958e2deec7c70dad3a85e1236d4 /src/util
parenta80ecbee1e838491343af760539a37fac4232048 (diff)
downloadminetest-3caad3f3c9e319ca67d63231e8c64b2ace855fff.tar.gz
minetest-3caad3f3c9e319ca67d63231e8c64b2ace855fff.tar.bz2
minetest-3caad3f3c9e319ca67d63231e8c64b2ace855fff.zip
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).
Diffstat (limited to 'src/util')
-rw-r--r--src/util/pointedthing.cpp46
-rw-r--r--src/util/pointedthing.h18
2 files changed, 51 insertions, 13 deletions
diff --git a/src/util/pointedthing.cpp b/src/util/pointedthing.cpp
index f1f1d3f20..e5c5dcf4c 100644
--- a/src/util/pointedthing.cpp
+++ b/src/util/pointedthing.cpp
@@ -23,20 +23,47 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "../exceptions.h"
#include <sstream>
+PointedThing::PointedThing(const v3s16 &under, const v3s16 &above,
+ const v3s16 &real_under, const v3f &point, const v3s16 &normal,
+ f32 distSq):
+ type(POINTEDTHING_NODE),
+ node_undersurface(under),
+ node_abovesurface(above),
+ node_real_undersurface(real_under),
+ intersection_point(point),
+ intersection_normal(normal),
+ distanceSq(distSq)
+{}
+
+PointedThing::PointedThing(s16 id, const v3f &point, const v3s16 &normal,
+ f32 distSq) :
+ type(POINTEDTHING_OBJECT),
+ object_id(id),
+ intersection_point(point),
+ intersection_normal(normal),
+ distanceSq(distSq)
+{}
+
std::string PointedThing::dump() const
{
std::ostringstream os(std::ios::binary);
- if (type == POINTEDTHING_NOTHING) {
- os<<"[nothing]";
- } else if (type == POINTEDTHING_NODE) {
+ switch (type) {
+ case POINTEDTHING_NOTHING:
+ os << "[nothing]";
+ break;
+ case POINTEDTHING_NODE:
+ {
const v3s16 &u = node_undersurface;
const v3s16 &a = node_abovesurface;
- os<<"[node under="<<u.X<<","<<u.Y<<","<<u.Z
- << " above="<<a.X<<","<<a.Y<<","<<a.Z<<"]";
- } else if (type == POINTEDTHING_OBJECT) {
- os<<"[object "<<object_id<<"]";
- } else {
- os<<"[unknown PointedThing]";
+ os << "[node under=" << u.X << "," << u.Y << "," << u.Z << " above="
+ << a.X << "," << a.Y << "," << a.Z << "]";
+ }
+ break;
+ case POINTEDTHING_OBJECT:
+ os << "[object " << object_id << "]";
+ break;
+ default:
+ os << "[unknown PointedThing]";
}
return os.str();
}
@@ -104,4 +131,3 @@ bool PointedThing::operator!=(const PointedThing &pt2) const
{
return !(*this == pt2);
}
-
diff --git a/src/util/pointedthing.h b/src/util/pointedthing.h
index 92c33968f..f63bcad9d 100644
--- a/src/util/pointedthing.h
+++ b/src/util/pointedthing.h
@@ -59,6 +59,11 @@ struct PointedThing
*/
v3s16 node_real_undersurface;
/*!
+ * Only valid if type is POINTEDTHING_OBJECT.
+ * The ID of the object the ray hit.
+ */
+ s16 object_id = -1;
+ /*!
* Only valid if type isn't POINTEDTHING_NONE.
* First intersection point of the ray and the nodebox.
*/
@@ -71,12 +76,19 @@ struct PointedThing
*/
v3s16 intersection_normal;
/*!
- * Only valid if type is POINTEDTHING_OBJECT.
- * The ID of the object the ray hit.
+ * Square of the distance between the pointing
+ * ray's start point and the intersection point.
*/
- s16 object_id = -1;
+ f32 distanceSq = 0;
+ //! Constructor for POINTEDTHING_NOTHING
PointedThing() {};
+ //! Constructor for POINTEDTHING_NODE
+ PointedThing(const v3s16 &under, const v3s16 &above,
+ const v3s16 &real_under, const v3f &point, const v3s16 &normal,
+ f32 distSq);
+ //! Constructor for POINTEDTHING_OBJECT
+ PointedThing(s16 id, const v3f &point, const v3s16 &normal, f32 distSq);
std::string dump() const;
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);