3 Copyright (C) 2016 juhdanad, Daniel Juhasz <juhdanad@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "irr_aabb3d.h"
23 #include "constants.h"
25 bool RaycastSort::operator() (const PointedThing &pt1,
26 const PointedThing &pt2) const
28 // "nothing" can not be sorted
29 assert(pt1.type != POINTEDTHING_NOTHING);
30 assert(pt2.type != POINTEDTHING_NOTHING);
31 f32 pt1_distSq = pt1.distanceSq;
33 // Add some bonus when one of them is an object
34 if (pt1.type != pt2.type) {
35 if (pt1.type == POINTEDTHING_OBJECT)
36 pt1_distSq -= BS * BS;
37 else if (pt2.type == POINTEDTHING_OBJECT)
38 pt1_distSq += BS * BS;
41 // returns false if pt1 is nearer than pt2
42 if (pt1_distSq < pt2.distanceSq) {
46 if (pt1_distSq == pt2.distanceSq) {
47 // Sort them to allow only one order
48 if (pt1.type == POINTEDTHING_OBJECT)
49 return (pt2.type == POINTEDTHING_OBJECT
50 && pt1.object_id < pt2.object_id);
52 return (pt2.type == POINTEDTHING_OBJECT
53 || pt1.node_undersurface < pt2.node_undersurface);
59 RaycastState::RaycastState(const core::line3d<f32> &shootline,
60 bool objects_pointable, bool liquids_pointable) :
61 m_shootline(shootline),
62 m_iterator(shootline.start / BS, shootline.getVector() / BS),
63 m_previous_node(m_iterator.m_current_node_pos),
64 m_objects_pointable(objects_pointable),
65 m_liquids_pointable(liquids_pointable)
70 bool boxLineCollision(const aabb3f &box, const v3f &start,
71 const v3f &dir, v3f *collision_point, v3s16 *collision_normal)
73 if (box.isPointInside(start)) {
74 *collision_point = start;
75 collision_normal->set(0, 0, 0);
83 m = (box.MinEdge.X - start.X) / dir.X;
85 m = (box.MaxEdge.X - start.X) / dir.X;
87 if (m >= 0 && m <= 1) {
88 *collision_point = start + dir * m;
89 if ((collision_point->Y >= box.MinEdge.Y)
90 && (collision_point->Y <= box.MaxEdge.Y)
91 && (collision_point->Z >= box.MinEdge.Z)
92 && (collision_point->Z <= box.MaxEdge.Z)) {
93 collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
102 m = (box.MinEdge.Y - start.Y) / dir.Y;
104 m = (box.MaxEdge.Y - start.Y) / dir.Y;
106 if (m >= 0 && m <= 1) {
107 *collision_point = start + dir * m;
108 if ((collision_point->X >= box.MinEdge.X)
109 && (collision_point->X <= box.MaxEdge.X)
110 && (collision_point->Z >= box.MinEdge.Z)
111 && (collision_point->Z <= box.MaxEdge.Z)) {
112 collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
121 m = (box.MinEdge.Z - start.Z) / dir.Z;
123 m = (box.MaxEdge.Z - start.Z) / dir.Z;
125 if (m >= 0 && m <= 1) {
126 *collision_point = start + dir * m;
127 if ((collision_point->X >= box.MinEdge.X)
128 && (collision_point->X <= box.MaxEdge.X)
129 && (collision_point->Y >= box.MinEdge.Y)
130 && (collision_point->Y <= box.MaxEdge.Y)) {
131 collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);