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 *=============================================================================================*/
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 *=============================================================================================*/
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 *=============================================================================================*/
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 *=============================================================================================*/
153CollisionMath::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 *=============================================================================================*/
173CollisionMath::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 *=============================================================================================*/
194{
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 *=============================================================================================*/
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 *=============================================================================================*/
244{
245 // TODO: overlap function that detects containment?
246 return ( Intersection_Test(sphere,obbox) ? BOTH : POS );
247}
248
249
#define WWASSERT
Vector3 Center
Definition aabox.h:123
Vector3 Extent
Definition aabox.h:124
static OverlapType Overlap_Test(const AAPlaneClass &plane, const Vector3 &point)
static bool Intersection_Test(const AABoxClass &box, const TriClass &tri)
static WWINLINE void Inverse_Transform_Vector(const Matrix3D &tm, const Vector3 &in, Vector3 *out)
Definition matrix3d.h:1778
Vector3 Extent
Definition obbox.h:114
Matrix3x3 Basis
Definition obbox.h:112
Vector3 Center
Definition obbox.h:113
float Radius
Definition sphere.h:91
Vector3 Center
Definition sphere.h:90
Definition tri.h:61
float X
Definition vector3.h:90
float Z
Definition vector3.h:92
float Y
Definition vector3.h:91
static WWINLINE float Fabs(float val)
Definition wwmath.h:113