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