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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Local fast collision system for particles
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "particle_collision.h"
#include "engine/ivdebugoverlay.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef _XBOX
#define __DEBUG_PARTICLE_COLLISION_RETEST 0
#else
#define __DEBUG_PARTICLE_COLLISION_RETEST 1
#endif // _XBOX
#define __DEBUG_PARTICLE_COLLISION_OVERLAY 0
#define __DEBUG_PARTICLE_COLLISION_OVERLAY_LIFETIME 0.1f
#define NUM_DISCREET_STEPS 8.0f
#define NUM_SIMULATION_SECONDS 2.0f
#define COLLISION_EPSILON 0.01f
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CBaseSimpleCollision::CBaseSimpleCollision( void )
{
ClearActivePlanes();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : &origin -
// radius -
//-----------------------------------------------------------------------------
void CBaseSimpleCollision::Setup( const Vector &origin, float speed, float gravity )
{
TestForPlane( origin, Vector( 1, 0, 0 ), speed, gravity );
TestForPlane( origin, Vector( -1, 0, 0 ), speed, gravity );
TestForPlane( origin, Vector( 0, 1, 0 ), speed, gravity );
TestForPlane( origin, Vector( 0, -1, 0 ), speed, gravity );
TestForPlane( origin, Vector( 0, 0, 1 ), speed, gravity );
TestForPlane( origin, Vector( 0, 0, -1 ), speed, gravity );
}
//-----------------------------------------------------------------------------
// Purpose: Trace line for super-simplified traces
// Input : &start - start position
// &end - end position
// *pTrace - trace structure to fill
// coarse - tests again with a real trace unless coarse is set
//-----------------------------------------------------------------------------
void CBaseSimpleCollision::TraceLine( const Vector &start, const Vector &end, trace_t *pTrace, bool coarse )
{
//Iterate over all active planes
for ( int i = 0; i < m_nActivePlanes; i++ )
{
//Must be a valid plane
if ( m_collisionPlanes[i].m_Dist == -1.0f )
continue;
//Get our information about the relation to this plane
float dot1 = m_collisionPlanes[i].DistTo(start);
float dot2 = m_collisionPlanes[i].DistTo(end);
//Don't consider particles on the backside of planes
if ( dot1 < -COLLISION_EPSILON )
continue;
//Must be crossing the plane's boundary
if ( ( dot1 > COLLISION_EPSILON ) == ( dot2 > COLLISION_EPSILON ) )
continue;
//Find the intersection point
float t = dot1 / (dot1 - dot2);
Vector vIntersection = start + (end - start) * t;
//Fake the collision info
pTrace->endpos = vIntersection;
pTrace->fraction = t - COLLISION_EPSILON;
pTrace->plane.normal = m_collisionPlanes[i].m_Normal;
pTrace->plane.dist = m_collisionPlanes[i].m_Dist;
//If we need an exact trace, test again on a successful hit
if ( ( coarse == false ) && ( pTrace->fraction < 1.0f ) )
{
UTIL_TraceLine( start, end, MASK_SOLID_BRUSHONLY, NULL, COLLISION_GROUP_NONE, pTrace );
}
#if __DEBUG_PARTICLE_COLLISION_OVERLAY
debugoverlay->AddBoxOverlay( vIntersection, Vector(-1,-1,-1), Vector(1,1,1), QAngle(0,0,0), 0, 255, 0, 16, __DEBUG_PARTICLE_COLLISION_OVERLAY_LIFETIME );
#endif //__DEBUG_PARTICLE_COLLISION_OVERLAY
//Done
return;
}
//Fell through, so clear all the fields
pTrace->plane.normal[0] = 0.0f;
pTrace->plane.normal[1] = 0.0f;
pTrace->plane.normal[2] = 0.0f;
pTrace->plane.dist = 0.0f;
pTrace->fraction = 1.0f;
pTrace->allsolid = false;
pTrace->startsolid = false;
pTrace->m_pEnt = NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Tests the planes against all others for validity
// Input : *plane - plane to test
//-----------------------------------------------------------------------------
void CBaseSimpleCollision::ConsiderPlane( cplane_t *plane )
{
//Test against all other active planes
for ( int i = 0; i < m_nActivePlanes; i++ )
{
if ( m_collisionPlanes[i].m_Dist != -1.0f )
{
//Test for coplanar
if ( ( m_collisionPlanes[i].m_Normal == plane->normal ) && ( m_collisionPlanes[i].m_Dist == plane->dist ) )
return;
}
}
//Don't overrun
if ( m_nActivePlanes >= MAX_COLLISION_PLANES )
return;
//Take it
m_collisionPlanes[m_nActivePlanes].m_Dist = plane->dist;
m_collisionPlanes[m_nActivePlanes].m_Normal = plane->normal;
m_nActivePlanes++;
}
//-----------------------------------------------------------------------------
// Purpose: Runs a simulation of the average particle's movement, looking for collisions along the way
// Input : &start - start of the simulation
// &dir - direction of travel
// speed - speed of the particle
// gravity - gravity being used
//-----------------------------------------------------------------------------
void CBaseSimpleCollision::TestForPlane( const Vector &start, const Vector &dir, float speed, float gravity )
{
trace_t tr;
Vector testStart, testEnd;
testStart = start;
//Setup our step increments
float dStepTime = (NUM_SIMULATION_SECONDS/NUM_DISCREET_STEPS);
Vector vStepIncr = dir * ( speed * dStepTime );
float flGravIncr = gravity*dStepTime;
//Simulate collsions in discreet steps
for ( int i = 1; i <= NUM_DISCREET_STEPS; i++ )
{
testEnd = testStart + vStepIncr;
testEnd[2] -= flGravIncr * (0.5f*(dStepTime*i)*(dStepTime*i) );
//Trace the line
UTIL_TraceLine( testStart, testEnd, MASK_SOLID_BRUSHONLY, NULL, COLLISION_GROUP_NONE, &tr );
//See if we found one
if ( tr.fraction != 1.0f )
{
#if __DEBUG_PARTICLE_COLLISION_OVERLAY
debugoverlay->AddLineOverlay( testStart, tr.endpos, 255, 0, 0, true, __DEBUG_PARTICLE_COLLISION_OVERLAY_LIFETIME );
QAngle angles;
VectorAngles( tr.plane.normal,angles );
angles[PITCH] += 90;
debugoverlay->AddBoxOverlay( tr.endpos, Vector(-64,-64,0), Vector(64,64,0), angles, 255, 0, 0, 16, __DEBUG_PARTICLE_COLLISION_OVERLAY_LIFETIME );
#endif //__DEBUG_PARTICLE_COLLISION_OVERLAY
//Test the plane against a set of criteria
ConsiderPlane( &tr.plane );
return;
}
//We missed
#if __DEBUG_PARTICLE_COLLISION_OVERLAY
debugoverlay->AddLineOverlay( testStart, tr.endpos, 0, 128.0f+(128.0f*((float)i/(float)NUM_DISCREET_STEPS)), 0, true, __DEBUG_PARTICLE_COLLISION_OVERLAY_LIFETIME );
#endif //__DEBUG_PARTICLE_COLLISION_OVERLAY
//Save that position for the next round
testStart = testEnd;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseSimpleCollision::ClearActivePlanes( void )
{
for ( int i = 0; i < MAX_COLLISION_PLANES; i++ )
{
m_collisionPlanes[i].m_Dist = -1.0f;
m_collisionPlanes[i].m_Normal.Init();
}
m_nActivePlanes = 0;
}
//
// CParticleCollision
//
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CParticleCollision::CParticleCollision( void )
{
m_flGravity = 800.0f;
m_flCollisionDampen = 0.5f;
m_flAngularCollisionDampen = 0.25f;
ClearActivePlanes();
}
//-----------------------------------------------------------------------------
// Purpose: Test for surrounding collision surfaces for quick collision testing for the particle system
// Input : &origin - starting position
// *dir - direction of movement (if NULL, will do a point emission test in four directions)
// angularSpread - looseness of the spread
// minSpeed - minimum speed
// maxSpeed - maximum speed
// gravity - particle gravity for the sytem
// dampen - dampening amount on collisions
//-----------------------------------------------------------------------------
void CParticleCollision::Setup( const Vector &origin, const Vector *dir, float angularSpread, float minSpeed, float maxSpeed, float gravity, float dampen )
{
//Take the information for this simulation
m_flGravity = gravity;
m_flCollisionDampen = dampen;
m_nActivePlanes = 0;
//We take a rough estimation of the spray
float speedAvg = (minSpeed+maxSpeed)*0.5f;
//Point or directed?
if ( dir == NULL )
{
//Test all around
TestForPlane( origin, Vector( 1, 0, 0 ), speedAvg, gravity );
TestForPlane( origin, Vector( -1, 0, 0 ), speedAvg, gravity );
TestForPlane( origin, Vector( 0, 1, 0 ), speedAvg, gravity );
TestForPlane( origin, Vector( 0, -1, 0 ), speedAvg, gravity );
TestForPlane( origin, Vector( 0, 0, 1 ), speedAvg, gravity );
TestForPlane( origin, Vector( 0, 0, -1 ), speedAvg, gravity );
}
else
{
Vector vSkewDir, vRight;
QAngle vAngles;
//FIXME: Quicker conversion?
//FIXME: We need to factor in the angular spread instead
VectorAngles( *dir, vAngles );
AngleVectors( vAngles, NULL, &vRight, NULL );
//Test straight
TestForPlane( origin, *dir, speedAvg, gravity );
vSkewDir = vRight;
//Test right
TestForPlane( origin, vSkewDir, speedAvg, gravity );
vSkewDir *= -1.0f;
//Test left
TestForPlane( origin, vSkewDir, speedAvg, gravity );
#if __DEBUG_PARTICLE_COLLISION_OVERLAY
DevMsg( 1, "CParticleCollision: Found %d active plane(s)\n", m_nActivePlanes );
#endif //__DEBUG_PARTICLE_COLLISION_OVERLAY
}
}
//-----------------------------------------------------------------------------
// Purpose: Simulate movement, with collision
// Input : &origin - position of the particle
// &velocity - velocity of the particle
// &rollDelta - roll delta of the particle
// timeDelta - time step
//-----------------------------------------------------------------------------
bool CParticleCollision::MoveParticle( Vector &origin, Vector &velocity, float *rollDelta, float timeDelta, trace_t *pTrace )
{
//Don't bother with non-moving particles
if ( velocity == vec3_origin )
return false;
//Factor in gravity
velocity[2] -= m_flGravity * timeDelta;
//Move
Vector testPosition = ( origin + ( velocity * timeDelta ) );
//Only collide if we have active planes
if ( m_nActivePlanes > 0 )
{
//Collide
TraceLine( origin, testPosition, pTrace );
//See if we hit something
if ( pTrace->fraction != 1.0f )
{
#if __DEBUG_PARTICLE_COLLISION_RETEST
//Retest the collision with a true trace line to avoid errant collisions
UTIL_TraceLine( origin, testPosition, MASK_SOLID_BRUSHONLY, NULL, COLLISION_GROUP_NONE, pTrace );
#endif //__DEBUG_RETEST_COLLISION
//Did we hit anything?
if ( pTrace->fraction != 1.0f )
{
//See if we've settled
if ( ( pTrace->plane.normal[2] >= 0.5f ) && ( fabs( velocity[2] ) <= 48.0f ) )
{
//Leave the particle at the collision point
origin += velocity * ( (pTrace->fraction-COLLISION_EPSILON) * timeDelta );
//Stop the particle
velocity = vec3_origin;
if ( rollDelta != NULL )
{
*rollDelta = 0.0f;
}
return false;
}
else
{
//Move the particle to the collision point
origin += velocity * ( (pTrace->fraction-COLLISION_EPSILON) * timeDelta );
//Find the reflection vector
float proj = velocity.Dot( pTrace->plane.normal );
velocity += pTrace->plane.normal * (-proj*2.0f);
//Apply dampening
velocity *= random->RandomFloat( (m_flCollisionDampen-0.1f), (m_flCollisionDampen+0.1f) );
//Dampen the roll of the particles
if ( rollDelta != NULL )
{
(*rollDelta) *= -0.25f;
}
return true;
}
}
else
{
#if __DEBUG_PARTICLE_COLLISION_OVERLAY
//Display a false hit
debugoverlay->AddBoxOverlay( pTrace->endpos, Vector(-1,-1,-1), Vector(1,1,1), QAngle(0,0,0), 255, 0, 0, 16, __DEBUG_PARTICLE_COLLISION_OVERLAY_LIFETIME );
#endif //__DEBUG_PARTICLE_COLLISION_OVERLAY
}
}
}
//Simple move, no collision
origin = testPosition;
return false;
}
|