Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
colmath.h
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/colmath.h $*
26 * *
27 * Author:: Greg Hjelstrom *
28 * *
29 * $Modtime:: 5/04/01 8:25p $*
30 * *
31 * $Revision:: 19 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37#if defined(_MSC_VER)
38#pragma once
39#endif
40
41#ifndef COLMATH_H
42#define COLMATH_H
43
44#ifndef ALWAYS_H
45#include "always.h"
46#endif
47
48#ifndef VECTOR3_H
49#include "vector3.h"
50#endif
51
52#ifndef CASTRES_H
53#include "castres.h"
54#endif
55
56class AAPlaneClass;
57class PlaneClass;
58class LineSegClass;
59class TriClass;
60class SphereClass;
61class AABoxClass;
62class OBBoxClass;
63class FrustumClass;
64
65const float COLLISION_EPSILON = 0.001f;
66
67/*
68** #define COLMATH_STAT_TRACKING to enable stat tracking for the collision math functions
69*/
70#ifdef WWDEBUG
71#define COLMATH_STAT_TRACKING
72#endif
73
74
80{
81public:
82
84 // Intersect Functions.
85 // These functions simply return a bool indicating whether the two operands intersect.
87 static bool Intersection_Test(const AABoxClass & box,const TriClass & tri);
88 static bool Intersection_Test(const AABoxClass & box,const AABoxClass & box2);
89 static bool Intersection_Test(const AABoxClass & box,const OBBoxClass & box2);
90 static bool Intersection_Test(const OBBoxClass & box,const TriClass & tri);
91 static bool Intersection_Test(const OBBoxClass & box,const AABoxClass & box2);
92 static bool Intersection_Test(const OBBoxClass & box,const OBBoxClass & box2);
93 static bool Intersection_Test(const SphereClass & sphere,const AABoxClass & box);
94 static bool Intersection_Test(const SphereClass & sphere,const OBBoxClass & box);
95
97 // Overlap Functions.
98 // Classify the second operand with respect to the first operand.
99 // For example Overlap_Test(plane,point) tests whether 'point' is in front of or
100 // behind 'plane'.
101 // OverlapType: This enumeration is the result of an overlap test.
102 // It indicates whether the the object is in the positive (front/outside) space
103 // of the volume, the negative (back/inside) space of the volume, or both (overlapping)
106 {
107 POS = 0x01,
108 NEG = 0x02,
109 ON = 0x04,
110 BOTH = 0x08,
116 };
117
118 // AAPlane functions. Where is operand B with respect to this AAPlane
119 static OverlapType Overlap_Test(const AAPlaneClass & plane,const Vector3 & point);
120 static OverlapType Overlap_Test(const AAPlaneClass & plane,const LineSegClass & line);
121 static OverlapType Overlap_Test(const AAPlaneClass & plane,const TriClass & tri);
122 static OverlapType Overlap_Test(const AAPlaneClass & plane,const SphereClass & sphere);
123 static OverlapType Overlap_Test(const AAPlaneClass & plane,const AABoxClass & box);
124 static OverlapType Overlap_Test(const AAPlaneClass & plane,const OBBoxClass & box);
125
126 // Plane functions. Where is operand B with respect to the plane
127 static OverlapType Overlap_Test(const PlaneClass & plane,const Vector3 & point);
128 static OverlapType Overlap_Test(const PlaneClass & plane,const LineSegClass & line);
129 static OverlapType Overlap_Test(const PlaneClass & plane,const TriClass & tri);
130 static OverlapType Overlap_Test(const PlaneClass & plane,const SphereClass & sphere);
131 static OverlapType Overlap_Test(const PlaneClass & plane,const Vector3 & center,const Vector3 & extent);
132 inline static OverlapType Overlap_Test(const PlaneClass & plane,const AABoxClass & box);
133 static OverlapType Overlap_Test(const PlaneClass & plane,const OBBoxClass & box);
134
135 // Sphere functions. Where is operand B with respect to the sphere
136 static OverlapType Overlap_Test(const SphereClass & sphere,const Vector3 & point);
137 static OverlapType Overlap_Test(const SphereClass & sphere,const LineSegClass & line);
138 static OverlapType Overlap_Test(const SphereClass & sphere,const TriClass & tri);
139 static OverlapType Overlap_Test(const SphereClass & sphere,const SphereClass & sphere2);
140 static OverlapType Overlap_Test(const SphereClass & sphere,const AABoxClass & aabox);
141 static OverlapType Overlap_Test(const SphereClass & sphere,const OBBoxClass & obbox);
142
143 // AABox functions. Where is operand B with respect to the AABox
144 static OverlapType Overlap_Test(const AABoxClass & box,const Vector3 & point);
145 static OverlapType Overlap_Test(const AABoxClass & box,const LineSegClass & line);
146 static OverlapType Overlap_Test(const AABoxClass & box,const TriClass & tri);
147 static OverlapType Overlap_Test(const AABoxClass & box,const AABoxClass & box2);
148 static OverlapType Overlap_Test(const AABoxClass & box,const OBBoxClass & obbox);
149 static OverlapType Overlap_Test(const AABoxClass & box,const SphereClass & sphere);
150
151 // OBBox functions, where is operand B with respect to the OBBox
152 static OverlapType Overlap_Test(const OBBoxClass & box,const Vector3 & point);
153 static OverlapType Overlap_Test(const OBBoxClass & box,const LineSegClass & line);
154 static OverlapType Overlap_Test(const OBBoxClass & box,const TriClass & tri);
155 static OverlapType Overlap_Test(const OBBoxClass & box,const AABoxClass & box2);
156 static OverlapType Overlap_Test(const OBBoxClass & box,const OBBoxClass & box2);
157
158 // Frustum functions
159 static OverlapType Overlap_Test(const FrustumClass & frustum,const Vector3 & point);
160 static OverlapType Overlap_Test(const FrustumClass & frustum,const TriClass & tri);
161 static OverlapType Overlap_Test(const FrustumClass & frustum,const SphereClass & sphere);
162 static OverlapType Overlap_Test(const FrustumClass & frustum,const AABoxClass & box);
163 static OverlapType Overlap_Test(const FrustumClass & frustum,const OBBoxClass & box);
164
165 // Frustum functions for hierachical culling systems.
166 // At your root node, just pass in planes_passed = 0, then the variable will be modified to
167 // indicate which planes that volume was inside. You can then pass that value in for the
168 // test of all child nodes and optimize away some of the tests. See AABTreeCullSystemClass
169 // for an example usage.
170 static OverlapType Overlap_Test(const FrustumClass & frustum,const AABoxClass & box,int & planes_passed);
171 static OverlapType Overlap_Test(const FrustumClass & frustum,const OBBoxClass & box,int & planes_passed);
172
173 // Miscellaneous other Overlap tests
174 static OverlapType Overlap_Test(const Vector3 & min,const Vector3 & max,const LineSegClass & line);
175
177 // Collision Functions.
178 // Collide the first operand into the last operand.
179 // For example Collide(box,move,tri) tests for collision between a box moving into
180 // a triangle.
182
183 // Line segment functions. Intersect this line segment with the given object
184 static bool Collide(const LineSegClass & line,const AAPlaneClass & plane,CastResultStruct * result);
185 static bool Collide(const LineSegClass & line,const PlaneClass & plane,CastResultStruct * result);
186 static bool Collide(const LineSegClass & line,const TriClass & tri,CastResultStruct * result);
187 static bool Collide(const LineSegClass & line,const SphereClass & sphere,CastResultStruct * result);
188 static bool Collide(const LineSegClass & line,const AABoxClass & box,CastResultStruct * result);
189 static bool Collide(const LineSegClass & line,const OBBoxClass & box,CastResultStruct * result);
190
191 // AABox functions
192 static bool Collide(const AABoxClass & box,const Vector3 & move,const PlaneClass & plane,CastResultStruct * result);
193 static bool Collide(const AABoxClass & box,const Vector3 & move,const TriClass & tri,CastResultStruct * result);
194 static bool Collide(const AABoxClass & box,const Vector3 & move,const AABoxClass & box2,CastResultStruct * result);
195 static bool Collide(const AABoxClass & box,const Vector3 & move,const OBBoxClass & box2,const Vector3 & move2,CastResultStruct * result);
196
197 // OBBox functions
198 static bool Collide(const OBBoxClass & box,const Vector3 & move,const PlaneClass & plane,CastResultStruct * result);
199 static bool Collide(const OBBoxClass & box,const Vector3 & move,const TriClass & tri,const Vector3 & move2,CastResultStruct * result);
200 static bool Collide(const OBBoxClass & box,const Vector3 & move,const AABoxClass & box2,const Vector3 & move2,CastResultStruct * result);
201 static bool Collide(const OBBoxClass & box,const Vector3 & move,const OBBoxClass & box2,const Vector3 & move2,CastResultStruct * result);
202
204 // Stats
205 // Note that these functions will only work if you have stat tracking enabled
230
231 static void Reset_Stats(void) { Stats.Reset(); }
232 static const ColmathStatsStruct & Get_Current_Stats(void) { return Stats; }
233
234private:
235
236 static OverlapType eval_overlap_mask(int mask);
237 static OverlapType eval_overlap_collision(const CastResultStruct & res);
238
239 static const float COINCIDENCE_EPSILON;
240
241 static ColmathStatsStruct Stats;
242};
243
244
245inline CollisionMath::OverlapType CollisionMath::eval_overlap_mask(int mask)
246{
247 // check if all verts are "ON"
248 if (mask == ON) {
249 return ON;
250 }
251
252 // check if all verts are either "ON" or "POS"
253 if ((mask & ~(POS | ON)) == 0) {
254 return POS;
255 }
256
257 // check if all verts are either "ON" or "BACK"
258 if ((mask & ~(NEG | ON)) == 0) {
259 return NEG;
260 }
261
262 // otherwise, poly spans the plane.
263 return BOTH;
264}
265
266
267inline CollisionMath::OverlapType CollisionMath::eval_overlap_collision(const CastResultStruct & res)
268{
269 if (res.Fraction < 1.0f) {
270 return BOTH;
271 } else {
272 if (res.StartBad) {
273 return NEG;
274 } else {
275 return POS;
276 }
277 }
278}
279
280
281/*
282** Stat tracking Macros
283*/
284#ifdef COLMATH_STAT_TRACKING
285#define TRACK_COLLISION_RAY_TRI Stats.CollisionRayTriCount++; Stats.TotalCollisionCount++;
286#define TRACK_COLLISION_RAY_TRI_HIT Stats.CollisionRayTriHitCount++; Stats.TotalCollisionHitCount;
287#define TRACK_COLLISION_AABOX_TRI Stats.CollisionAABoxTriCount++; Stats.TotalCollisionCount++;
288#define TRACK_COLLISION_AABOX_TRI_HIT Stats.CollisionAABoxTriHitCount++; Stats.TotalCollisionHitCount++;
289#define TRACK_COLLISION_AABOX_AABOX Stats.CollisionAABoxAABoxCount++; Stats.TotalCollisionCount++;
290#define TRACK_COLLISION_AABOX_AABOX_HIT Stats.CollisionAABoxAABoxHitCount++; Stats.TotalCollisionHitCount++;
291#define TRACK_COLLISION_OBBOX_TRI Stats.CollisionOBBoxTriCount++; Stats.TotalCollisionCount++;
292#define TRACK_COLLISION_OBBOX_TRI_HIT Stats.CollisionOBBoxTriHitCount++; Stats.TotalCollisionHitCount++;
293#define TRACK_COLLISION_OBBOX_AABOX Stats.CollisionOBBoxAABoxCount++; Stats.TotalCollisionCount++;
294#define TRACK_COLLISION_OBBOX_AABOX_HIT Stats.CollisionOBBoxAABoxHitCount++; Stats.TotalCollisionHitCount++;
295#define TRACK_COLLISION_OBBOX_OBBOX Stats.CollisionOBBoxOBBoxCount++; Stats.TotalCollisionCount++;
296#define TRACK_COLLISION_OBBOX_OBBOX_HIT Stats.CollisionOBBoxOBBoxHitCount++; Stats.TotalCollisionHitCount++;
297#else
298#define TRACK_COLLISION_RAY_TRI
299#define TRACK_COLLISION_RAY_TRI_HIT
300#define TRACK_COLLISION_AABOX_TRI
301#define TRACK_COLLISION_AABOX_TRI_HIT
302#define TRACK_COLLISION_AABOX_AABOX
303#define TRACK_COLLISION_AABOX_AABOX_HIT
304#define TRACK_COLLISION_OBBOX_TRI
305#define TRACK_COLLISION_OBBOX_TRI_HIT
306#define TRACK_COLLISION_OBBOX_AABOX
307#define TRACK_COLLISION_OBBOX_AABOX_HIT
308#define TRACK_COLLISION_OBBOX_OBBOX
309#define TRACK_COLLISION_OBBOX_OBBOX_HIT
310#endif
311
312#endif // COLMATH_H
#define min(x, y)
Definition BaseType.h:101
#define max(x, y)
Definition BaseType.h:105
const float COINCIDENCE_EPSILON
static bool Collide(const LineSegClass &line, const AAPlaneClass &plane, CastResultStruct *result)
static OverlapType Overlap_Test(const AAPlaneClass &plane, const Vector3 &point)
static OverlapType Overlap_Test(const PlaneClass &plane, const Vector3 &center, const Vector3 &extent)
static const ColmathStatsStruct & Get_Current_Stats(void)
Definition colmath.h:232
static void Reset_Stats(void)
Definition colmath.h:231
static bool Intersection_Test(const AABoxClass &box, const TriClass &tri)
Definition tri.h:61
const float COLLISION_EPSILON
Definition colmath.h:65