1 /*
2 Minetest
3 Copyright (C) 2016 juhdanad, Daniel Juhasz <juhdanad@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "raycast.h"
21 #include "irr_v3d.h"
22 #include "irr_aabb3d.h"
23 #include "constants.h"
24
operator ()(const PointedThing & pt1,const PointedThing & pt2) const25 bool RaycastSort::operator() (const PointedThing &pt1,
26 const PointedThing &pt2) const
27 {
28 // "nothing" can not be sorted
29 assert(pt1.type != POINTEDTHING_NOTHING);
30 assert(pt2.type != POINTEDTHING_NOTHING);
31 f32 pt1_distSq = pt1.distanceSq;
32
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;
39 }
40
41 // returns false if pt1 is nearer than pt2
42 if (pt1_distSq < pt2.distanceSq) {
43 return false;
44 }
45
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);
51
52 return (pt2.type == POINTEDTHING_OBJECT
53 || pt1.node_undersurface < pt2.node_undersurface);
54 }
55 return true;
56 }
57
58
RaycastState(const core::line3d<f32> & shootline,bool objects_pointable,bool liquids_pointable)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)
66 {
67 }
68
69
boxLineCollision(const aabb3f & box,const v3f & start,const v3f & dir,v3f * collision_point,v3s16 * collision_normal)70 bool boxLineCollision(const aabb3f &box, const v3f &start,
71 const v3f &dir, v3f *collision_point, v3s16 *collision_normal)
72 {
73 if (box.isPointInside(start)) {
74 *collision_point = start;
75 collision_normal->set(0, 0, 0);
76 return true;
77 }
78 float m = 0;
79
80 // Test X collision
81 if (dir.X != 0) {
82 if (dir.X > 0)
83 m = (box.MinEdge.X - start.X) / dir.X;
84 else
85 m = (box.MaxEdge.X - start.X) / dir.X;
86
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);
94 return true;
95 }
96 }
97 }
98
99 // Test Y collision
100 if (dir.Y != 0) {
101 if (dir.Y > 0)
102 m = (box.MinEdge.Y - start.Y) / dir.Y;
103 else
104 m = (box.MaxEdge.Y - start.Y) / dir.Y;
105
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);
113 return true;
114 }
115 }
116 }
117
118 // Test Z collision
119 if (dir.Z != 0) {
120 if (dir.Z > 0)
121 m = (box.MinEdge.Z - start.Z) / dir.Z;
122 else
123 m = (box.MaxEdge.Z - start.Z) / dir.Z;
124
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);
132 return true;
133 }
134 }
135 }
136 return false;
137 }
138