]> git.lizzy.rs Git - dragonfireclient.git/blob - src/raycast.cpp
Don't include client/game.h on server build
[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         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
59 RaycastState::RaycastState(const core::line3d<f32> &shootline,
60         bool objects_pointable, bool liquids_pointable, bool nodes_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         m_nodes_pointable(nodes_pointable)
67 {
68 }
69
70
71 bool boxLineCollision(const aabb3f &box, const v3f &start,
72         const v3f &dir, v3f *collision_point, v3s16 *collision_normal)
73 {
74         if (box.isPointInside(start)) {
75                 *collision_point = start;
76                 collision_normal->set(0, 0, 0);
77                 return true;
78         }
79         float m = 0;
80
81         // Test X collision
82         if (dir.X != 0) {
83                 if (dir.X > 0)
84                         m = (box.MinEdge.X - start.X) / dir.X;
85                 else
86                         m = (box.MaxEdge.X - start.X) / dir.X;
87
88                 if (m >= 0 && m <= 1) {
89                         *collision_point = start + dir * m;
90                         if ((collision_point->Y >= box.MinEdge.Y)
91                                         && (collision_point->Y <= box.MaxEdge.Y)
92                                         && (collision_point->Z >= box.MinEdge.Z)
93                                         && (collision_point->Z <= box.MaxEdge.Z)) {
94                                 collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
95                                 return true;
96                         }
97                 }
98         }
99
100         // Test Y collision
101         if (dir.Y != 0) {
102                 if (dir.Y > 0)
103                         m = (box.MinEdge.Y - start.Y) / dir.Y;
104                 else
105                         m = (box.MaxEdge.Y - start.Y) / dir.Y;
106
107                 if (m >= 0 && m <= 1) {
108                         *collision_point = start + dir * m;
109                         if ((collision_point->X >= box.MinEdge.X)
110                                         && (collision_point->X <= box.MaxEdge.X)
111                                         && (collision_point->Z >= box.MinEdge.Z)
112                                         && (collision_point->Z <= box.MaxEdge.Z)) {
113                                 collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
114                                 return true;
115                         }
116                 }
117         }
118
119         // Test Z collision
120         if (dir.Z != 0) {
121                 if (dir.Z > 0)
122                         m = (box.MinEdge.Z - start.Z) / dir.Z;
123                 else
124                         m = (box.MaxEdge.Z - start.Z) / dir.Z;
125
126                 if (m >= 0 && m <= 1) {
127                         *collision_point = start + dir * m;
128                         if ((collision_point->X >= box.MinEdge.X)
129                                         && (collision_point->X <= box.MaxEdge.X)
130                                         && (collision_point->Y >= box.MinEdge.Y)
131                                         && (collision_point->Y <= box.MaxEdge.Y)) {
132                                 collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
133                                 return true;
134                         }
135                 }
136         }
137         return false;
138 }