]> git.lizzy.rs Git - dragonfireclient.git/blob - src/raycast.cpp
Typo fix in compat code from commit 1d8d01074fdb52946f81110bebf1d001185b394b
[dragonfireclient.git] / src / raycast.cpp
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
25 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         // returns false if pt1 is nearer than pt2
32         if (pt1.distanceSq < pt2.distanceSq) {
33                 return false;
34         } else if (pt1.distanceSq == pt2.distanceSq) {
35                 // Sort them to allow only one order
36                 if (pt1.type == POINTEDTHING_OBJECT)
37                         return (pt2.type == POINTEDTHING_OBJECT
38                                 && pt1.object_id < pt2.object_id);
39                 else
40                         return (pt2.type == POINTEDTHING_OBJECT
41                                 || pt1.node_undersurface < pt2.node_undersurface);
42         }
43         return true;
44 }
45
46
47 RaycastState::RaycastState(const core::line3d<f32> &shootline,
48         bool objects_pointable, bool liquids_pointable) :
49         m_shootline(shootline),
50         m_iterator(shootline.start / BS, shootline.getVector() / BS),
51         m_previous_node(m_iterator.m_current_node_pos),
52         m_objects_pointable(objects_pointable),
53         m_liquids_pointable(liquids_pointable)
54 {
55 }
56
57
58 bool boxLineCollision(const aabb3f &box, const v3f &start,
59         const v3f &dir, v3f *collision_point, v3s16 *collision_normal)
60 {
61         if (box.isPointInside(start)) {
62                 *collision_point = start;
63                 collision_normal->set(0, 0, 0);
64                 return true;
65         }
66         float m = 0;
67
68         // Test X collision
69         if (dir.X != 0) {
70                 if (dir.X > 0)
71                         m = (box.MinEdge.X - start.X) / dir.X;
72                 else
73                         m = (box.MaxEdge.X - start.X) / dir.X;
74
75                 if (m >= 0 && m <= 1) {
76                         *collision_point = start + dir * m;
77                         if ((collision_point->Y >= box.MinEdge.Y)
78                                         && (collision_point->Y <= box.MaxEdge.Y)
79                                         && (collision_point->Z >= box.MinEdge.Z)
80                                         && (collision_point->Z <= box.MaxEdge.Z)) {
81                                 collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
82                                 return true;
83                         }
84                 }
85         }
86
87         // Test Y collision
88         if (dir.Y != 0) {
89                 if (dir.Y > 0)
90                         m = (box.MinEdge.Y - start.Y) / dir.Y;
91                 else
92                         m = (box.MaxEdge.Y - start.Y) / dir.Y;
93
94                 if (m >= 0 && m <= 1) {
95                         *collision_point = start + dir * m;
96                         if ((collision_point->X >= box.MinEdge.X)
97                                         && (collision_point->X <= box.MaxEdge.X)
98                                         && (collision_point->Z >= box.MinEdge.Z)
99                                         && (collision_point->Z <= box.MaxEdge.Z)) {
100                                 collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
101                                 return true;
102                         }
103                 }
104         }
105
106         // Test Z collision
107         if (dir.Z != 0) {
108                 if (dir.Z > 0)
109                         m = (box.MinEdge.Z - start.Z) / dir.Z;
110                 else
111                         m = (box.MaxEdge.Z - start.Z) / dir.Z;
112
113                 if (m >= 0 && m <= 1) {
114                         *collision_point = start + dir * m;
115                         if ((collision_point->X >= box.MinEdge.X)
116                                         && (collision_point->X <= box.MaxEdge.X)
117                                         && (collision_point->Y >= box.MinEdge.Y)
118                                         && (collision_point->Y <= box.MaxEdge.Y)) {
119                                 collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
120                                 return true;
121                         }
122                 }
123         }
124         return false;
125 }