-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundingSphere.cpp
67 lines (56 loc) · 1.29 KB
/
BoundingSphere.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "BoundingSphere.h"
namespace Math
{
ContainmentType BoundingSphere::Contains(XMFLOAT3 point)
{
float sqRadius = Radius * Radius;
float sqDistance;
sqDistance = XMVectorGetX(XMVector3LengthSq(XMLoadFloat3(&Center) - XMLoadFloat3(&point)));
if(sqDistance > sqRadius)
{
return ContainmentType::DISJOINT;
}
else if(sqDistance < sqRadius)
{
return ContainmentType::CONTAINS;
}
return ContainmentType::INTERSECTS;
}
ContainmentType BoundingSphere::Contains(Frustum frustum)
{
// Check if all corners are in sphere.
bool inside = true;
XMFLOAT3* corners = frustum.GetCorners();
for(int i = 0; i < 6; i++)
{
if(Contains(corners[i]) == ContainmentType::DISJOINT)
{
inside = false;
break;
}
}
if(inside)
{
return ContainmentType::CONTAINS;
}
// Check if the distance from sphere center to frustrum face is less than radius.
double dmin = 0;
if(dmin <= Radius * Radius)
{
return ContainmentType::INTERSECTS;
}
return ContainmentType::DISJOINT;
}
bool BoundingSphere::Intersects(XMFLOAT4 plane)
{
PlaneIntersectionType result;
XMVECTOR P = XMLoadFloat4(&plane);
XMVECTOR V = XMLoadFloat3(&Center);
float distance = XMVectorGetX(XMPlaneDotCoord(P, V));
if(distance > Radius)
{
return false;
}
return true;
}
}