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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "rope_physics.h"
#include "tier0/dbg.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
CBaseRopePhysics::CBaseRopePhysics( CSimplePhysics::CNode *pNodes, int nNodes, CRopeSpring *pSprings, float *flSpringDistsSqr )
{
m_pNodes = pNodes;
m_pSprings = pSprings;
m_flNodeSpringDistsSqr = flSpringDistsSqr;
m_flSpringDist = m_flSpringDistSqr = 1;
Restart();
// Initialize the nodes.
for ( int i=0; i < nNodes; i++ )
{
pNodes[i].m_vPos.Init();
pNodes[i].m_vPrevPos.Init();
pNodes[i].m_vPredicted.Init();
}
SetNumNodes( nNodes );
m_pDelegate = NULL;
}
void CBaseRopePhysics::SetNumNodes( int nNodes )
{
m_nNodes = nNodes;
// Setup the springs.
for( int i=0; i < NumSprings(); i++ )
{
m_pSprings[i].m_pNode1 = &m_pNodes[i].m_vPos;
m_pSprings[i].m_pNode2 = &m_pNodes[i+1].m_vPos;
Assert( m_pSprings[i].m_pNode1->IsValid() );
Assert( m_pSprings[i].m_pNode2->IsValid() );
m_flNodeSpringDistsSqr[i] = m_flSpringDistSqr / NumSprings();
}
}
void CBaseRopePhysics::Restart()
{
m_Physics.Init( 1.0 / 50 );
}
void CBaseRopePhysics::ResetSpringLength( float flSpringDist )
{
m_flSpringDist = max( flSpringDist, 0 );
m_flSpringDistSqr = m_flSpringDist * m_flSpringDist;
for( int i=0; i < NumSprings(); i++ )
{
m_flNodeSpringDistsSqr[i] = m_flSpringDistSqr / NumSprings();
}
}
float CBaseRopePhysics::GetSpringLength() const
{
return m_flSpringDist;
}
void CBaseRopePhysics::ResetNodeSpringLength( int iStartNode, float flSpringDist )
{
m_flNodeSpringDistsSqr[iStartNode] = flSpringDist * flSpringDist;
}
void CBaseRopePhysics::SetupSimulation( float flSpringDist, CSimplePhysics::IHelper *pDelegate )
{
ResetSpringLength( flSpringDist );
SetDelegate( pDelegate );
}
void CBaseRopePhysics::SetDelegate( CSimplePhysics::IHelper *pDelegate )
{
m_pDelegate = pDelegate;
}
void CBaseRopePhysics::Simulate( float dt )
{
static float flEnergy = 0.98;
m_Physics.Simulate( m_pNodes, m_nNodes, this, dt, flEnergy );
}
void CBaseRopePhysics::GetNodeForces( CSimplePhysics::CNode *pNodes, int iNode, Vector *pAccel )
{
if( m_pDelegate )
m_pDelegate->GetNodeForces( pNodes, iNode, pAccel );
else
pAccel->Init( 0, 0, 0 );
}
void CBaseRopePhysics::ApplyConstraints( CSimplePhysics::CNode *pNodes, int nNodes )
{
// Handle springs..
//
// Iterate multiple times here. If we don't, then gravity tends to
// win over the constraint solver and it's impossible to get straight ropes.
static int nIterations = 3;
for( int iIteration=0; iIteration < nIterations; iIteration++ )
{
for( int i=0; i < NumSprings(); i++ )
{
CRopeSpring *s = &m_pSprings[i];
Vector vTo = *s->m_pNode1 - *s->m_pNode2;
float flDistSqr = vTo.LengthSqr();
// If we don't have an overall spring distance, see if we have a per-node one
float flSpringDist = m_flSpringDistSqr;
if ( !flSpringDist )
{
// TODO: This still isn't enough. Ropes with different spring lengths
// per-node will oscillate forever.
flSpringDist = m_flNodeSpringDistsSqr[i];
}
if( flDistSqr > flSpringDist )
{
float flDist = (float)sqrt( flDistSqr );
vTo *= 1 - (m_flSpringDist / flDist);
*s->m_pNode1 -= vTo * 0.5f;
*s->m_pNode2 += vTo * 0.5f;
}
}
if( m_pDelegate )
m_pDelegate->ApplyConstraints( pNodes, nNodes );
}
}
|