forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollisionproperty.h
503 lines (396 loc) · 16.8 KB
/
collisionproperty.h
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#ifndef COLLISIONPROPERTY_H
#define COLLISIONPROPERTY_H
#ifdef _WIN32
#pragma once
#endif
#include "networkvar.h"
#include "engine/ICollideable.h"
#include "mathlib/vector.h"
#include "ispatialpartition.h"
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CBaseEntity;
class IHandleEntity;
class QAngle;
class Vector;
struct Ray_t;
class IPhysicsObject;
//-----------------------------------------------------------------------------
// Force spatial partition updates (to avoid threading problems caused by lazy update)
//-----------------------------------------------------------------------------
void UpdateDirtySpatialPartitionEntities();
//-----------------------------------------------------------------------------
// Specifies how to compute the surrounding box
//-----------------------------------------------------------------------------
enum SurroundingBoundsType_t
{
USE_OBB_COLLISION_BOUNDS = 0,
USE_BEST_COLLISION_BOUNDS, // Always use the best bounds (most expensive)
USE_HITBOXES,
USE_SPECIFIED_BOUNDS,
USE_GAME_CODE,
USE_ROTATION_EXPANDED_BOUNDS,
USE_COLLISION_BOUNDS_NEVER_VPHYSICS,
SURROUNDING_TYPE_BIT_COUNT = 3
};
//-----------------------------------------------------------------------------
// Encapsulates collision representation for an entity
//-----------------------------------------------------------------------------
class CCollisionProperty : public ICollideable
{
DECLARE_CLASS_NOBASE( CCollisionProperty );
DECLARE_EMBEDDED_NETWORKVAR();
DECLARE_PREDICTABLE();
#ifdef GAME_DLL
DECLARE_DATADESC();
#endif
public:
CCollisionProperty();
~CCollisionProperty();
void Init( CBaseEntity *pEntity );
// Methods of ICollideable
virtual IHandleEntity *GetEntityHandle();
virtual const Vector& OBBMinsPreScaled() const { return m_vecMinsPreScaled.Get(); }
virtual const Vector& OBBMaxsPreScaled() const { return m_vecMaxsPreScaled.Get(); }
virtual const Vector& OBBMins() const { return m_vecMins.Get(); }
virtual const Vector& OBBMaxs() const { return m_vecMaxs.Get(); }
virtual void WorldSpaceTriggerBounds( Vector *pVecWorldMins, Vector *pVecWorldMaxs ) const;
virtual bool TestCollision( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr );
virtual bool TestHitboxes( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr );
virtual int GetCollisionModelIndex();
virtual const model_t* GetCollisionModel();
virtual const Vector& GetCollisionOrigin() const;
virtual const QAngle& GetCollisionAngles() const;
virtual const matrix3x4_t& CollisionToWorldTransform() const;
virtual SolidType_t GetSolid() const;
virtual int GetSolidFlags() const;
virtual IClientUnknown* GetIClientUnknown();
virtual int GetCollisionGroup() const;
virtual void WorldSpaceSurroundingBounds( Vector *pVecMins, Vector *pVecMaxs );
virtual bool ShouldTouchTrigger( int triggerSolidFlags ) const;
virtual const matrix3x4_t *GetRootParentToWorldTransform() const;
public:
// Spatial partition management
void CreatePartitionHandle();
void DestroyPartitionHandle();
unsigned short GetPartitionHandle() const;
// Marks the spatial partition dirty
void MarkPartitionHandleDirty();
// Sets the collision bounds + the size (OBB)
void SetCollisionBounds( const Vector& mins, const Vector &maxs );
// Rebuilds the scaled bounds from the pre-scaled bounds after a model's scale has changed
void RefreshScaledCollisionBounds( void );
// Sets special trigger bounds. The bloat amount indicates how much bigger the
// trigger bounds should be beyond the bounds set in SetCollisionBounds
// This method will also set the FSOLID flag FSOLID_USE_TRIGGER_BOUNDS
void UseTriggerBounds( bool bEnable, float flBloat = 0.0f );
// Sets the method by which the surrounding collision bounds is set
// You must pass in values for mins + maxs if you select the USE_SPECIFIED_BOUNDS type.
void SetSurroundingBoundsType( SurroundingBoundsType_t type, const Vector *pMins = NULL, const Vector *pMaxs = NULL );
// Sets the solid type (which type of collision representation)
void SetSolid( SolidType_t val );
// Methods related to size. The OBB here is measured in CollisionSpace
// (specified by GetCollisionToWorld)
const Vector& OBBSize( ) const;
// Returns a radius (or the square of the radius) of a sphere
// *centered at the world space center* bounding the collision representation
// of the entity. NOTE: The world space center *may* move when the entity rotates.
float BoundingRadius() const;
float BoundingRadius2D() const;
// Returns the center of the OBB in collision space
const Vector & OBBCenter( ) const;
// center point of entity measured in world space
// NOTE: This point *may* move when the entity moves depending on
// which solid type is being used.
const Vector & WorldSpaceCenter( ) const;
// Methods related to solid flags
void ClearSolidFlags( void );
void RemoveSolidFlags( int flags );
void AddSolidFlags( int flags );
bool IsSolidFlagSet( int flagMask ) const;
void SetSolidFlags( int flags );
bool IsSolid() const;
// Updates the spatial partition
void UpdatePartition( );
// Are the bounds defined in entity space?
bool IsBoundsDefinedInEntitySpace() const;
// Transforms a point in OBB space to world space
const Vector & CollisionToWorldSpace( const Vector &in, Vector *pResult ) const;
// Transforms a point in world space to OBB space
const Vector & WorldToCollisionSpace( const Vector &in, Vector *pResult ) const;
// Transforms a direction in world space to OBB space
const Vector & WorldDirectionToCollisionSpace( const Vector &in, Vector *pResult ) const;
// Selects a random point in the bounds given the normalized 0-1 bounds
void RandomPointInBounds( const Vector &vecNormalizedMins, const Vector &vecNormalizedMaxs, Vector *pPoint) const;
// Is a worldspace point within the bounds of the OBB?
bool IsPointInBounds( const Vector &vecWorldPt ) const;
// Computes a bounding box in world space surrounding the collision bounds
void WorldSpaceAABB( Vector *pWorldMins, Vector *pWorldMaxs ) const;
// Get the collision space mins directly
const Vector & CollisionSpaceMins( void ) const;
// Get the collision space maxs directly
const Vector & CollisionSpaceMaxs( void ) const;
// Computes a "normalized" point (range 0,0,0 - 1,1,1) in collision space
// Useful for things like getting a point 75% of the way along z on the OBB, for example
const Vector & NormalizedToCollisionSpace( const Vector &in, Vector *pResult ) const;
// Computes a "normalized" point (range 0,0,0 - 1,1,1) in world space
const Vector & NormalizedToWorldSpace( const Vector &in, Vector *pResult ) const;
// Transforms a point in world space to normalized space
const Vector & WorldToNormalizedSpace( const Vector &in, Vector *pResult ) const;
// Transforms a point in collision space to normalized space
const Vector & CollisionToNormalizedSpace( const Vector &in, Vector *pResult ) const;
// Computes the nearest point in the OBB to a point specified in world space
void CalcNearestPoint( const Vector &vecWorldPt, Vector *pVecNearestWorldPt ) const;
// Computes the distance from a point in world space to the OBB
float CalcDistanceFromPoint( const Vector &vecWorldPt ) const;
// Does a rotation make us need to recompute the surrounding box?
bool DoesRotationInvalidateSurroundingBox( ) const;
// Does VPhysicsUpdate make us need to recompute the surrounding box?
bool DoesVPhysicsInvalidateSurroundingBox( ) const;
// Marks the entity has having a dirty surrounding box
void MarkSurroundingBoundsDirty();
// Compute the largest dot product of the OBB and the specified direction vector
float ComputeSupportMap( const Vector &vecDirection ) const;
private:
// Transforms an AABB measured in collision space to a box that surrounds it in world space
void CollisionAABBToWorldAABB( const Vector &entityMins, const Vector &entityMaxs, Vector *pWorldMins, Vector *pWorldMaxs ) const;
// Expand trigger bounds..
void ComputeVPhysicsSurroundingBox( Vector *pVecWorldMins, Vector *pVecWorldMaxs );
// Expand trigger bounds..
bool ComputeHitboxSurroundingBox( Vector *pVecWorldMins, Vector *pVecWorldMaxs );
bool ComputeEntitySpaceHitboxSurroundingBox( Vector *pVecWorldMins, Vector *pVecWorldMaxs );
// Computes the surrounding collision bounds based on whatever algorithm we want...
void ComputeCollisionSurroundingBox( bool bUseVPhysics, Vector *pVecWorldMins, Vector *pVecWorldMaxs );
// Computes the surrounding collision bounds from the the OBB (not vphysics)
void ComputeRotationExpandedBounds( Vector *pVecWorldMins, Vector *pVecWorldMaxs );
// Computes the surrounding collision bounds based on whatever algorithm we want...
void ComputeSurroundingBox( Vector *pVecWorldMins, Vector *pVecWorldMaxs );
// Check for untouch
void CheckForUntouch();
// Updates the spatial partition
void UpdateServerPartitionMask( );
// Outer
CBaseEntity *GetOuter();
const CBaseEntity *GetOuter() const;
private:
CBaseEntity *m_pOuter;
CNetworkVector( m_vecMinsPreScaled );
CNetworkVector( m_vecMaxsPreScaled );
CNetworkVector( m_vecMins );
CNetworkVector( m_vecMaxs );
float m_flRadius;
CNetworkVar( unsigned short, m_usSolidFlags );
// Spatial partition
SpatialPartitionHandle_t m_Partition;
CNetworkVar( unsigned char, m_nSurroundType );
// One of the SOLID_ defines. Use GetSolid/SetSolid.
CNetworkVar( unsigned char, m_nSolidType );
CNetworkVar( unsigned char , m_triggerBloat );
// SUCKY: We didn't use to have to store this previously
// but storing it here means that we can network it + avoid a ton of
// client-side mismatch problems
CNetworkVector( m_vecSpecifiedSurroundingMinsPreScaled );
CNetworkVector( m_vecSpecifiedSurroundingMaxsPreScaled );
CNetworkVector( m_vecSpecifiedSurroundingMins );
CNetworkVector( m_vecSpecifiedSurroundingMaxs );
// Cached off world-aligned surrounding bounds
#if 0
short m_surroundingMins[3];
short m_surroundingMaxs[3];
#else
Vector m_vecSurroundingMins;
Vector m_vecSurroundingMaxs;
#endif
// pointer to the entity's physics object (vphysics.dll)
//IPhysicsObject *m_pPhysicsObject;
friend class CBaseEntity;
};
//-----------------------------------------------------------------------------
// For networking this bad boy
//-----------------------------------------------------------------------------
#ifdef CLIENT_DLL
EXTERN_RECV_TABLE( DT_CollisionProperty );
#else
EXTERN_SEND_TABLE( DT_CollisionProperty );
#endif
//-----------------------------------------------------------------------------
// Inline methods
//-----------------------------------------------------------------------------
inline CBaseEntity *CCollisionProperty::GetOuter()
{
return m_pOuter;
}
inline const CBaseEntity *CCollisionProperty::GetOuter() const
{
return m_pOuter;
}
//-----------------------------------------------------------------------------
// Spatial partition
//-----------------------------------------------------------------------------
inline unsigned short CCollisionProperty::GetPartitionHandle() const
{
return m_Partition;
}
//-----------------------------------------------------------------------------
// Methods related to size
//-----------------------------------------------------------------------------
inline const Vector& CCollisionProperty::OBBSize( ) const
{
// NOTE: Could precache this, but it's not used that often..
Vector &temp = AllocTempVector();
VectorSubtract( m_vecMaxs, m_vecMins, temp );
return temp;
}
//-----------------------------------------------------------------------------
// Bounding radius size
//-----------------------------------------------------------------------------
inline float CCollisionProperty::BoundingRadius() const
{
return m_flRadius;
}
//-----------------------------------------------------------------------------
// Methods relating to solid flags
//-----------------------------------------------------------------------------
inline bool CCollisionProperty::IsBoundsDefinedInEntitySpace() const
{
return (( m_usSolidFlags & FSOLID_FORCE_WORLD_ALIGNED ) == 0 ) &&
( m_nSolidType != SOLID_BBOX ) && ( m_nSolidType != SOLID_NONE );
}
inline void CCollisionProperty::ClearSolidFlags( void )
{
SetSolidFlags( 0 );
}
inline void CCollisionProperty::RemoveSolidFlags( int flags )
{
SetSolidFlags( m_usSolidFlags & ~flags );
}
inline void CCollisionProperty::AddSolidFlags( int flags )
{
SetSolidFlags( m_usSolidFlags | flags );
}
inline int CCollisionProperty::GetSolidFlags( void ) const
{
return m_usSolidFlags;
}
inline bool CCollisionProperty::IsSolidFlagSet( int flagMask ) const
{
return (m_usSolidFlags & flagMask) != 0;
}
inline bool CCollisionProperty::IsSolid() const
{
return ::IsSolid( (SolidType_t)(unsigned char)m_nSolidType, m_usSolidFlags );
}
//-----------------------------------------------------------------------------
// Returns the center in OBB space
//-----------------------------------------------------------------------------
inline const Vector& CCollisionProperty::OBBCenter( ) const
{
Vector &vecResult = AllocTempVector();
VectorLerp( m_vecMins, m_vecMaxs, 0.5f, vecResult );
return vecResult;
}
//-----------------------------------------------------------------------------
// center point of entity
//-----------------------------------------------------------------------------
inline const Vector &CCollisionProperty::WorldSpaceCenter( ) const
{
Vector &vecResult = AllocTempVector();
CollisionToWorldSpace( OBBCenter(), &vecResult );
return vecResult;
}
//-----------------------------------------------------------------------------
// Transforms a point in OBB space to world space
//-----------------------------------------------------------------------------
inline const Vector &CCollisionProperty::CollisionToWorldSpace( const Vector &in, Vector *pResult ) const
{
// Makes sure we don't re-use the same temp twice
if ( !IsBoundsDefinedInEntitySpace() || ( GetCollisionAngles() == vec3_angle ) )
{
VectorAdd( in, GetCollisionOrigin(), *pResult );
}
else
{
VectorTransform( in, CollisionToWorldTransform(), *pResult );
}
return *pResult;
}
//-----------------------------------------------------------------------------
// Transforms a point in world space to OBB space
//-----------------------------------------------------------------------------
inline const Vector &CCollisionProperty::WorldToCollisionSpace( const Vector &in, Vector *pResult ) const
{
if ( !IsBoundsDefinedInEntitySpace() || ( GetCollisionAngles() == vec3_angle ) )
{
VectorSubtract( in, GetCollisionOrigin(), *pResult );
}
else
{
VectorITransform( in, CollisionToWorldTransform(), *pResult );
}
return *pResult;
}
//-----------------------------------------------------------------------------
// Transforms a direction in world space to OBB space
//-----------------------------------------------------------------------------
inline const Vector & CCollisionProperty::WorldDirectionToCollisionSpace( const Vector &in, Vector *pResult ) const
{
if ( !IsBoundsDefinedInEntitySpace() || ( GetCollisionAngles() == vec3_angle ) )
{
*pResult = in;
}
else
{
VectorIRotate( in, CollisionToWorldTransform(), *pResult );
}
return *pResult;
}
//-----------------------------------------------------------------------------
// Computes a bounding box in world space surrounding the collision bounds
//-----------------------------------------------------------------------------
inline void CCollisionProperty::WorldSpaceAABB( Vector *pWorldMins, Vector *pWorldMaxs ) const
{
CollisionAABBToWorldAABB( m_vecMins, m_vecMaxs, pWorldMins, pWorldMaxs );
}
// Get the collision space mins directly
inline const Vector & CCollisionProperty::CollisionSpaceMins( void ) const
{
return m_vecMins;
}
// Get the collision space maxs directly
inline const Vector & CCollisionProperty::CollisionSpaceMaxs( void ) const
{
return m_vecMaxs;
}
//-----------------------------------------------------------------------------
// Does a rotation make us need to recompute the surrounding box?
//-----------------------------------------------------------------------------
inline bool CCollisionProperty::DoesRotationInvalidateSurroundingBox( ) const
{
if ( IsSolidFlagSet(FSOLID_ROOT_PARENT_ALIGNED) )
return true;
switch ( m_nSurroundType )
{
case USE_COLLISION_BOUNDS_NEVER_VPHYSICS:
case USE_OBB_COLLISION_BOUNDS:
case USE_BEST_COLLISION_BOUNDS:
return IsBoundsDefinedInEntitySpace();
// In the case of game code, we don't really know, so we have to assume it does
case USE_HITBOXES:
case USE_GAME_CODE:
return true;
case USE_ROTATION_EXPANDED_BOUNDS:
case USE_SPECIFIED_BOUNDS:
return false;
default:
Assert(0);
return true;
}
}
#endif // COLLISIONPROPERTY_H