summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
Diffstat (limited to 'core/math')
-rw-r--r--core/math/aabb.h28
-rw-r--r--core/math/audio_frame.h2
-rw-r--r--core/math/geometry.cpp43
-rw-r--r--core/math/geometry.h6
-rw-r--r--core/math/octree.h14
-rw-r--r--core/math/transform.cpp4
-rw-r--r--core/math/transform_2d.cpp6
-rw-r--r--core/math/triangle_mesh.cpp8
-rw-r--r--core/math/triangle_mesh.h4
-rw-r--r--core/math/vector2.cpp10
-rw-r--r--core/math/vector2.h21
-rw-r--r--core/math/vector3.h4
12 files changed, 102 insertions, 48 deletions
diff --git a/core/math/aabb.h b/core/math/aabb.h
index eca74e6755..7fdad07c89 100644
--- a/core/math/aabb.h
+++ b/core/math/aabb.h
@@ -76,7 +76,7 @@ public:
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
_FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const;
- _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count) const;
+ _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
_FORCE_INLINE_ bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const;
bool intersects_plane(const Plane &p_plane) const;
@@ -190,7 +190,7 @@ Vector3 AABB::get_endpoint(int p_point) const {
ERR_FAIL_V(Vector3());
}
-bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) 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 ofs = position + half_extents;
@@ -206,6 +206,30 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) con
return false;
}
+ // Make sure all points in the shape aren't fully separated from the AABB on
+ // each axis.
+ int bad_point_counts_positive[3] = { 0 };
+ int bad_point_counts_negative[3] = { 0 };
+
+ for (int k = 0; k < 3; k++) {
+
+ for (int i = 0; i < p_point_count; i++) {
+ if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
+ bad_point_counts_positive[k]++;
+ }
+ if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
+ bad_point_counts_negative[k]++;
+ }
+ }
+
+ if (bad_point_counts_negative[k] == p_point_count) {
+ return false;
+ }
+ if (bad_point_counts_positive[k] == p_point_count) {
+ return false;
+ }
+ }
+
return true;
}
diff --git a/core/math/audio_frame.h b/core/math/audio_frame.h
index e1dbb385e4..4665311059 100644
--- a/core/math/audio_frame.h
+++ b/core/math/audio_frame.h
@@ -104,7 +104,7 @@ struct AudioFrame {
r = ::undenormalise(r);
}
- _FORCE_INLINE_ AudioFrame linear_interpolate(const AudioFrame &p_b, float p_t) const {
+ _FORCE_INLINE_ AudioFrame lerp(const AudioFrame &p_b, float p_t) const {
AudioFrame res = *this;
diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp
index 3e07e9253e..fa96fc4535 100644
--- a/core/math/geometry.cpp
+++ b/core/math/geometry.cpp
@@ -911,7 +911,7 @@ Vector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_l
for (int j = 1; j <= p_lats; j++) {
// FIXME: This is stupid.
- Vector3 angle = normal.linear_interpolate(axis, j / (real_t)p_lats).normalized();
+ Vector3 angle = normal.lerp(axis, j / (real_t)p_lats).normalized();
Vector3 pos = angle * p_radius;
planes.push_back(Plane(pos, angle));
planes.push_back(Plane(pos * axis_neg, angle * axis_neg));
@@ -943,7 +943,7 @@ Vector<Plane> Geometry::build_capsule_planes(real_t p_radius, real_t p_height, i
for (int j = 1; j <= p_lats; j++) {
- Vector3 angle = normal.linear_interpolate(axis, j / (real_t)p_lats).normalized();
+ Vector3 angle = normal.lerp(axis, j / (real_t)p_lats).normalized();
Vector3 pos = axis * p_height * 0.5 + angle * p_radius;
planes.push_back(Plane(pos, angle));
planes.push_back(Plane(pos * axis_neg, angle * axis_neg));
@@ -1178,3 +1178,42 @@ Vector<Vector<Point2>> Geometry::_polypath_offset(const Vector<Point2> &p_polypa
}
return polypaths;
}
+
+Vector<Vector3> Geometry::compute_convex_mesh_points(const Plane *p_planes, int p_plane_count) {
+
+ Vector<Vector3> points;
+
+ // Iterate through every unique combination of any three planes.
+ for (int i = p_plane_count - 1; i >= 0; i--) {
+ for (int j = i - 1; j >= 0; j--) {
+ for (int k = j - 1; k >= 0; k--) {
+
+ // Find the point where these planes all cross over (if they
+ // do at all).
+ Vector3 convex_shape_point;
+ if (p_planes[i].intersect_3(p_planes[j], p_planes[k], &convex_shape_point)) {
+
+ // See if any *other* plane excludes this point because it's
+ // on the wrong side.
+ bool excluded = false;
+ for (int n = 0; n < p_plane_count; n++) {
+ if (n != i && n != j && n != k) {
+ real_t dp = p_planes[n].normal.dot(convex_shape_point);
+ if (dp - p_planes[n].d > CMP_EPSILON) {
+ excluded = true;
+ break;
+ }
+ }
+ }
+
+ // Only add the point if it passed all tests.
+ if (!excluded) {
+ points.push_back(convex_shape_point);
+ }
+ }
+ }
+ }
+ }
+
+ return points;
+}
diff --git a/core/math/geometry.h b/core/math/geometry.h
index e47d18b056..ea063a8a59 100644
--- a/core/math/geometry.h
+++ b/core/math/geometry.h
@@ -117,8 +117,8 @@ public:
if (mub < 0) mub = 0;
if (mua > 1) mua = 1;
if (mub > 1) mub = 1;
- c1 = p1.linear_interpolate(p2, mua);
- c2 = q1.linear_interpolate(q2, mub);
+ c1 = p1.lerp(p2, mua);
+ c2 = q1.lerp(q2, mub);
}
static real_t get_closest_distance_between_segments(const Vector3 &p_from_a, const Vector3 &p_to_a, const Vector3 &p_from_b, const Vector3 &p_to_b) {
@@ -1014,6 +1014,8 @@ public:
static void make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size);
+ static Vector<Vector3> compute_convex_mesh_points(const Plane *p_planes, int p_plane_count);
+
private:
static Vector<Vector<Point2>> _polypaths_do_operation(PolyBooleanOperation p_op, const Vector<Point2> &p_polypath_a, const Vector<Point2> &p_polypath_b, bool is_a_open = false);
static Vector<Vector<Point2>> _polypath_offset(const Vector<Point2> &p_polypath, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type);
diff --git a/core/math/octree.h b/core/math/octree.h
index 5225fbecb4..2060a61b4b 100644
--- a/core/math/octree.h
+++ b/core/math/octree.h
@@ -34,6 +34,7 @@
#include "core/list.h"
#include "core/map.h"
#include "core/math/aabb.h"
+#include "core/math/geometry.h"
#include "core/math/vector3.h"
#include "core/print_string.h"
#include "core/variant.h"
@@ -341,6 +342,8 @@ private:
const Plane *planes;
int plane_count;
+ const Vector3 *points;
+ int point_count;
T **result_array;
int *result_idx;
int result_max;
@@ -1017,8 +1020,7 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p
continue;
e->last_pass = pass;
- if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count)) {
-
+ if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
if (*p_cull->result_idx < p_cull->result_max) {
p_cull->result_array[*p_cull->result_idx] = e->userdata;
(*p_cull->result_idx)++;
@@ -1043,7 +1045,7 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p
continue;
e->last_pass = pass;
- if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count)) {
+ if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
if (*p_cull->result_idx < p_cull->result_max) {
@@ -1059,7 +1061,7 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p
for (int i = 0; i < 8; i++) {
- if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count)) {
+ if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
_cull_convex(p_octant->children[i], p_cull);
}
}
@@ -1291,11 +1293,15 @@ int Octree<T, use_pairs, AL>::cull_convex(const Vector<Plane> &p_convex, T **p_r
if (!root)
return 0;
+ Vector<Vector3> convex_points = Geometry::compute_convex_mesh_points(&p_convex[0], p_convex.size());
+
int result_count = 0;
pass++;
_CullConvexData cdata;
cdata.planes = &p_convex[0];
cdata.plane_count = p_convex.size();
+ cdata.points = &convex_points[0];
+ cdata.point_count = convex_points.size();
cdata.result_array = p_result_array;
cdata.result_max = p_result_max;
cdata.result_idx = &result_count;
diff --git a/core/math/transform.cpp b/core/math/transform.cpp
index 9dad3262d2..82e4005d3e 100644
--- a/core/math/transform.cpp
+++ b/core/math/transform.cpp
@@ -129,8 +129,8 @@ Transform Transform::interpolate_with(const Transform &p_transform, real_t p_c)
Vector3 dst_loc = p_transform.origin;
Transform interp;
- interp.basis.set_quat_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.linear_interpolate(dst_scale, p_c));
- interp.origin = src_loc.linear_interpolate(dst_loc, p_c);
+ interp.basis.set_quat_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c));
+ interp.origin = src_loc.lerp(dst_loc, p_c);
return interp;
}
diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp
index f28b664e46..97a9216a5a 100644
--- a/core/math/transform_2d.cpp
+++ b/core/math/transform_2d.cpp
@@ -267,7 +267,7 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t
Vector2 v;
if (dot > 0.9995) {
- v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
+ v = v1.lerp(v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
} else {
real_t angle = p_c * Math::acos(dot);
Vector2 v3 = (v2 - v1 * dot).normalized();
@@ -275,8 +275,8 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t
}
//construct matrix
- Transform2D res(Math::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c));
- res.scale_basis(Vector2::linear_interpolate(s1, s2, p_c));
+ Transform2D res(Math::atan2(v.y, v.x), p1.lerp(p2, p_c));
+ res.scale_basis(s1.lerp(s2, p_c));
return res;
}
diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp
index 01d38cf24e..5c66721b9d 100644
--- a/core/math/triangle_mesh.cpp
+++ b/core/math/triangle_mesh.cpp
@@ -502,7 +502,7 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V
return inters;
}
-bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_count) const {
+bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
//p_fully_inside = true;
@@ -536,7 +536,7 @@ bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_cou
switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
- bool valid = b.aabb.intersects_convex_shape(p_planes, p_plane_count);
+ bool valid = b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
if (!valid) {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
@@ -617,7 +617,7 @@ bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_cou
return false;
}
-bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, Vector3 p_scale) const {
+bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale) const {
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
@@ -651,7 +651,7 @@ bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count,
switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
- bool intersects = scale.xform(b.aabb).intersects_convex_shape(p_planes, p_plane_count);
+ bool intersects = scale.xform(b.aabb).intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
if (!intersects) return false;
bool inside = scale.xform(b.aabb).inside_convex_shape(p_planes, p_plane_count);
diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h
index fdbfb90465..64704477cc 100644
--- a/core/math/triangle_mesh.h
+++ b/core/math/triangle_mesh.h
@@ -90,8 +90,8 @@ public:
bool is_valid() const;
bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const;
bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const;
- bool intersect_convex_shape(const Plane *p_planes, int p_plane_count) const;
- bool inside_convex_shape(const Plane *p_planes, int p_plane_count, Vector3 p_scale = Vector3(1, 1, 1)) const;
+ bool intersect_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
+ bool inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale = Vector3(1, 1, 1)) const;
Vector3 get_area_normal(const AABB &p_aabb) const;
Vector<Face3> get_faces() const;
diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp
index f4259e388b..f46badd19e 100644
--- a/core/math/vector2.cpp
+++ b/core/math/vector2.cpp
@@ -119,11 +119,11 @@ Vector2 Vector2::round() const {
}
Vector2 Vector2::rotated(real_t p_by) const {
-
- Vector2 v;
- v.set_rotation(angle() + p_by);
- v *= length();
- return v;
+ real_t sine = Math::sin(p_by);
+ real_t cosi = Math::cos(p_by);
+ return Vector2(
+ x * cosi - y * sine,
+ x * sine + y * cosi);
}
Vector2 Vector2::posmod(const real_t p_mod) const {
diff --git a/core/math/vector2.h b/core/math/vector2.h
index ba5558102f..c0057f2543 100644
--- a/core/math/vector2.h
+++ b/core/math/vector2.h
@@ -82,8 +82,7 @@ struct Vector2 {
Vector2 clamped(real_t p_len) const;
- _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t);
- _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const;
+ _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_b, real_t p_t) const;
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_b, real_t p_t) const;
Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
@@ -123,12 +122,6 @@ struct Vector2 {
real_t angle() const;
- void set_rotation(real_t p_radians) {
-
- x = Math::cos(p_radians);
- y = Math::sin(p_radians);
- }
-
_FORCE_INLINE_ Vector2 abs() const {
return Vector2(Math::abs(x), Math::abs(y));
@@ -230,7 +223,7 @@ _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
return x != p_vec2.x || y != p_vec2.y;
}
-Vector2 Vector2::linear_interpolate(const Vector2 &p_b, real_t p_t) const {
+Vector2 Vector2::lerp(const Vector2 &p_b, real_t p_t) const {
Vector2 res = *this;
@@ -254,16 +247,6 @@ Vector2 Vector2::direction_to(const Vector2 &p_b) const {
return ret;
}
-Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
-
- Vector2 res = p_a;
-
- res.x += (p_t * (p_b.x - p_a.x));
- res.y += (p_t * (p_b.y - p_a.y));
-
- return res;
-}
-
typedef Vector2 Size2;
typedef Vector2 Point2;
diff --git a/core/math/vector3.h b/core/math/vector3.h
index 3bf8644af9..a5e9d09208 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -89,7 +89,7 @@ struct Vector3 {
/* Static Methods between 2 vector3s */
- _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const;
+ _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_b, real_t p_t) const;
_FORCE_INLINE_ Vector3 slerp(const Vector3 &p_b, real_t p_t) const;
Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const;
Vector3 cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const;
@@ -206,7 +206,7 @@ Vector3 Vector3::round() const {
return Vector3(Math::round(x), Math::round(y), Math::round(z));
}
-Vector3 Vector3::linear_interpolate(const Vector3 &p_b, real_t p_t) const {
+Vector3 Vector3::lerp(const Vector3 &p_b, real_t p_t) const {
return Vector3(
x + (p_t * (p_b.x - x)),