3 Copyright (C) 2016 juhdanad, Daniel Juhasz <juhdanad@gmail.com>
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.
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.
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.
21 #include "irr_aabb3d.h"
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);
35 m = (box.MinEdge.X - start.X) / dir.X;
37 m = (box.MaxEdge.X - start.X) / dir.X;
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);
54 m = (box.MinEdge.Y - start.Y) / dir.Y;
56 m = (box.MaxEdge.Y - start.Y) / dir.Y;
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);
73 m = (box.MinEdge.Z - start.Z) / dir.Z;
75 m = (box.MaxEdge.Z - start.Z) / dir.Z;
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);