Richard Boegli's CnC_Generals_Zero_Hour Fork
WIP
This is documentation of Richard Boegil's Zero Hour Fork
Loading...
Searching...
No Matches
colmathsphere.cpp
Go to the documentation of this file.
1
/*
2
** Command & Conquer Generals Zero Hour(tm)
3
** Copyright 2025 Electronic Arts Inc.
4
**
5
** This program is free software: you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation, either version 3 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
/***********************************************************************************************
20
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21
***********************************************************************************************
22
* *
23
* Project Name : WWMath *
24
* *
25
* $Archive:: /Commando/Code/wwmath/colmathsphere.cpp $*
26
* *
27
* Author:: Greg Hjelstrom *
28
* *
29
* $Modtime:: 4/25/01 2:05p $*
30
* *
31
* $Revision:: 6 $*
32
* *
33
*---------------------------------------------------------------------------------------------*
34
* Functions: *
35
* CollisionMath::Intersection_Test -- Sphere - AAbox intersection *
36
* CollisionMath::Intersection_Test -- Sphere - OBBox intersection *
37
* CollisionMath::Overlap_Test -- Sphere - Point overlap test *
38
* CollisionMath::Overlap_Test -- sphere line overlap test *
39
* CollisionMath::Overlap_Test -- sphere triangle overlap test *
40
* CollisionMath::Overlap_Test -- Sphere - Sphere overlap test *
41
* CollisionMath::Overlap_Test -- Sphere - AABox overlap test *
42
* CollisionMath::Overlap_Test -- Sphere - OBBox overlap test *
43
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
44
45
46
#include "
colmath.h
"
47
#include "
aaplane.h
"
48
#include "
plane.h
"
49
#include "
lineseg.h
"
50
#include "
tri.h
"
51
#include "
sphere.h
"
52
#include "
aabox.h
"
53
#include "
obbox.h
"
54
#include "
wwdebug.h
"
55
56
57
// Sphere Intersection fucntions. Does the sphere intersect the passed in object
58
/***********************************************************************************************
59
* CollisionMath::Intersection_Test -- Sphere - AAbox intersection *
60
* *
61
* INPUT: *
62
* *
63
* OUTPUT: *
64
* *
65
* WARNINGS: *
66
* *
67
* HISTORY: *
68
* 4/25/2001 gth : Created. *
69
*=============================================================================================*/
70
bool
CollisionMath::Intersection_Test
(
const
SphereClass
& sphere,
const
AABoxClass
& box)
71
{
72
/*
73
** Simple but slightly inaccurate test, expand the box by the sphere's radius, then
74
** test whether the sphere is contained in that new box. This is actually testing
75
** against a cube which encloses the sphere...
76
*/
77
Vector3
dc = box.
Center
- sphere.
Center
;
78
if
(
WWMath::Fabs
(dc.
X
) < box.
Extent
.
X
+ sphere.
Radius
)
return
false
;
79
if
(
WWMath::Fabs
(dc.
Y
) < box.
Extent
.
Y
+ sphere.
Radius
)
return
false
;
80
if
(
WWMath::Fabs
(dc.
Z
) < box.
Extent
.
Z
+ sphere.
Radius
)
return
false
;
81
return
true
;
82
}
83
84
85
/***********************************************************************************************
86
* CollisionMath::Intersection_Test -- Sphere - OBBox intersection *
87
* *
88
* INPUT: *
89
* *
90
* OUTPUT: *
91
* *
92
* WARNINGS: *
93
* *
94
* HISTORY: *
95
* 4/25/2001 gth : Created. *
96
*=============================================================================================*/
97
bool
CollisionMath::Intersection_Test
(
const
SphereClass
& sphere,
const
OBBoxClass
& box)
98
{
99
/*
100
** Compute the sphere's position in the box's coordinate system
101
*/
102
Matrix3D
tm(box.
Basis
,box.
Center
);
103
Vector3
box_rel_center;
104
Matrix3D::Inverse_Transform_Vector
(tm,sphere.
Center
,&box_rel_center);
105
106
if
(box.
Extent
.
X
<
WWMath::Fabs
(box_rel_center.
X
))
return
false
;
107
if
(box.
Extent
.
Y
<
WWMath::Fabs
(box_rel_center.
Y
))
return
false
;
108
if
(box.
Extent
.
Z
<
WWMath::Fabs
(box_rel_center.
Z
))
return
false
;
109
110
return
true
;
111
}
112
113
// Sphere Overlap functions. Where is operand B with respect to the sphere
114
/***********************************************************************************************
115
* CollisionMath::Overlap_Test -- Sphere - Point overlap test *
116
* *
117
* INPUT: *
118
* *
119
* OUTPUT: *
120
* *
121
* WARNINGS: *
122
* *
123
* HISTORY: *
124
* 4/25/2001 gth : Created. *
125
*=============================================================================================*/
126
CollisionMath::OverlapType
127
CollisionMath::Overlap_Test
(
const
SphereClass
& sphere,
const
Vector3
& point)
128
{
129
float
r2 = (point - sphere.
Center
).Length2();
130
if
(r2 < sphere.
Radius
* sphere.
Radius
- COINCIDENCE_EPSILON) {
131
return
NEG
;
132
}
133
if
(r2 > sphere.
Radius
* sphere.
Radius
+ COINCIDENCE_EPSILON) {
134
return
POS
;
135
}
136
return
ON
;
137
}
138
139
140
/***********************************************************************************************
141
* CollisionMath::Overlap_Test -- sphere line overlap test *
142
* *
143
* INPUT: *
144
* *
145
* OUTPUT: *
146
* *
147
* WARNINGS: *
148
* *
149
* HISTORY: *
150
* 4/25/2001 gth : Created. *
151
*=============================================================================================*/
152
CollisionMath::OverlapType
153
CollisionMath::Overlap_Test
(
const
SphereClass
&
/*sphere*/
,
const
LineSegClass
&
/*line*/
)
154
{
155
WWASSERT
(0);
//TODO
156
return
POS
;
157
}
158
159
160
/***********************************************************************************************
161
* CollisionMath::Overlap_Test -- sphere triangle overlap test *
162
* *
163
* INPUT: *
164
* *
165
* OUTPUT: *
166
* *
167
* WARNINGS: *
168
* *
169
* HISTORY: *
170
* 4/25/2001 gth : Created. *
171
*=============================================================================================*/
172
CollisionMath::OverlapType
173
CollisionMath::Overlap_Test
(
const
SphereClass
&
/*sphere*/
,
const
TriClass
&
/*tri*/
)
174
{
175
WWASSERT
(0);
//TODO
176
return
POS
;
177
}
178
179
180
/***********************************************************************************************
181
* CollisionMath::Overlap_Test -- Sphere - Sphere overlap test *
182
* *
183
* INPUT: *
184
* *
185
* OUTPUT: *
186
* *
187
* WARNINGS: *
188
* *
189
* HISTORY: *
190
* 4/25/2001 gth : Created. *
191
*=============================================================================================*/
192
CollisionMath::OverlapType
193
CollisionMath::Overlap_Test
(
const
SphereClass
& sphere,
const
SphereClass
& sphere2)
194
{
195
CollisionMath::OverlapType
retval =
OUTSIDE
;
196
197
float
radius = sphere.
Radius
+ sphere2.
Radius
;
198
float
dist2 = (sphere2.
Center
- sphere.
Center
).Length2();
199
200
if
(dist2 == 0 && sphere.
Radius
== sphere2.
Radius
) {
201
retval =
OVERLAPPED
;
202
}
else
if
(dist2 <= radius * radius - COINCIDENCE_EPSILON) {
203
retval =
INSIDE
;
204
}
205
206
return
retval;
207
}
208
209
210
/***********************************************************************************************
211
* CollisionMath::Overlap_Test -- Sphere - AABox overlap test *
212
* *
213
* INPUT: *
214
* *
215
* OUTPUT: *
216
* *
217
* WARNINGS: *
218
* *
219
* HISTORY: *
220
* 4/25/2001 gth : Created. *
221
*=============================================================================================*/
222
CollisionMath::OverlapType
223
CollisionMath::Overlap_Test
(
const
SphereClass
& sphere,
const
AABoxClass
& aabox)
224
{
225
// TODO: overlap function that detects containment?
226
return
(
Intersection_Test
(sphere,aabox) ?
BOTH
:
POS
);
227
}
228
229
230
/***********************************************************************************************
231
* CollisionMath::Overlap_Test -- Sphere - OBBox overlap test *
232
* *
233
* INPUT: *
234
* *
235
* OUTPUT: *
236
* *
237
* WARNINGS: *
238
* *
239
* HISTORY: *
240
* 4/25/2001 gth : Created. *
241
*=============================================================================================*/
242
CollisionMath::OverlapType
243
CollisionMath::Overlap_Test
(
const
SphereClass
& sphere,
const
OBBoxClass
& obbox)
244
{
245
// TODO: overlap function that detects containment?
246
return
(
Intersection_Test
(sphere,obbox) ?
BOTH
:
POS
);
247
}
248
249
WWASSERT
#define WWASSERT
Definition
aabtreebuilder.cpp:68
aaplane.h
plane.h
aabox.h
AABoxClass
Definition
aabox.h:83
AABoxClass::Center
Vector3 Center
Definition
aabox.h:123
AABoxClass::Extent
Vector3 Extent
Definition
aabox.h:124
CollisionMath::Overlap_Test
static OverlapType Overlap_Test(const AAPlaneClass &plane, const Vector3 &point)
Definition
colmathplane.cpp:55
CollisionMath::OverlapType
OverlapType
Definition
colmath.h:106
CollisionMath::POS
@ POS
Definition
colmath.h:107
CollisionMath::OUTSIDE
@ OUTSIDE
Definition
colmath.h:111
CollisionMath::BOTH
@ BOTH
Definition
colmath.h:110
CollisionMath::NEG
@ NEG
Definition
colmath.h:108
CollisionMath::ON
@ ON
Definition
colmath.h:109
CollisionMath::INSIDE
@ INSIDE
Definition
colmath.h:112
CollisionMath::OVERLAPPED
@ OVERLAPPED
Definition
colmath.h:113
CollisionMath::Intersection_Test
static bool Intersection_Test(const AABoxClass &box, const TriClass &tri)
Definition
colmathaabtri.cpp:1041
LineSegClass
Definition
lineseg.h:58
Matrix3D
Definition
matrix3d.h:133
Matrix3D::Inverse_Transform_Vector
static WWINLINE void Inverse_Transform_Vector(const Matrix3D &tm, const Vector3 &in, Vector3 *out)
Definition
matrix3d.h:1778
OBBoxClass
Definition
obbox.h:77
OBBoxClass::Extent
Vector3 Extent
Definition
obbox.h:114
OBBoxClass::Basis
Matrix3x3 Basis
Definition
obbox.h:112
OBBoxClass::Center
Vector3 Center
Definition
obbox.h:113
SphereClass
Definition
sphere.h:72
SphereClass::Radius
float Radius
Definition
sphere.h:91
SphereClass::Center
Vector3 Center
Definition
sphere.h:90
TriClass
Definition
tri.h:61
Vector3
Definition
vector3.h:85
Vector3::X
float X
Definition
vector3.h:90
Vector3::Z
float Z
Definition
vector3.h:92
Vector3::Y
float Y
Definition
vector3.h:91
WWMath::Fabs
static WWINLINE float Fabs(float val)
Definition
wwmath.h:113
colmath.h
lineseg.h
obbox.h
sphere.h
tri.h
wwdebug.h
Code
Libraries
Source
WWVegas
WWMath
colmathsphere.cpp
Generated by
1.13.2