Fix alpha for liquid nodes (#5494)
[oweals/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 "irr_v3d.h"
21 #include "irr_aabb3d.h"
22
23 bool boxLineCollision(const aabb3f &box, const v3f &start,
24                 const v3f &dir, v3f *collision_point, v3s16 *collision_normal) {
25         if (box.isPointInside(start)) {
26                 *collision_point = start;
27                 collision_normal->set(0, 0, 0);
28                 return true;
29         }
30         float m = 0;
31
32         // Test X collision
33         if (dir.X != 0) {
34                 if (dir.X > 0)
35                         m = (box.MinEdge.X - start.X) / dir.X;
36                 else
37                         m = (box.MaxEdge.X - start.X) / dir.X;
38
39                 if (m >= 0 && m <= 1) {
40                         *collision_point = start + dir * m;
41                         if ((collision_point->Y >= box.MinEdge.Y)
42                                         && (collision_point->Y <= box.MaxEdge.Y)
43                                         && (collision_point->Z >= box.MinEdge.Z)
44                                         && (collision_point->Z <= box.MaxEdge.Z)) {
45                                 collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
46                                 return true;
47                         }
48                 }
49         }
50
51         // Test Y collision
52         if (dir.Y != 0) {
53                 if (dir.Y > 0)
54                         m = (box.MinEdge.Y - start.Y) / dir.Y;
55                 else
56                         m = (box.MaxEdge.Y - start.Y) / dir.Y;
57
58                 if (m >= 0 && m <= 1) {
59                         *collision_point = start + dir * m;
60                         if ((collision_point->X >= box.MinEdge.X)
61                                         && (collision_point->X <= box.MaxEdge.X)
62                                         && (collision_point->Z >= box.MinEdge.Z)
63                                         && (collision_point->Z <= box.MaxEdge.Z)) {
64                                 collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
65                                 return true;
66                         }
67                 }
68         }
69
70         // Test Z collision
71         if (dir.Z != 0) {
72                 if (dir.Z > 0)
73                         m = (box.MinEdge.Z - start.Z) / dir.Z;
74                 else
75                         m = (box.MaxEdge.Z - start.Z) / dir.Z;
76
77                 if (m >= 0 && m <= 1) {
78                         *collision_point = start + dir * m;
79                         if ((collision_point->X >= box.MinEdge.X)
80                                         && (collision_point->X <= box.MaxEdge.X)
81                                         && (collision_point->Y >= box.MinEdge.Y)
82                                         && (collision_point->Y <= box.MaxEdge.Y)) {
83                                 collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
84                                 return true;
85                         }
86                 }
87         }
88         return false;
89 }