diff options
author | Dániel Juhász <juhdanad@gmail.com> | 2017-01-04 19:18:40 +0100 |
---|---|---|
committer | est31 <est31@users.noreply.github.com> | 2017-01-04 19:18:40 +0100 |
commit | 3f8261830e0503cd59d8713d5c9aab12fc1491db (patch) | |
tree | b49d898815a6c2e692cfe2fc0978cfedd49cd34f /src/util/pointedthing.cpp | |
parent | 8aadc62856cc3789ed345ddf3870e311af60afe9 (diff) | |
download | minetest-3f8261830e0503cd59d8713d5c9aab12fc1491db.tar.gz minetest-3f8261830e0503cd59d8713d5c9aab12fc1491db.tar.bz2 minetest-3f8261830e0503cd59d8713d5c9aab12fc1491db.zip |
Improve getPointedThing() (#4346)
* Improved getPointedThing()
The new algorithm checks every node exactly once.
Now the point and normal vector of the collision is also returned in the
PointedThing (currently they are not used outside of the function).
Now the CNodeDefManager keeps the union of all possible nodeboxes, so
the raycast won't miss any nodes. Also if there are only small
nodeboxes, getPointedThing() is exceptionally fast.
Also adds unit test for VoxelLineIterator.
* Cleanup, code move
This commit moves getPointedThing() and
Client::getSelectedActiveObject() to ClientEnvironment.
The map nodes now can decide which neighbors they are connecting to
(MapNode::getNeighbors()).
Diffstat (limited to 'src/util/pointedthing.cpp')
-rw-r--r-- | src/util/pointedthing.cpp | 75 |
1 files changed, 33 insertions, 42 deletions
diff --git a/src/util/pointedthing.cpp b/src/util/pointedthing.cpp index cd13000b5..ed3d4bb67 100644 --- a/src/util/pointedthing.cpp +++ b/src/util/pointedthing.cpp @@ -27,29 +27,25 @@ PointedThing::PointedThing(): type(POINTEDTHING_NOTHING), node_undersurface(0,0,0), node_abovesurface(0,0,0), + node_real_undersurface(0,0,0), + intersection_point(0,0,0), + intersection_normal(0,0,0), object_id(-1) {} std::string PointedThing::dump() const { std::ostringstream os(std::ios::binary); - if(type == POINTEDTHING_NOTHING) - { + if (type == POINTEDTHING_NOTHING) { os<<"[nothing]"; - } - else if(type == POINTEDTHING_NODE) - { + } else if (type == 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) - { + } else if (type == POINTEDTHING_OBJECT) { os<<"[object "<<object_id<<"]"; - } - else - { + } else { os<<"[unknown PointedThing]"; } return os.str(); @@ -59,61 +55,56 @@ void PointedThing::serialize(std::ostream &os) const { writeU8(os, 0); // version writeU8(os, (u8)type); - if(type == POINTEDTHING_NOTHING) - { - // nothing - } - else if(type == POINTEDTHING_NODE) - { + switch (type) { + case POINTEDTHING_NOTHING: + break; + case POINTEDTHING_NODE: writeV3S16(os, node_undersurface); writeV3S16(os, node_abovesurface); - } - else if(type == POINTEDTHING_OBJECT) - { + break; + case POINTEDTHING_OBJECT: writeS16(os, object_id); + break; } } void PointedThing::deSerialize(std::istream &is) { int version = readU8(is); - if(version != 0) throw SerializationError( + if (version != 0) throw SerializationError( "unsupported PointedThing version"); type = (PointedThingType) readU8(is); - if(type == POINTEDTHING_NOTHING) - { - // nothing - } - else if(type == POINTEDTHING_NODE) - { + switch (type) { + case POINTEDTHING_NOTHING: + break; + case POINTEDTHING_NODE: node_undersurface = readV3S16(is); node_abovesurface = readV3S16(is); - } - else if(type == POINTEDTHING_OBJECT) - { + break; + case POINTEDTHING_OBJECT: object_id = readS16(is); - } - else - { - throw SerializationError( - "unsupported PointedThingType"); + break; + default: + throw SerializationError("unsupported PointedThingType"); } } bool PointedThing::operator==(const PointedThing &pt2) const { - if(type != pt2.type) + if (type != pt2.type) + { return false; - if(type == POINTEDTHING_NODE) + } + if (type == POINTEDTHING_NODE) { - if(node_undersurface != pt2.node_undersurface) - return false; - if(node_abovesurface != pt2.node_abovesurface) + if ((node_undersurface != pt2.node_undersurface) + || (node_abovesurface != pt2.node_abovesurface) + || (node_real_undersurface != pt2.node_real_undersurface)) return false; } - else if(type == POINTEDTHING_OBJECT) + else if (type == POINTEDTHING_OBJECT) { - if(object_id != pt2.object_id) + if (object_id != pt2.object_id) return false; } return true; |