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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#ifndef SHEETSIMULATOR_H
#define SHEETSIMULATOR_H
#ifdef _WIN32
#pragma once
#endif
#include "mathlib/mathlib.h"
#include "mathlib/vector.h"
#include "utlvector.h"
// Uncomment this for client-side simulation
//#define CLIENT_SIDE_SIMULATION 1
//-----------------------------------------------------------------------------
// Simulates a sheet
//-----------------------------------------------------------------------------
class CGameTrace;
typedef CGameTrace trace_t;
//struct trace_t;
typedef void (*TraceLineFunc_t)(const Vector &vecStart, const Vector &vecEnd,
unsigned int mask, int collisionGroup, trace_t *ptr);
typedef void (*TraceHullFunc_t)(const Vector &vecStart, const Vector &vecEnd,
const Vector &hullMin, const Vector &hullMax,
unsigned int mask, int collisionGroup, trace_t *ptr);
class CSheetSimulator
{
public:
CSheetSimulator( TraceLineFunc_t traceline, TraceHullFunc_t traceHull );
~CSheetSimulator();
void Init( int w, int h, int fixedPointCount );
// orientation
void SetPosition( const Vector& origin, const QAngle& angles );
// Makes a spring
void AddSpring( int p1, int p2, float restLength );
void AddFixedPointSpring( int fixedPoint, int p, float restLength );
// spring constants....
void SetPointSpringConstant( float constant );
void SetFixedSpringConstant( float constant );
// Used for both kinds of springs
void SetSpringDampConstant( float damp );
void SetViscousDrag( float drag );
// Sets the collision group
void SetCollisionGroup( int group );
// Sets the bounding box used for collision vs world
void SetBoundingBox( Vector& mins, Vector& maxs );
// Computes the bounding box
void ComputeBounds( Vector& mins, Vector& maxs );
// simulation
void Simulate( float dt );
void Simulate( float dt, int steps );
// get at the points
int NumHorizontal() const;
int NumVertical() const;
int PointCount() const;
// Fixed points
Vector& GetFixedPoint( int i );
// Point masses
const Vector& GetPoint( int x, int y ) const;
const Vector& GetPoint( int i ) const;
// For iterative collision detection
void DetectCollision( int i, float flOffset );
void InitPosition( int i );
// For offseting the control points
void SetControlPointOffset( const Vector& offset );
// Gravity
void SetGravityConstant( float g );
void AddGravityForce( int particle );
protected:
struct Particle_t
{
float m_Mass;
Vector m_Position;
Vector m_Velocity;
Vector m_Force;
int m_Collided;
int m_CollisionPlane;
float m_CollisionDist;
};
struct Spring_t
{
int m_Particle1;
int m_Particle2;
float m_RestLength;
};
inline int NumParticles() const
{
return m_HorizontalCount * m_VerticalCount;
}
// simulator
void EulerStep( float dt );
void ComputeControlPoints();
void ClearForces();
void ComputeForces();
void TestVertAgainstPlane( int vert, int plane, bool bFarTest = true );
void SatisfyCollisionConstraints();
void DetermineBestCollisionPlane( bool bFarTest = true );
void ClampPointsToCollisionPlanes();
// How many particles horiz + vert?
int m_HorizontalCount;
int m_VerticalCount;
// The particles
Particle_t* m_Particle;
// Output position after simulation
Vector* m_OutputPosition;
// fixed points
int m_FixedPointCount;
Vector* m_pFixedPoint;
Vector* m_ControlPoints;
CUtlVector<Spring_t> m_Springs;
CUtlVector<int> m_Gravity;
// raycasting methods
TraceLineFunc_t m_TraceLine;
TraceHullFunc_t m_TraceHull;
// Spring constants
float m_FixedSpringConstant;
float m_PointSpringConstant;
float m_DampConstant;
float m_ViscousDrag;
// Collision group
int m_CollisionGroup;
// position + orientation
Vector m_Origin;
QAngle m_Angles;
// collision box
Vector m_FrustumBoxMin;
Vector m_FrustumBoxMax;
// Collision planes
cplane_t* m_pCollisionPlanes;
bool* m_pValidCollisionPlane;
// Control point offset
Vector m_ControlPointOffset;
// Gravity
float m_GravityConstant;
};
//-----------------------------------------------------------------------------
// Class to help dealing with the iterative computation
//-----------------------------------------------------------------------------
class CIterativeSheetSimulator : public CSheetSimulator
{
public:
CIterativeSheetSimulator( TraceLineFunc_t traceline, TraceHullFunc_t traceHull );
void BeginSimulation( float dt, int steps, int substeps, int collisionCount );
// Returns true if it just did a simulation step
bool Think( );
bool IsDone() const { return m_SimulationSteps == 0; }
int StepsRemaining( ) const { return m_SimulationSteps; }
private:
CIterativeSheetSimulator( const CIterativeSheetSimulator & ); // not defined, not accessible
// Iterative collision detection
void DetectCollisions( void );
float m_TimeStep;
float m_SubSteps;
char m_TotalSteps;
char m_SimulationSteps;
char m_CollisionCount;
char m_CurrentCollisionPt;
bool m_InitialPass;
};
#endif // TF_SHIELD_SHARED_H
|