2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2016 juhdanad, Daniel Juhasz <juhdanad@gmail.com>
|
2017-01-04 19:18:40 +01:00
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
#include "raycast.h"
|
2017-01-04 19:18:40 +01:00
|
|
|
#include "irr_v3d.h"
|
|
|
|
#include "irr_aabb3d.h"
|
2022-10-30 16:53:14 +01:00
|
|
|
#include <quaternion.h>
|
2016-07-23 21:11:20 +02:00
|
|
|
#include "constants.h"
|
|
|
|
|
|
|
|
bool RaycastSort::operator() (const PointedThing &pt1,
|
|
|
|
const PointedThing &pt2) const
|
|
|
|
{
|
2022-11-09 17:57:19 +01:00
|
|
|
// "nothing" cannot be sorted
|
2016-07-23 21:11:20 +02:00
|
|
|
assert(pt1.type != POINTEDTHING_NOTHING);
|
|
|
|
assert(pt2.type != POINTEDTHING_NOTHING);
|
2018-10-26 19:23:33 +02:00
|
|
|
f32 pt1_distSq = pt1.distanceSq;
|
|
|
|
|
|
|
|
// Add some bonus when one of them is an object
|
|
|
|
if (pt1.type != pt2.type) {
|
|
|
|
if (pt1.type == POINTEDTHING_OBJECT)
|
|
|
|
pt1_distSq -= BS * BS;
|
|
|
|
else if (pt2.type == POINTEDTHING_OBJECT)
|
|
|
|
pt1_distSq += BS * BS;
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
// returns false if pt1 is nearer than pt2
|
2018-10-26 19:23:33 +02:00
|
|
|
if (pt1_distSq < pt2.distanceSq) {
|
2016-07-23 21:11:20 +02:00
|
|
|
return false;
|
2017-08-19 14:25:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 19:23:33 +02:00
|
|
|
if (pt1_distSq == pt2.distanceSq) {
|
2016-07-23 21:11:20 +02:00
|
|
|
// Sort them to allow only one order
|
|
|
|
if (pt1.type == POINTEDTHING_OBJECT)
|
|
|
|
return (pt2.type == POINTEDTHING_OBJECT
|
|
|
|
&& pt1.object_id < pt2.object_id);
|
2017-08-19 14:25:35 +02:00
|
|
|
|
|
|
|
return (pt2.type == POINTEDTHING_OBJECT
|
2016-07-23 21:11:20 +02:00
|
|
|
|| pt1.node_undersurface < pt2.node_undersurface);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RaycastState::RaycastState(const core::line3d<f32> &shootline,
|
2024-01-22 18:27:08 +01:00
|
|
|
bool objects_pointable, bool liquids_pointable,
|
|
|
|
const std::optional<Pointabilities> &pointabilities) :
|
2016-07-23 21:11:20 +02:00
|
|
|
m_shootline(shootline),
|
|
|
|
m_iterator(shootline.start / BS, shootline.getVector() / BS),
|
|
|
|
m_previous_node(m_iterator.m_current_node_pos),
|
|
|
|
m_objects_pointable(objects_pointable),
|
2024-01-22 18:27:08 +01:00
|
|
|
m_liquids_pointable(liquids_pointable),
|
|
|
|
m_pointabilities(pointabilities)
|
2016-07-23 21:11:20 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-04 19:18:40 +01:00
|
|
|
|
2024-03-12 14:13:24 +01:00
|
|
|
bool boxLineCollision(const aabb3f &box, const v3f start,
|
|
|
|
const v3f dir, v3f *collision_point, v3f *collision_normal)
|
2016-07-23 21:11:20 +02:00
|
|
|
{
|
2017-01-04 19:18:40 +01:00
|
|
|
if (box.isPointInside(start)) {
|
|
|
|
*collision_point = start;
|
|
|
|
collision_normal->set(0, 0, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
float m = 0;
|
|
|
|
|
|
|
|
// Test X collision
|
|
|
|
if (dir.X != 0) {
|
|
|
|
if (dir.X > 0)
|
|
|
|
m = (box.MinEdge.X - start.X) / dir.X;
|
|
|
|
else
|
|
|
|
m = (box.MaxEdge.X - start.X) / dir.X;
|
|
|
|
|
|
|
|
if (m >= 0 && m <= 1) {
|
|
|
|
*collision_point = start + dir * m;
|
|
|
|
if ((collision_point->Y >= box.MinEdge.Y)
|
|
|
|
&& (collision_point->Y <= box.MaxEdge.Y)
|
|
|
|
&& (collision_point->Z >= box.MinEdge.Z)
|
|
|
|
&& (collision_point->Z <= box.MaxEdge.Z)) {
|
|
|
|
collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Y collision
|
|
|
|
if (dir.Y != 0) {
|
|
|
|
if (dir.Y > 0)
|
|
|
|
m = (box.MinEdge.Y - start.Y) / dir.Y;
|
|
|
|
else
|
|
|
|
m = (box.MaxEdge.Y - start.Y) / dir.Y;
|
|
|
|
|
|
|
|
if (m >= 0 && m <= 1) {
|
|
|
|
*collision_point = start + dir * m;
|
|
|
|
if ((collision_point->X >= box.MinEdge.X)
|
|
|
|
&& (collision_point->X <= box.MaxEdge.X)
|
|
|
|
&& (collision_point->Z >= box.MinEdge.Z)
|
|
|
|
&& (collision_point->Z <= box.MaxEdge.Z)) {
|
|
|
|
collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Z collision
|
|
|
|
if (dir.Z != 0) {
|
|
|
|
if (dir.Z > 0)
|
|
|
|
m = (box.MinEdge.Z - start.Z) / dir.Z;
|
|
|
|
else
|
|
|
|
m = (box.MaxEdge.Z - start.Z) / dir.Z;
|
|
|
|
|
|
|
|
if (m >= 0 && m <= 1) {
|
|
|
|
*collision_point = start + dir * m;
|
|
|
|
if ((collision_point->X >= box.MinEdge.X)
|
|
|
|
&& (collision_point->X <= box.MaxEdge.X)
|
|
|
|
&& (collision_point->Y >= box.MinEdge.Y)
|
|
|
|
&& (collision_point->Y <= box.MaxEdge.Y)) {
|
|
|
|
collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-30 16:53:14 +01:00
|
|
|
|
2024-03-12 14:13:24 +01:00
|
|
|
bool boxLineCollision(const aabb3f &box, const v3f rotation,
|
|
|
|
const v3f start, const v3f dir,
|
2022-10-30 16:53:14 +01:00
|
|
|
v3f *collision_point, v3f *collision_normal, v3f *raw_collision_normal)
|
|
|
|
{
|
|
|
|
// Inversely transform the ray rather than rotating the box faces;
|
|
|
|
// this allows us to continue using a simple ray - AABB intersection
|
|
|
|
core::quaternion rot(rotation * core::DEGTORAD);
|
|
|
|
rot.makeInverse();
|
|
|
|
|
|
|
|
bool collision = boxLineCollision(box, rot * start, rot * dir, collision_point, collision_normal);
|
2024-03-12 14:13:24 +01:00
|
|
|
if (!collision)
|
|
|
|
return collision;
|
2022-10-30 16:53:14 +01:00
|
|
|
|
|
|
|
// Transform the results back
|
|
|
|
rot.makeInverse();
|
|
|
|
*collision_point = rot * *collision_point;
|
|
|
|
*raw_collision_normal = *collision_normal;
|
|
|
|
*collision_normal = rot * *collision_normal;
|
|
|
|
return collision;
|
|
|
|
}
|