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/voxelalgorithms.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/voxelalgorithms.cpp')
-rw-r--r-- | src/voxelalgorithms.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/voxelalgorithms.cpp b/src/voxelalgorithms.cpp index 93cc33acc..c20917164 100644 --- a/src/voxelalgorithms.cpp +++ b/src/voxelalgorithms.cpp @@ -747,5 +747,73 @@ void update_lighting_nodes(Map *map, INodeDefManager *ndef, } } +VoxelLineIterator::VoxelLineIterator( + const v3f &start_position, + const v3f &line_vector) : + m_start_position(start_position), + m_line_vector(line_vector), + m_next_intersection_multi(10000.0f, 10000.0f, 10000.0f), + m_intersection_multi_inc(10000.0f, 10000.0f, 10000.0f), + m_step_directions(1.0f, 1.0f, 1.0f) +{ + m_current_node_pos = floatToInt(m_start_position, 1); + + if (m_line_vector.X > 0) { + m_next_intersection_multi.X = (floorf(m_start_position.X - 0.5) + 1.5 + - m_start_position.X) / m_line_vector.X; + m_intersection_multi_inc.X = 1 / m_line_vector.X; + } else if (m_line_vector.X < 0) { + m_next_intersection_multi.X = (floorf(m_start_position.X - 0.5) + - m_start_position.X + 0.5) / m_line_vector.X; + m_intersection_multi_inc.X = -1 / m_line_vector.X; + m_step_directions.X = -1; + } + + if (m_line_vector.Y > 0) { + m_next_intersection_multi.Y = (floorf(m_start_position.Y - 0.5) + 1.5 + - m_start_position.Y) / m_line_vector.Y; + m_intersection_multi_inc.Y = 1 / m_line_vector.Y; + } else if (m_line_vector.Y < 0) { + m_next_intersection_multi.Y = (floorf(m_start_position.Y - 0.5) + - m_start_position.Y + 0.5) / m_line_vector.Y; + m_intersection_multi_inc.Y = -1 / m_line_vector.Y; + m_step_directions.Y = -1; + } + + if (m_line_vector.Z > 0) { + m_next_intersection_multi.Z = (floorf(m_start_position.Z - 0.5) + 1.5 + - m_start_position.Z) / m_line_vector.Z; + m_intersection_multi_inc.Z = 1 / m_line_vector.Z; + } else if (m_line_vector.Z < 0) { + m_next_intersection_multi.Z = (floorf(m_start_position.Z - 0.5) + - m_start_position.Z + 0.5) / m_line_vector.Z; + m_intersection_multi_inc.Z = -1 / m_line_vector.Z; + m_step_directions.Z = -1; + } + + m_has_next = (m_next_intersection_multi.X <= 1) + || (m_next_intersection_multi.Y <= 1) + || (m_next_intersection_multi.Z <= 1); +} + +void VoxelLineIterator::next() +{ + if ((m_next_intersection_multi.X < m_next_intersection_multi.Y) + && (m_next_intersection_multi.X < m_next_intersection_multi.Z)) { + m_next_intersection_multi.X += m_intersection_multi_inc.X; + m_current_node_pos.X += m_step_directions.X; + } else if ((m_next_intersection_multi.Y < m_next_intersection_multi.Z)) { + m_next_intersection_multi.Y += m_intersection_multi_inc.Y; + m_current_node_pos.Y += m_step_directions.Y; + } else { + m_next_intersection_multi.Z += m_intersection_multi_inc.Z; + m_current_node_pos.Z += m_step_directions.Z; + } + + m_has_next = (m_next_intersection_multi.X <= 1) + || (m_next_intersection_multi.Y <= 1) + || (m_next_intersection_multi.Z <= 1); +} + } // namespace voxalgo |