2017-11-23 11:50:05 +00:00
|
|
|
#include "AABB.hpp"
|
2017-03-06 07:49:24 +00:00
|
|
|
#include "Vector3.hpp"
|
|
|
|
#include "Plane.hpp"
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace godot {
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::intersects(const AABB& p_aabb) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( position.x >= (p_aabb.position.x + p_aabb.size.x) )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( (position.x+size.x) <= p_aabb.position.x )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( position.y >= (p_aabb.position.y + p_aabb.size.y) )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( (position.y+size.y) <= p_aabb.position.y )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( position.z >= (p_aabb.position.z + p_aabb.size.z) )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( (position.z+size.z) <= p_aabb.position.z )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::intersects_inclusive(const AABB& p_aabb) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( position.x > (p_aabb.position.x + p_aabb.size.x) )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( (position.x+size.x) < p_aabb.position.x )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( position.y > (p_aabb.position.y + p_aabb.size.y) )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( (position.y+size.y) < p_aabb.position.y )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( position.z > (p_aabb.position.z + p_aabb.size.z) )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if ( (position.z+size.z) < p_aabb.position.z )
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::encloses(const AABB & p_aabb) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 src_min=position;
|
|
|
|
Vector3 src_max=position+size;
|
|
|
|
Vector3 dst_min=p_aabb.position;
|
|
|
|
Vector3 dst_max=p_aabb.position+p_aabb.size;
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
(src_min.x <= dst_min.x) &&
|
|
|
|
(src_max.x > dst_max.x) &&
|
|
|
|
(src_min.y <= dst_min.y) &&
|
|
|
|
(src_max.y > dst_max.y) &&
|
|
|
|
(src_min.z <= dst_min.z) &&
|
|
|
|
(src_max.z > dst_max.z) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
Vector3 AABB::get_support(const Vector3& p_normal) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 half_extents = size * 0.5;
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 ofs = position + half_extents;
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
return Vector3(
|
|
|
|
(p_normal.x>0) ? -half_extents.x : half_extents.x,
|
|
|
|
(p_normal.y>0) ? -half_extents.y : half_extents.y,
|
|
|
|
(p_normal.z>0) ? -half_extents.z : half_extents.z
|
|
|
|
)+ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
Vector3 AABB::get_endpoint(int p_point) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
switch(p_point) {
|
2018-01-17 00:45:48 +00:00
|
|
|
case 0: return Vector3( position.x , position.y , position.z );
|
|
|
|
case 1: return Vector3( position.x , position.y , position.z+size.z );
|
|
|
|
case 2: return Vector3( position.x , position.y+size.y , position.z );
|
|
|
|
case 3: return Vector3( position.x , position.y+size.y , position.z+size.z );
|
|
|
|
case 4: return Vector3( position.x+size.x , position.y , position.z );
|
|
|
|
case 5: return Vector3( position.x+size.x , position.y , position.z+size.z );
|
|
|
|
case 6: return Vector3( position.x+size.x , position.y+size.y , position.z );
|
|
|
|
case 7: return Vector3( position.x+size.x , position.y+size.y , position.z+size.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ERR_FAIL_V(Vector3());
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 half_extents = size * 0.5;
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 ofs = position + half_extents;
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
for(int i=0;i<p_plane_count;i++) {
|
|
|
|
const Plane &p=p_planes[i];
|
|
|
|
Vector3 point(
|
|
|
|
(p.normal.x>0) ? -half_extents.x : half_extents.x,
|
|
|
|
(p.normal.y>0) ? -half_extents.y : half_extents.y,
|
|
|
|
(p.normal.z>0) ? -half_extents.z : half_extents.z
|
|
|
|
);
|
|
|
|
point+=ofs;
|
|
|
|
if (p.is_point_over(point))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::has_point(const Vector3& p_point) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
if (p_point.x<position.x)
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if (p_point.y<position.y)
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if (p_point.z<position.z)
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if (p_point.x>position.x+size.x)
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if (p_point.y>position.y+size.y)
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
2018-01-17 00:45:48 +00:00
|
|
|
if (p_point.z>position.z+size.z)
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
void AABB::expand_to(const Vector3& p_vector) {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 begin=position;
|
|
|
|
Vector3 end=position+size;
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
if (p_vector.x<begin.x)
|
|
|
|
begin.x=p_vector.x;
|
|
|
|
if (p_vector.y<begin.y)
|
|
|
|
begin.y=p_vector.y;
|
|
|
|
if (p_vector.z<begin.z)
|
|
|
|
begin.z=p_vector.z;
|
|
|
|
|
|
|
|
if (p_vector.x>end.x)
|
|
|
|
end.x=p_vector.x;
|
|
|
|
if (p_vector.y>end.y)
|
|
|
|
end.y=p_vector.y;
|
|
|
|
if (p_vector.z>end.z)
|
|
|
|
end.z=p_vector.z;
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
position=begin;
|
2017-03-06 02:30:46 +00:00
|
|
|
size=end-begin;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
void AABB::project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 );
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 center( position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
real_t length = p_plane.normal.abs().dot(half_extents);
|
|
|
|
real_t distance = p_plane.distance_to( center );
|
|
|
|
r_min = distance - length;
|
|
|
|
r_max = distance + length;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
real_t AABB::get_longest_axis_size() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
real_t max_size=size.x;
|
|
|
|
|
|
|
|
if (size.y > max_size ) {
|
|
|
|
max_size=size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size.z > max_size ) {
|
|
|
|
max_size=size.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_size;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
real_t AABB::get_shortest_axis_size() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
real_t max_size=size.x;
|
|
|
|
|
|
|
|
if (size.y < max_size ) {
|
|
|
|
max_size=size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size.z < max_size ) {
|
|
|
|
max_size=size.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_size;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::smits_intersect_ray(const Vector3 &from,const Vector3& dir, real_t t0, real_t t1) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
real_t divx=1.0/dir.x;
|
|
|
|
real_t divy=1.0/dir.y;
|
|
|
|
real_t divz=1.0/dir.z;
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 upbound=position+size;
|
2017-03-06 02:30:46 +00:00
|
|
|
real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
|
|
|
|
if (dir.x >= 0) {
|
2018-01-17 00:45:48 +00:00
|
|
|
tmin = (position.x - from.x) * divx;
|
2017-03-06 02:30:46 +00:00
|
|
|
tmax = (upbound.x - from.x) * divx;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmin = (upbound.x - from.x) * divx;
|
2018-01-17 00:45:48 +00:00
|
|
|
tmax = (position.x - from.x) * divx;
|
2017-03-06 02:30:46 +00:00
|
|
|
}
|
|
|
|
if (dir.y >= 0) {
|
2018-01-17 00:45:48 +00:00
|
|
|
tymin = (position.y - from.y) * divy;
|
2017-03-06 02:30:46 +00:00
|
|
|
tymax = (upbound.y - from.y) * divy;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tymin = (upbound.y - from.y) * divy;
|
2018-01-17 00:45:48 +00:00
|
|
|
tymax = (position.y - from.y) * divy;
|
2017-03-06 02:30:46 +00:00
|
|
|
}
|
|
|
|
if ( (tmin > tymax) || (tymin > tmax) )
|
|
|
|
return false;
|
|
|
|
if (tymin > tmin)
|
|
|
|
tmin = tymin;
|
|
|
|
if (tymax < tmax)
|
|
|
|
tmax = tymax;
|
|
|
|
if (dir.z >= 0) {
|
2018-01-17 00:45:48 +00:00
|
|
|
tzmin = (position.z - from.z) * divz;
|
2017-03-06 02:30:46 +00:00
|
|
|
tzmax = (upbound.z - from.z) * divz;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tzmin = (upbound.z - from.z) * divz;
|
2018-01-17 00:45:48 +00:00
|
|
|
tzmax = (position.z - from.z) * divz;
|
2017-03-06 02:30:46 +00:00
|
|
|
}
|
|
|
|
if ( (tmin > tzmax) || (tzmin > tmax) )
|
|
|
|
return false;
|
|
|
|
if (tzmin > tmin)
|
|
|
|
tmin = tzmin;
|
|
|
|
if (tzmax < tmax)
|
|
|
|
tmax = tzmax;
|
|
|
|
return ( (tmin < t1) && (tmax > t0) );
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
void AABB::grow_by(real_t p_amount) {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
position.x-=p_amount;
|
|
|
|
position.y-=p_amount;
|
|
|
|
position.z-=p_amount;
|
2017-03-06 02:30:46 +00:00
|
|
|
size.x+=2.0*p_amount;
|
|
|
|
size.y+=2.0*p_amount;
|
|
|
|
size.z+=2.0*p_amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
real_t AABB::get_area() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
return size.x*size.y*size.z;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::operator==(const AABB& p_rval) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
return ((position==p_rval.position) && (size==p_rval.size));
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
}
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::operator!=(const AABB& p_rval) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
return ((position!=p_rval.position) || (size!=p_rval.size));
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
void AABB::merge_with(const AABB& p_aabb) {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 beg_1,beg_2;
|
|
|
|
Vector3 end_1,end_2;
|
|
|
|
Vector3 min,max;
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
beg_1=position;
|
|
|
|
beg_2=p_aabb.position;
|
2017-03-06 02:30:46 +00:00
|
|
|
end_1=Vector3(size.x,size.y,size.z)+beg_1;
|
|
|
|
end_2=Vector3(p_aabb.size.x,p_aabb.size.y,p_aabb.size.z)+beg_2;
|
|
|
|
|
|
|
|
min.x=(beg_1.x<beg_2.x)?beg_1.x:beg_2.x;
|
|
|
|
min.y=(beg_1.y<beg_2.y)?beg_1.y:beg_2.y;
|
|
|
|
min.z=(beg_1.z<beg_2.z)?beg_1.z:beg_2.z;
|
|
|
|
|
|
|
|
max.x=(end_1.x>end_2.x)?end_1.x:end_2.x;
|
|
|
|
max.y=(end_1.y>end_2.y)?end_1.y:end_2.y;
|
|
|
|
max.z=(end_1.z>end_2.z)?end_1.z:end_2.z;
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
position=min;
|
2017-03-06 02:30:46 +00:00
|
|
|
size=max-min;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
AABB AABB::intersection(const AABB& p_aabb) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 src_min=position;
|
|
|
|
Vector3 src_max=position+size;
|
|
|
|
Vector3 dst_min=p_aabb.position;
|
|
|
|
Vector3 dst_max=p_aabb.position+p_aabb.size;
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 min,max;
|
|
|
|
|
|
|
|
if (src_min.x > dst_max.x || src_max.x < dst_min.x )
|
2017-11-23 11:50:05 +00:00
|
|
|
return AABB();
|
2017-03-06 02:30:46 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
min.x= ( src_min.x > dst_min.x ) ? src_min.x :dst_min.x;
|
|
|
|
max.x= ( src_max.x < dst_max.x ) ? src_max.x :dst_max.x;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_min.y > dst_max.y || src_max.y < dst_min.y )
|
2017-11-23 11:50:05 +00:00
|
|
|
return AABB();
|
2017-03-06 02:30:46 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
min.y= ( src_min.y > dst_min.y ) ? src_min.y :dst_min.y;
|
|
|
|
max.y= ( src_max.y < dst_max.y ) ? src_max.y :dst_max.y;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_min.z > dst_max.z || src_max.z < dst_min.z )
|
2017-11-23 11:50:05 +00:00
|
|
|
return AABB();
|
2017-03-06 02:30:46 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
min.z= ( src_min.z > dst_min.z ) ? src_min.z :dst_min.z;
|
|
|
|
max.z= ( src_max.z < dst_max.z ) ? src_max.z :dst_max.z;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
return AABB( min, max-min );
|
2017-03-06 02:30:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 c1, c2;
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3 end = position+size;
|
2017-03-06 02:30:46 +00:00
|
|
|
real_t near=-1e20;
|
|
|
|
real_t far=1e20;
|
|
|
|
int axis=0;
|
|
|
|
|
|
|
|
for (int i=0;i<3;i++){
|
|
|
|
if (p_dir[i] == 0){
|
2018-01-17 00:45:48 +00:00
|
|
|
if ((p_from[i] < position[i]) || (p_from[i] > end[i])) {
|
2017-03-06 02:30:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else { // ray not parallel to planes in this direction
|
2018-01-17 00:45:48 +00:00
|
|
|
c1[i] = (position[i] - p_from[i]) / p_dir[i];
|
2017-03-06 02:30:46 +00:00
|
|
|
c2[i] = (end[i] - p_from[i]) / p_dir[i];
|
|
|
|
|
|
|
|
if(c1[i] > c2[i]){
|
|
|
|
std::swap(c1,c2);
|
|
|
|
}
|
|
|
|
if (c1[i] > near){
|
|
|
|
near = c1[i];
|
|
|
|
axis=i;
|
|
|
|
}
|
|
|
|
if (c2[i] < far){
|
|
|
|
far = c2[i];
|
|
|
|
}
|
|
|
|
if( (near > far) || (far < 0) ){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_clip)
|
|
|
|
*r_clip=c1;
|
|
|
|
if (r_normal) {
|
|
|
|
*r_normal=Vector3();
|
|
|
|
(*r_normal)[axis]=p_dir[axis]?-1:1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip,Vector3* r_normal) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
real_t min=0,max=1;
|
|
|
|
int axis=0;
|
|
|
|
real_t sign=0;
|
|
|
|
|
|
|
|
for(int i=0;i<3;i++) {
|
|
|
|
real_t seg_from=p_from[i];
|
|
|
|
real_t seg_to=p_to[i];
|
2018-01-17 00:45:48 +00:00
|
|
|
real_t box_begin=position[i];
|
2017-03-06 02:30:46 +00:00
|
|
|
real_t box_end=box_begin+size[i];
|
|
|
|
real_t cmin,cmax;
|
|
|
|
real_t csign;
|
|
|
|
|
|
|
|
if (seg_from < seg_to) {
|
|
|
|
|
|
|
|
if (seg_from > box_end || seg_to < box_begin)
|
|
|
|
return false;
|
|
|
|
real_t length=seg_to-seg_from;
|
|
|
|
cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
|
|
|
|
cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
|
|
|
|
csign=-1.0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (seg_to > box_end || seg_from < box_begin)
|
|
|
|
return false;
|
|
|
|
real_t length=seg_to-seg_from;
|
|
|
|
cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
|
|
|
|
cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
|
|
|
|
csign=1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmin > min) {
|
|
|
|
min = cmin;
|
|
|
|
axis=i;
|
|
|
|
sign=csign;
|
|
|
|
}
|
|
|
|
if (cmax < max)
|
|
|
|
max = cmax;
|
|
|
|
if (max < min)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vector3 rel=p_to-p_from;
|
|
|
|
|
|
|
|
if (r_normal) {
|
|
|
|
Vector3 normal;
|
|
|
|
normal[axis]=sign;
|
|
|
|
*r_normal=normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_clip)
|
|
|
|
*r_clip=p_from+rel*min;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
bool AABB::intersects_plane(const Plane &p_plane) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 points[8] = {
|
2018-01-17 00:45:48 +00:00
|
|
|
Vector3( position.x , position.y , position.z ),
|
|
|
|
Vector3( position.x , position.y , position.z+size.z ),
|
|
|
|
Vector3( position.x , position.y+size.y , position.z ),
|
|
|
|
Vector3( position.x , position.y+size.y , position.z+size.z ),
|
|
|
|
Vector3( position.x+size.x , position.y , position.z ),
|
|
|
|
Vector3( position.x+size.x , position.y , position.z+size.z ),
|
|
|
|
Vector3( position.x+size.x , position.y+size.y , position.z ),
|
|
|
|
Vector3( position.x+size.x , position.y+size.y , position.z+size.z ),
|
2017-03-06 02:30:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool over=false;
|
|
|
|
bool under=false;
|
|
|
|
|
|
|
|
for (int i=0;i<8;i++) {
|
|
|
|
|
|
|
|
if (p_plane.distance_to(points[i])>0)
|
|
|
|
over=true;
|
|
|
|
else
|
|
|
|
under=true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return under && over;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
Vector3 AABB::get_longest_axis() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 axis(1,0,0);
|
|
|
|
real_t max_size=size.x;
|
|
|
|
|
|
|
|
if (size.y > max_size ) {
|
|
|
|
axis=Vector3(0,1,0);
|
|
|
|
max_size=size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size.z > max_size ) {
|
|
|
|
axis=Vector3(0,0,1);
|
|
|
|
max_size=size.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
2017-11-23 11:50:05 +00:00
|
|
|
int AABB::get_longest_axis_index() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
int axis=0;
|
|
|
|
real_t max_size=size.x;
|
|
|
|
|
|
|
|
if (size.y > max_size ) {
|
|
|
|
axis=1;
|
|
|
|
max_size=size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size.z > max_size ) {
|
|
|
|
axis=2;
|
|
|
|
max_size=size.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
Vector3 AABB::get_shortest_axis() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
Vector3 axis(1,0,0);
|
|
|
|
real_t max_size=size.x;
|
|
|
|
|
|
|
|
if (size.y < max_size ) {
|
|
|
|
axis=Vector3(0,1,0);
|
|
|
|
max_size=size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size.z < max_size ) {
|
|
|
|
axis=Vector3(0,0,1);
|
|
|
|
max_size=size.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
2017-11-23 11:50:05 +00:00
|
|
|
int AABB::get_shortest_axis_index() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
int axis=0;
|
|
|
|
real_t max_size=size.x;
|
|
|
|
|
|
|
|
if (size.y < max_size ) {
|
|
|
|
axis=1;
|
|
|
|
max_size=size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size.z < max_size ) {
|
|
|
|
axis=2;
|
|
|
|
max_size=size.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
AABB AABB::merge(const AABB& p_with) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
AABB aabb=*this;
|
2017-03-06 02:30:46 +00:00
|
|
|
aabb.merge_with(p_with);
|
|
|
|
return aabb;
|
|
|
|
}
|
2017-11-23 11:50:05 +00:00
|
|
|
AABB AABB::expand(const Vector3& p_vector) const {
|
|
|
|
AABB aabb=*this;
|
2017-03-06 02:30:46 +00:00
|
|
|
aabb.expand_to(p_vector);
|
|
|
|
return aabb;
|
|
|
|
|
|
|
|
}
|
2017-11-23 11:50:05 +00:00
|
|
|
AABB AABB::grow(real_t p_by) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
AABB aabb=*this;
|
2017-03-06 02:30:46 +00:00
|
|
|
aabb.grow_by(p_by);
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
void AABB::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
ERR_FAIL_INDEX(p_edge,12);
|
|
|
|
switch(p_edge) {
|
|
|
|
|
|
|
|
case 0:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x+size.x , position.y , position.z );
|
|
|
|
r_to=Vector3( position.x , position.y , position.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
} break;
|
|
|
|
case 1:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x+size.x , position.y , position.z+size.z );
|
|
|
|
r_to=Vector3( position.x+size.x , position.y , position.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
} break;
|
|
|
|
case 2:{
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x , position.y , position.z+size.z );
|
|
|
|
r_to=Vector3( position.x+size.x , position.y , position.z+size.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case 3:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x , position.y , position.z );
|
|
|
|
r_to=Vector3( position.x , position.y , position.z+size.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case 4:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x , position.y+size.y , position.z );
|
|
|
|
r_to=Vector3( position.x+size.x , position.y+size.y , position.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
} break;
|
|
|
|
case 5:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x+size.x , position.y+size.y , position.z );
|
|
|
|
r_to=Vector3( position.x+size.x , position.y+size.y , position.z+size.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
} break;
|
|
|
|
case 6:{
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x+size.x , position.y+size.y , position.z+size.z );
|
|
|
|
r_to=Vector3( position.x , position.y+size.y , position.z+size.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case 7:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x , position.y+size.y , position.z+size.z );
|
|
|
|
r_to=Vector3( position.x , position.y+size.y , position.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case 8:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x , position.y , position.z+size.z );
|
|
|
|
r_to=Vector3( position.x , position.y+size.y , position.z+size.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case 9:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x , position.y , position.z );
|
|
|
|
r_to=Vector3( position.x , position.y+size.y , position.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case 10:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x+size.x , position.y , position.z );
|
|
|
|
r_to=Vector3( position.x+size.x , position.y+size.y , position.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case 11:{
|
|
|
|
|
2018-01-17 00:45:48 +00:00
|
|
|
r_from=Vector3( position.x+size.x , position.y , position.z+size.z );
|
|
|
|
r_to=Vector3( position.x+size.x , position.y+size.y , position.z+size.z );
|
2017-03-06 02:30:46 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:05 +00:00
|
|
|
AABB::operator String() const {
|
2017-03-06 02:30:46 +00:00
|
|
|
|
2018-01-22 23:24:23 +00:00
|
|
|
return String() + position + " - " + size;
|
2017-03-06 02:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|