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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "simple_physics.h"
#include "mathlib/vmatrix.h"
#include "beamdraw.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
class C_Hairball : public C_BaseEntity
{
DECLARE_CLASS( C_Hairball, C_BaseEntity );
private:
class CHairballDelegate : public CSimplePhysics::IHelper
{
public:
virtual void GetNodeForces( CSimplePhysics::CNode *pNodes, int iNode, Vector *pAccel );
virtual void ApplyConstraints( CSimplePhysics::CNode *pNodes, int nNodes );
C_Hairball *m_pParent;
};
public:
C_Hairball();
void Init();
// IClientThinkable.
public:
virtual void ClientThink();
// IClientRenderable.
public:
virtual int DrawModel( int flags );
public:
float m_flSphereRadius;
int m_nHairs;
int m_nNodesPerHair;
float m_flSpringDist; // = hair length / (m_nNodesPerHair-1)
CUtlVector<CSimplePhysics::CNode> m_Nodes; // This is m_nHairs * m_nNodesPerHair large.
CUtlVector<Vector> m_HairPositions; // Untransformed base hair positions, distributed on the sphere.
CUtlVector<Vector> m_TransformedHairPositions; // Transformed base hair positions, distributed on the sphere.
CHairballDelegate m_Delegate;
CSimplePhysics m_Physics;
IMaterial *m_pMaterial;
// Super sophisticated AI.
float m_flSitStillTime;
Vector m_vMoveDir;
float m_flSpinDuration;
float m_flCurSpinTime;
float m_flSpinRateX, m_flSpinRateY;
bool m_bFirstThink;
};
void C_Hairball::CHairballDelegate::GetNodeForces( CSimplePhysics::CNode *pNodes, int iNode, Vector *pAccel )
{
pAccel->Init( 0, 0, -1500 );
}
void C_Hairball::CHairballDelegate::ApplyConstraints( CSimplePhysics::CNode *pNodes, int nNodes )
{
int nSegments = m_pParent->m_nNodesPerHair - 1;
float flSpringDistSqr = m_pParent->m_flSpringDist * m_pParent->m_flSpringDist;
static int nIterations = 1;
for( int iIteration=0; iIteration < nIterations; iIteration++ )
{
for ( int iHair=0; iHair < m_pParent->m_nHairs; iHair++ )
{
CSimplePhysics::CNode *pBase = &pNodes[iHair * m_pParent->m_nNodesPerHair];
for( int i=0; i < nSegments; i++ )
{
Vector &vNode1 = pBase[i].m_vPos;
Vector &vNode2 = pBase[i+1].m_vPos;
Vector vTo = vNode1 - vNode2;
float flDistSqr = vTo.LengthSqr();
if( flDistSqr > flSpringDistSqr )
{
float flDist = (float)sqrt( flDistSqr );
vTo *= 1 - (m_pParent->m_flSpringDist / flDist);
vNode1 -= vTo * 0.5f;
vNode2 += vTo * 0.5f;
}
}
// Lock the base of each hair to the right spot.
pBase->m_vPos = m_pParent->m_TransformedHairPositions[iHair];
}
}
}
C_Hairball::C_Hairball()
{
m_nHairs = 100;
m_nNodesPerHair = 3;
float flHairLength = 20;
m_flSpringDist = flHairLength / (m_nNodesPerHair - 1);
m_Nodes.SetSize( m_nHairs * m_nNodesPerHair );
m_HairPositions.SetSize( m_nHairs );
m_TransformedHairPositions.SetSize( m_nHairs );
m_flSphereRadius = 20;
m_vMoveDir.Init();
m_flSpinDuration = 1;
m_flCurSpinTime = 0;
m_flSpinRateX = m_flSpinRateY = 0;
// Distribute on the sphere (need a better random distribution for the sphere).
for ( int i=0; i < m_HairPositions.Count(); i++ )
{
float theta = RandomFloat( -M_PI, M_PI );
float phi = RandomFloat( -M_PI/2, M_PI/2 );
float cosPhi = cos( phi );
m_HairPositions[i].Init(
cos(theta) * cosPhi * m_flSphereRadius,
sin(theta) * cosPhi * m_flSphereRadius,
sin(phi) * m_flSphereRadius );
}
m_Delegate.m_pParent = this;
m_Physics.Init( 1.0 / 20 ); // NOTE: PLAY WITH THIS FOR EFFICIENCY
m_pMaterial = NULL;
m_bFirstThink = true;
}
void C_Hairball::Init()
{
ClientEntityList().AddNonNetworkableEntity( this );
ClientThinkList()->SetNextClientThink( GetClientHandle(), CLIENT_THINK_ALWAYS );
AddToLeafSystem( RENDER_GROUP_OPAQUE_ENTITY );
m_pMaterial = materials->FindMaterial( "cable/cable", TEXTURE_GROUP_OTHER );
m_flSitStillTime = 5;
}
void C_Hairball::ClientThink()
{
// Do some AI-type stuff.. move the entity around.
//C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
//m_vecAngles = SetAbsAngles( pPlayer->GetAbsAngles() ); // copy player angles.
Assert( !GetMoveParent() );
// Sophisticated AI.
m_flCurSpinTime += gpGlobals->frametime;
if ( m_flCurSpinTime < m_flSpinDuration )
{
float div = m_flCurSpinTime / m_flSpinDuration;
QAngle angles = GetLocalAngles();
angles.x += m_flSpinRateX * SmoothCurve( div );
angles.y += m_flSpinRateY * SmoothCurve( div );
SetLocalAngles( angles );
}
else
{
// Flip between stopped and starting.
if ( fabs( m_flSpinRateX ) > 0.01f )
{
m_flSpinRateX = m_flSpinRateY = 0;
m_flSpinDuration = RandomFloat( 1, 2 );
}
else
{
static float flXSpeed = 3;
static float flYSpeed = flXSpeed * 0.1f;
m_flSpinRateX = RandomFloat( -M_PI*flXSpeed, M_PI*flXSpeed );
m_flSpinRateY = RandomFloat( -M_PI*flYSpeed, M_PI*flYSpeed );
m_flSpinDuration = RandomFloat( 1, 4 );
}
m_flCurSpinTime = 0;
}
if ( m_flSitStillTime > 0 )
{
m_flSitStillTime -= gpGlobals->frametime;
if ( m_flSitStillTime <= 0 )
{
// Shoot out some random lines and find the longest one.
m_vMoveDir.Init( 1, 0, 0 );
float flLongestFraction = 0;
for ( int i=0; i < 15; i++ )
{
Vector vDir( RandomFloat( -1, 1 ), RandomFloat( -1, 1 ), RandomFloat( -1, 1 ) );
VectorNormalize( vDir );
trace_t trace;
UTIL_TraceLine( GetAbsOrigin(), GetAbsOrigin() + vDir * 10000, MASK_SOLID, NULL, COLLISION_GROUP_NONE, &trace );
if ( trace.fraction != 1.0 )
{
if ( trace.fraction > flLongestFraction )
{
flLongestFraction = trace.fraction;
m_vMoveDir = vDir;
}
}
}
m_vMoveDir *= 650; // set speed.
m_flSitStillTime = -1; // Move in this direction..
}
}
else
{
// Move in the specified direction.
Vector vEnd = GetAbsOrigin() + m_vMoveDir * gpGlobals->frametime;
trace_t trace;
UTIL_TraceLine( GetAbsOrigin(), vEnd, MASK_SOLID, NULL, COLLISION_GROUP_NONE, &trace );
if ( trace.fraction < 1 )
{
// Ok, stop moving.
m_flSitStillTime = RandomFloat( 1, 3 );
}
else
{
SetLocalOrigin( GetLocalOrigin() + m_vMoveDir * gpGlobals->frametime );
}
}
// Transform the base hair positions so we can lock them down.
VMatrix mTransform;
mTransform.SetupMatrixOrgAngles( GetLocalOrigin(), GetLocalAngles() );
for ( int i=0; i < m_HairPositions.Count(); i++ )
{
Vector3DMultiplyPosition( mTransform, m_HairPositions[i], m_TransformedHairPositions[i] );
}
if ( m_bFirstThink )
{
m_bFirstThink = false;
for ( int i=0; i < m_HairPositions.Count(); i++ )
{
for ( int j=0; j < m_nNodesPerHair; j++ )
{
m_Nodes[i*m_nNodesPerHair+j].Init( m_TransformedHairPositions[i] );
}
}
}
// Simulate the physics and apply constraints.
m_Physics.Simulate( m_Nodes.Base(), m_Nodes.Count(), &m_Delegate, gpGlobals->frametime, 0.98 );
}
int C_Hairball::DrawModel( int flags )
{
if ( !m_pMaterial )
return 0;
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
for ( int iHair=0; iHair < m_nHairs; iHair++ )
{
CSimplePhysics::CNode *pBase = &m_Nodes[iHair * m_nNodesPerHair];
CBeamSegDraw beamDraw;
beamDraw.Start( pRenderContext, m_nNodesPerHair-1, m_pMaterial );
for ( int i=0; i < m_nNodesPerHair; i++ )
{
BeamSeg_t seg;
seg.m_vPos = pBase[i].m_vPredicted;
seg.m_vColor.Init( 0, 0, 0 );
seg.m_flTexCoord = 0;
static float flHairWidth = 1;
seg.m_flWidth = flHairWidth;
seg.m_flAlpha = 0;
beamDraw.NextSeg( &seg );
}
beamDraw.End();
}
return 1;
}
void CreateHairballCallback()
{
for ( int i=0; i < 20; i++ )
{
C_Hairball *pHairball = new C_Hairball;
pHairball->Init();
// Put it a short distance in front of the player.
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
Vector vForward;
AngleVectors( pPlayer->GetAbsAngles(), &vForward );
pHairball->SetLocalOrigin( pPlayer->GetAbsOrigin() + vForward * 300 + RandomVector( 0, 100 ) );
}
}
ConCommand cc_CreateHairball( "CreateHairball", CreateHairballCallback, 0, FCVAR_CHEAT );
|