diff options
Diffstat (limited to 'core/math/aabb.h')
-rw-r--r-- | core/math/aabb.h | 64 |
1 files changed, 47 insertions, 17 deletions
diff --git a/core/math/aabb.h b/core/math/aabb.h index 02ce2501a0..e88ba33531 100644 --- a/core/math/aabb.h +++ b/core/math/aabb.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -36,13 +36,13 @@ #include "core/math/vector3.h" /** - * AABB / AABB (Axis Aligned Bounding Box) - * This is implemented by a point (position) and the box size + * AABB (Axis Aligned Bounding Box) + * This is implemented by a point (position) and the box size. */ + class Variant; -class AABB { -public: +struct _NO_DISCARD_ AABB { Vector3 position; Vector3 size; @@ -119,7 +119,7 @@ public: } _FORCE_INLINE_ Vector3 get_center() const { - return position + (size * 0.5); + return position + (size * 0.5f); } operator String() const; @@ -132,6 +132,11 @@ public: }; inline bool AABB::intersects(const AABB &p_aabb) const { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) { + ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); + } +#endif if (position.x >= (p_aabb.position.x + p_aabb.size.x)) { return false; } @@ -155,6 +160,11 @@ inline bool AABB::intersects(const AABB &p_aabb) const { } inline bool AABB::intersects_inclusive(const AABB &p_aabb) const { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) { + ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); + } +#endif if (position.x > (p_aabb.position.x + p_aabb.size.x)) { return false; } @@ -178,6 +188,11 @@ inline bool AABB::intersects_inclusive(const AABB &p_aabb) const { } inline bool AABB::encloses(const AABB &p_aabb) const { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) { + ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); + } +#endif Vector3 src_min = position; Vector3 src_max = position + size; Vector3 dst_min = p_aabb.position; @@ -193,7 +208,7 @@ inline bool AABB::encloses(const AABB &p_aabb) const { } Vector3 AABB::get_support(const Vector3 &p_normal) const { - Vector3 half_extents = size * 0.5; + Vector3 half_extents = size * 0.5f; Vector3 ofs = position + half_extents; return Vector3( @@ -227,7 +242,7 @@ Vector3 AABB::get_endpoint(int p_point) const { } bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const { - Vector3 half_extents = size * 0.5; + Vector3 half_extents = size * 0.5f; Vector3 ofs = position + half_extents; for (int i = 0; i < p_plane_count; i++) { @@ -269,7 +284,7 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con } bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const { - Vector3 half_extents = size * 0.5; + Vector3 half_extents = size * 0.5f; Vector3 ofs = position + half_extents; for (int i = 0; i < p_plane_count; i++) { @@ -288,6 +303,11 @@ bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const { } bool AABB::has_point(const Vector3 &p_point) const { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) { + ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); + } +#endif if (p_point.x < position.x) { return false; } @@ -311,6 +331,11 @@ bool AABB::has_point(const Vector3 &p_point) const { } inline void AABB::expand_to(const Vector3 &p_vector) { +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) { + ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); + } +#endif Vector3 begin = position; Vector3 end = position + size; @@ -339,7 +364,7 @@ inline void AABB::expand_to(const Vector3 &p_vector) { } void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const { - Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5); + Vector3 half_extents(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f); Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z); real_t length = p_plane.normal.abs().dot(half_extents); @@ -377,9 +402,14 @@ inline real_t AABB::get_shortest_axis_size() const { } bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const { - real_t divx = 1.0 / p_dir.x; - real_t divy = 1.0 / p_dir.y; - real_t divz = 1.0 / p_dir.z; +#ifdef MATH_CHECKS + if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) { + ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); + } +#endif + real_t divx = 1.0f / p_dir.x; + real_t divy = 1.0f / p_dir.y; + real_t divz = 1.0f / p_dir.z; Vector3 upbound = position + size; real_t tmin, tmax, tymin, tymax, tzmin, tzmax; @@ -429,9 +459,9 @@ void AABB::grow_by(real_t p_amount) { position.x -= p_amount; position.y -= p_amount; position.z -= p_amount; - size.x += 2.0 * p_amount; - size.y += 2.0 * p_amount; - size.z += 2.0 * p_amount; + size.x += 2.0f * p_amount; + size.y += 2.0f * p_amount; + size.z += 2.0f * p_amount; } void AABB::quantize(real_t p_unit) { |