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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "portal_physics_collisionevent.h"
#include "physicsshadowclone.h"
#include "prop_combine_ball.h"
#include "prop_portal.h"
#include "portal_player.h"
#include "portal/weapon_physcannon.h" //grab controller
int CPortal_CollisionEvent::ShouldCollide( IPhysicsObject *pObj0, IPhysicsObject *pObj1, void *pGameData0, void *pGameData1 )
{
if ( !pGameData0 || !pGameData1 )
return 1;
AssertOnce( pObj0 && pObj1 );
bool bShadowClonesInvolved = ((pObj0->GetGameFlags() | pObj1->GetGameFlags()) & FVPHYSICS_IS_SHADOWCLONE) != 0;
if( bShadowClonesInvolved )
{
//at least one shadow clone
if( (pObj0->GetGameFlags() & pObj1->GetGameFlags()) & FVPHYSICS_IS_SHADOWCLONE )
return 0; //both are shadow clones
if( (pObj0->GetGameFlags() | pObj1->GetGameFlags()) & FVPHYSICS_PLAYER_HELD )
{
//at least one is held
//don't let players collide with objects they're holding, they get kinda messed up sometimes
if( pGameData0 && ((CBaseEntity *)pGameData0)->IsPlayer() && (GetPlayerHeldEntity( (CBasePlayer *)pGameData0 ) == (CBaseEntity *)pGameData1) )
return 0;
if( pGameData1 && ((CBaseEntity *)pGameData1)->IsPlayer() && (GetPlayerHeldEntity( (CBasePlayer *)pGameData1 ) == (CBaseEntity *)pGameData0) )
return 0;
}
}
//everything is in one environment. This means we must tightly control what collides with what
if( pGameData0 != pGameData1 )
{
//this code only decides what CAN'T collide due to portal environment differences, things that should collide will pass through here to deeper ShouldCollide() code
CBaseEntity *pEntities[2] = { (CBaseEntity *)pGameData0, (CBaseEntity *)pGameData1 };
IPhysicsObject *pPhysObjects[2] = { pObj0, pObj1 };
bool bStatic[2] = { pObj0->IsStatic(), pObj1->IsStatic() };
CPortalSimulator *pSimulators[2];
for( int i = 0; i != 2; ++i )
pSimulators[i] = CPortalSimulator::GetSimulatorThatOwnsEntity( pEntities[i] );
AssertOnce( (bStatic[0] && bStatic[1]) == false ); //hopefully the system doesn't even call in for this, they're both static and can't collide
if( bStatic[0] && bStatic[1] )
return 0;
#ifdef _DEBUG
for( int i = 0; i != 2; ++i )
{
if( (pSimulators[i] != NULL) && CPhysicsShadowClone::IsShadowClone( pEntities[i] ) )
{
CPhysicsShadowClone *pClone = (CPhysicsShadowClone *)pEntities[i];
CBaseEntity *pSource = pClone->GetClonedEntity();
CPortalSimulator *pSourceSimulator = CPortalSimulator::GetSimulatorThatOwnsEntity( pSource );
Assert( (pSimulators[i]->m_DataAccess.Simulation.Dynamic.EntFlags[pClone->entindex()] & PSEF_IS_IN_PORTAL_HOLE) == (pSourceSimulator->m_DataAccess.Simulation.Dynamic.EntFlags[pSource->entindex()] & PSEF_IS_IN_PORTAL_HOLE) );
}
}
#endif
if( pSimulators[0] == pSimulators[1] ) //same simulator
{
if( pSimulators[0] != NULL ) //and not main world
{
if( bStatic[0] || bStatic[1] )
{
for( int i = 0; i != 2; ++i )
{
if( bStatic[i] )
{
if( CPSCollisionEntity::IsPortalSimulatorCollisionEntity( pEntities[i] ) )
{
PS_PhysicsObjectSourceType_t objectSource;
if( pSimulators[i]->CreatedPhysicsObject( pPhysObjects[i], &objectSource ) &&
((objectSource == PSPOST_REMOTE_BRUSHES) || (objectSource == PSPOST_REMOTE_STATICPROPS)) )
{
if( (pSimulators[1-i]->m_DataAccess.Simulation.Dynamic.EntFlags[pEntities[1-i]->entindex()] & PSEF_IS_IN_PORTAL_HOLE) == 0 )
return 0; //require that the entity be in the portal hole before colliding with transformed geometry
//FIXME: The above requirement might fail horribly for transformed collision blocking the portal from the other side and fast moving objects
}
}
break;
}
}
}
else if( bShadowClonesInvolved )
{
if( ((pSimulators[0]->m_DataAccess.Simulation.Dynamic.EntFlags[pEntities[0]->entindex()] |
pSimulators[1]->m_DataAccess.Simulation.Dynamic.EntFlags[pEntities[1]->entindex()]) &
PSEF_IS_IN_PORTAL_HOLE) == 0 )
{
return 0; //neither entity was actually in the portal hole
}
}
}
}
else //different simulators
{
if( bShadowClonesInvolved ) //entities can only collide with shadow clones "owned" by the same simulator.
return 0;
if( bStatic[0] || bStatic[1] )
{
for( int i = 0; i != 2; ++i )
{
if( bStatic[i] )
{
int j = 1-i;
CPortalSimulator *pSimulator_Entity = pSimulators[j];
if( pEntities[i]->IsWorld() )
{
Assert( CPortalSimulator::GetSimulatorThatCreatedPhysicsObject( pPhysObjects[i] ) == NULL );
if( pSimulator_Entity )
return 0;
}
else
{
CPortalSimulator *pSimulator_Static = CPortalSimulator::GetSimulatorThatCreatedPhysicsObject( pPhysObjects[i] ); //might have been a static prop which would yield a new simulator
if( pSimulator_Static && (pSimulator_Static != pSimulator_Entity) )
return 0; //static collideable is from a different simulator
}
break;
}
}
}
else
{
Assert( CPSCollisionEntity::IsPortalSimulatorCollisionEntity( pEntities[0] ) == false );
Assert( CPSCollisionEntity::IsPortalSimulatorCollisionEntity( pEntities[1] ) == false );
for( int i = 0; i != 2; ++i )
{
if( pSimulators[i] )
{
//entities in the physics environment only collide with statics created by the environment (handled above), entities in the same environment (also above), or entities that should be cloned from main to the same environment
if( (pSimulators[i]->m_DataAccess.Simulation.Dynamic.EntFlags[pEntities[1-i]->entindex()] & PSEF_CLONES_ENTITY_FROM_MAIN) == 0 ) //not cloned from main
return 0;
}
}
}
}
}
return BaseClass::ShouldCollide( pObj0, pObj1, pGameData0, pGameData1 );
}
int CPortal_CollisionEvent::ShouldSolvePenetration( IPhysicsObject *pObj0, IPhysicsObject *pObj1, void *pGameData0, void *pGameData1, float dt )
{
if( (pGameData0 == NULL) || (pGameData1 == NULL) )
return 0;
if( CPSCollisionEntity::IsPortalSimulatorCollisionEntity( (CBaseEntity *)pGameData0 ) ||
CPSCollisionEntity::IsPortalSimulatorCollisionEntity( (CBaseEntity *)pGameData1 ) )
return 0;
// For portal, don't solve penetrations on combine balls
if( FClassnameIs( (CBaseEntity *)pGameData0, "prop_energy_ball" ) ||
FClassnameIs( (CBaseEntity *)pGameData1, "prop_energy_ball" ) )
return 0;
if( (pObj0->GetGameFlags() | pObj1->GetGameFlags()) & FVPHYSICS_PLAYER_HELD )
{
//at least one is held
CBaseEntity *pHeld;
CBaseEntity *pOther;
IPhysicsObject *pPhysHeld;
IPhysicsObject *pPhysOther;
if( pObj0->GetGameFlags() & FVPHYSICS_PLAYER_HELD )
{
pHeld = (CBaseEntity *)pGameData0;
pPhysHeld = pObj0;
pOther = (CBaseEntity *)pGameData1;
pPhysOther = pObj1;
}
else
{
pHeld = (CBaseEntity *)pGameData1;
pPhysHeld = pObj1;
pOther = (CBaseEntity *)pGameData0;
pPhysOther = pObj0;
}
//don't let players collide with objects they're holding, they get kinda messed up sometimes
if( pOther->IsPlayer() && (GetPlayerHeldEntity( (CBasePlayer *)pOther ) == pHeld) )
return 0;
//held objects are clipping into other objects when travelling across a portal. We're close to ship, so this seems to be the
//most localized way to make a fix.
//Note that we're not actually going to change whether it should solve, we're just going to tack on some hacks
CPortal_Player *pHoldingPlayer = (CPortal_Player *)GetPlayerHoldingEntity( pHeld );
if( !pHoldingPlayer && CPhysicsShadowClone::IsShadowClone( pHeld ) )
pHoldingPlayer = (CPortal_Player *)GetPlayerHoldingEntity( ((CPhysicsShadowClone *)pHeld)->GetClonedEntity() );
Assert( pHoldingPlayer );
if( pHoldingPlayer )
{
CGrabController *pGrabController = GetGrabControllerForPlayer( pHoldingPlayer );
if ( !pGrabController )
pGrabController = GetGrabControllerForPhysCannon( pHoldingPlayer->GetActiveWeapon() );
Assert( pGrabController );
if( pGrabController )
{
GrabController_SetPortalPenetratingEntity( pGrabController, pOther );
}
//NDebugOverlay::EntityBounds( pHeld, 0, 0, 255, 16, 1.0f );
//NDebugOverlay::EntityBounds( pOther, 255, 0, 0, 16, 1.0f );
//pPhysOther->Wake();
//FindClosestPassableSpace( pOther, Vector( 0.0f, 0.0f, 1.0f ) );
}
}
if( (pObj0->GetGameFlags() | pObj1->GetGameFlags()) & FVPHYSICS_IS_SHADOWCLONE )
{
//at least one shadowclone is involved
if( (pObj0->GetGameFlags() & pObj1->GetGameFlags()) & FVPHYSICS_IS_SHADOWCLONE ) //don't solve between two shadowclones, they're just going to resync in a frame anyways
return 0;
IPhysicsObject * const pObjects[2] = { pObj0, pObj1 };
for( int i = 0; i != 2; ++i )
{
if( pObjects[i]->GetGameFlags() & FVPHYSICS_IS_SHADOWCLONE )
{
int j = 1 - i;
if( !pObjects[j]->IsMoveable() )
return 0; //don't solve between shadow clones and statics
if( ((CPhysicsShadowClone *)(pObjects[i]->GetGameData()))->GetClonedEntity() == (pObjects[j]->GetGameData()) )
return 0; //don't solve between a shadow clone and its source entity
}
}
}
return BaseClass::ShouldSolvePenetration( pObj0, pObj1, pGameData0, pGameData1, dt );
}
// Data for energy ball vs held item mass swapping hack
static float s_fSavedMass[2];
static bool s_bChangedMass[2] = { false, false };
static bool s_bUseUnshadowed[2] = { false, false };
static IPhysicsObject *s_pUnshadowed[2] = { NULL, NULL };
static void ModifyWeight_PreCollision( vcollisionevent_t *pEvent )
{
Assert( (pEvent->pObjects[0] != NULL) && (pEvent->pObjects[1] != NULL) );
CBaseEntity *pUnshadowedEntities[2];
IPhysicsObject *pUnshadowedObjects[2];
for( int i = 0; i != 2; ++i )
{
if( pEvent->pObjects[i]->GetGameFlags() & FVPHYSICS_IS_SHADOWCLONE )
{
CPhysicsShadowClone *pClone = ((CPhysicsShadowClone *)pEvent->pObjects[i]->GetGameData());
pUnshadowedEntities[i] = pClone->GetClonedEntity();
if( pUnshadowedEntities[i] == NULL )
return;
pUnshadowedObjects[i] = pClone->TranslatePhysicsToClonedEnt( pEvent->pObjects[i] );
if( pUnshadowedObjects[i] == NULL )
return;
}
else
{
pUnshadowedEntities[i] = (CBaseEntity *)pEvent->pObjects[i]->GetGameData();
pUnshadowedObjects[i] = pEvent->pObjects[i];
}
}
// HACKHACK: Reduce mass for combine ball vs movable brushes so the collision
// appears fully elastic regardless of mass ratios
for( int i = 0; i != 2; ++i )
{
int j = 1-i;
// One is a combine ball, if the other is a movable brush, reduce the combine ball mass
if ( dynamic_cast<CPropCombineBall *>(pUnshadowedEntities[j]) != NULL && pUnshadowedEntities[i] != NULL )
{
if ( pUnshadowedEntities[i]->GetMoveType() == MOVETYPE_PUSH )
{
s_bChangedMass[j] = true;
s_fSavedMass[j] = pUnshadowedObjects[j]->GetMass();
pEvent->pObjects[j]->SetMass( VPHYSICS_MIN_MASS );
if( pUnshadowedObjects[j] != pEvent->pObjects[j] )
{
s_bUseUnshadowed[j] = true;
s_pUnshadowed[j] = pUnshadowedObjects[j];
pUnshadowedObjects[j]->SetMass( VPHYSICS_MIN_MASS );
}
}
//HACKHACK: last minute problem knocking over turrets with energy balls, up the mass of the ball by a lot
if( FClassnameIs( pUnshadowedEntities[i], "npc_portal_turret_floor" ) )
{
pUnshadowedObjects[j]->SetMass( pUnshadowedEntities[i]->VPhysicsGetObject()->GetMass() );
}
}
}
for( int i = 0; i != 2; ++i )
{
if( ( pUnshadowedObjects[i] && pUnshadowedObjects[i]->GetGameFlags() & FVPHYSICS_PLAYER_HELD ) )
{
int j = 1-i;
if( dynamic_cast<CPropCombineBall *>(pUnshadowedEntities[j]) != NULL )
{
// [j] is the combine ball, set mass low
// if the above ball vs brush entity check didn't already change the mass, change the mass
if ( !s_bChangedMass[j] )
{
s_bChangedMass[j] = true;
s_fSavedMass[j] = pUnshadowedObjects[j]->GetMass();
pEvent->pObjects[j]->SetMass( VPHYSICS_MIN_MASS );
if( pUnshadowedObjects[j] != pEvent->pObjects[j] )
{
s_bUseUnshadowed[j] = true;
s_pUnshadowed[j] = pUnshadowedObjects[j];
pUnshadowedObjects[j]->SetMass( VPHYSICS_MIN_MASS );
}
}
// [i] is the held object, set mass high
s_bChangedMass[i] = true;
s_fSavedMass[i] = pUnshadowedObjects[i]->GetMass();
pEvent->pObjects[i]->SetMass( VPHYSICS_MAX_MASS );
if( pUnshadowedObjects[i] != pEvent->pObjects[i] )
{
s_bUseUnshadowed[i] = true;
s_pUnshadowed[i] = pUnshadowedObjects[i];
pUnshadowedObjects[i]->SetMass( VPHYSICS_MAX_MASS );
}
}
else if( pEvent->pObjects[j]->GetGameFlags() & FVPHYSICS_IS_SHADOWCLONE )
{
//held object vs shadow clone, set held object mass back to grab controller saved mass
// [i] is the held object
s_bChangedMass[i] = true;
s_fSavedMass[i] = pUnshadowedObjects[i]->GetMass();
CGrabController *pGrabController = NULL;
CBaseEntity *pLookingForEntity = (CBaseEntity*)pEvent->pObjects[i]->GetGameData();
CBasePlayer *pHoldingPlayer = GetPlayerHoldingEntity( pLookingForEntity );
if( pHoldingPlayer )
pGrabController = GetGrabControllerForPlayer( pHoldingPlayer );
float fSavedMass, fSavedRotationalDamping;
AssertMsg( pGrabController, "Physics object is held, but we can't find the holding controller." );
GetSavedParamsForCarriedPhysObject( pGrabController, pUnshadowedObjects[i], &fSavedMass, &fSavedRotationalDamping );
pEvent->pObjects[i]->SetMass( fSavedMass );
if( pUnshadowedObjects[i] != pEvent->pObjects[i] )
{
s_bUseUnshadowed[i] = true;
s_pUnshadowed[i] = pUnshadowedObjects[i];
pUnshadowedObjects[i]->SetMass( fSavedMass );
}
}
}
}
}
void CPortal_CollisionEvent::PreCollision( vcollisionevent_t *pEvent )
{
ModifyWeight_PreCollision( pEvent );
return BaseClass::PreCollision( pEvent );
}
static void ModifyWeight_PostCollision( vcollisionevent_t *pEvent )
{
for( int i = 0; i != 2; ++i )
{
if( s_bChangedMass[i] )
{
pEvent->pObjects[i]->SetMass( s_fSavedMass[i] );
if( s_bUseUnshadowed[i] )
{
s_pUnshadowed[i]->SetMass( s_fSavedMass[i] );
s_bUseUnshadowed[i] = false;
}
s_bChangedMass[i] = false;
}
}
}
void CPortal_CollisionEvent::PostCollision( vcollisionevent_t *pEvent )
{
ModifyWeight_PostCollision( pEvent );
return BaseClass::PostCollision( pEvent );
}
void CPortal_CollisionEvent::PostSimulationFrame()
{
//this actually happens once per physics environment simulation, and we don't want that, so do nothing and we'll get a different version manually called
}
void CPortal_CollisionEvent::PortalPostSimulationFrame( void )
{
BaseClass::PostSimulationFrame();
}
void CPortal_CollisionEvent::AddDamageEvent( CBaseEntity *pEntity, const CTakeDamageInfo &info, IPhysicsObject *pInflictorPhysics, bool bRestoreVelocity, const Vector &savedVel, const AngularImpulse &savedAngVel )
{
const CTakeDamageInfo *pPassDownInfo = &info;
CTakeDamageInfo ReplacementDamageInfo; //only used some of the time
if( (info.GetDamageType() & DMG_CRUSH) &&
(pInflictorPhysics->GetGameFlags() & FVPHYSICS_IS_SHADOWCLONE) &&
(!info.BaseDamageIsValid()) &&
(info.GetDamageForce().LengthSqr() > (20000.0f * 20000.0f))
)
{
//VERY likely this was caused by the penetration solver. Since a shadow clone is involved we're going to ignore it becuase it causes more problems than it solves in this case
ReplacementDamageInfo = info;
ReplacementDamageInfo.SetDamage( 0.0f );
pPassDownInfo = &ReplacementDamageInfo;
}
BaseClass::AddDamageEvent( pEntity, *pPassDownInfo, pInflictorPhysics, bRestoreVelocity, savedVel, savedAngVel );
}
|