File: Rope.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 90,112 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 39
file content (270 lines) | stat: -rw-r--r-- 8,337 bytes parent folder | download | duplicates (3)
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
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)

#include "Rope.h"

WM5_WINDOW_APPLICATION(Rope);

//----------------------------------------------------------------------------
Rope::Rope ()
    :
    WindowApplication3("SamplePhysics/Rope", 0, 0, 640, 480,
        Float4(0.75f, 0.85f, 0.95f, 1.0f))
{
    mSpline = 0;
    mModule = 0;
    mLastIdle = 0.0f;
}
//----------------------------------------------------------------------------
bool Rope::OnInitialize ()
{
    if (!WindowApplication3::OnInitialize())
    {
        return false;
    }

    CreateScene();

    // Center-and-fit for camera viewing.
    mScene->Update();
    mTrnNode->LocalTransform.SetTranslate(-mScene->WorldBound.GetCenter());
    mCamera->SetFrustum(60.0f, GetAspectRatio(), 1.0f, 100.0f);
    AVector camDVector(0.0f, -1.0f, 0.0f);
    AVector camUVector(0.0f, 0.0f, 1.0f);
    AVector camRVector = camDVector.Cross(camUVector);
    APoint camPosition = APoint::ORIGIN -
        2.5f*mScene->WorldBound.GetRadius()*camDVector - 0.5f*camUVector;
    mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);

    // Initial update of objects.
    mScene->Update();

    // Initial culling of scene.
    mCuller.SetCamera(mCamera);
    mCuller.ComputeVisibleSet(mScene);

    InitializeCameraMotion(0.01f, 0.01f);
    InitializeObjectMotion(mScene);
    return true;
}
//----------------------------------------------------------------------------
void Rope::OnTerminate ()
{
    delete0(mModule);

    mScene = 0;
    mTrnNode = 0;
    mWireState = 0;
    mRope = 0;

    WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
void Rope::OnIdle ()
{
    MeasureTime();

    MoveCamera();
    if (MoveObject())
    {
        mScene->Update();
    }

    float currIdle = (float)GetTimeInSeconds();
    float diff = currIdle - mLastIdle;
    if (diff >= 0.001f)
    {
        mLastIdle = currIdle;
        PhysicsTick();
        GraphicsTick();
    }

    UpdateFrameCount();
}
//----------------------------------------------------------------------------
bool Rope::OnKeyDown (unsigned char key, int x, int y)
{
    int i;

    switch (key)
    {
    case 'w':  // toggle wireframe
    case 'W':
        mWireState->Enabled = !mWireState->Enabled;
        return true;
    case 'm':  // decrease mass
        if (mModule->GetMass(1) > 0.05f)
        {
            for (i = 1; i < mModule->GetNumParticles() - 1; ++i)
            {
                mModule->SetMass(i, mModule->GetMass(i) - 0.01f);
            }
        }
        return true;
    case 'M':  // increase mass
        for (i = 1; i < mModule->GetNumParticles() - 1; ++i)
        {
            mModule->SetMass(i, mModule->GetMass(i) + 0.01f);
        }
        return true;
    case 'c':  // decrease spring constant
        if (mModule->Constant(0) > 0.05f)
        {
            for (i = 0; i < mModule->GetNumSprings(); ++i)
            {
                mModule->Constant(i) -= 0.01f;
            }
        }
        return true;
    case 'C':  // increase spring constant
        for (i = 0; i < mModule->GetNumSprings(); ++i)
        {
            mModule->Constant(i) += 0.01f;
        }
        return true;
    case 'l':  // decrease spring resting length
        if (mModule->Length(0) > 0.05f)
        {
            for (i = 0; i < mModule->GetNumSprings(); ++i)
            {
                mModule->Length(i) -= 0.01f;
            }
        }
        return true;
    case 'L':  // increase spring resting length
        for (i = 0; i < mModule->GetNumSprings(); ++i)
        {
            mModule->Length(i) += 0.01f;
        }
        return true;
    case 'f':  // toggle wind force on/off
    case 'F':
        mModule->EnableWind = !mModule->EnableWind;
        return true;
    case 'r':  // toggle random wind direction change on/off
    case 'R':
        mModule->EnableWindChange = !mModule->EnableWindChange;
        return true;
    }

    return WindowApplication3::OnKeyDown(key, x, y);
}
//----------------------------------------------------------------------------
void Rope::CreateScene ()
{
    mScene = new0 Node();
    mTrnNode = new0 Node();
    mScene->AttachChild(mTrnNode);
    mWireState = new0 WireState();
    mRenderer->SetOverrideWireState(mWireState);

    CreateSprings();
    CreateRope();
}
//----------------------------------------------------------------------------
void Rope::CreateSprings ()
{
    const int numParticles = 8;
    const float step = 0.1f;
    Vector3f gravity(0.0f, 0.0f, -1.0f);
    Vector3f wind(0.0f, -0.25f, 0.0f);
    float windChangeAmplitude = 0.01f;
    float viscosity = 10.0f;
    mModule = new0 PhysicsModule(numParticles, step, gravity, wind,
        windChangeAmplitude, viscosity);

    // Constant mass at interior points (endpoints are immovable).
    mModule->SetMass(0, Mathf::MAX_REAL);
    mModule->SetMass(numParticles - 1, Mathf::MAX_REAL);
    int i;
    for (i = 1; i < numParticles - 1; ++i)
    {
        mModule->SetMass(i, 1.0f);
    }

    // Initial position on a horizontal line segment.
    float factor = 1.0f/(float)(numParticles - 1);
    for (i = 0; i < numParticles; ++i)
    {
        mModule->Position(i) = Vector3f(i*factor, 0.0f, 1.0f);
    }

    // Initial velocities are all zero.
    for (i = 0; i < numParticles; ++i)
    {
        mModule->Velocity(i) = Vector3f::ZERO;
    }

    // Springs are at rest in the initial horizontal configuration.
    int numSprings = numParticles - 1;
    float restLength = 1.0f/(float)numSprings;
    for (i = 0; i < numSprings; ++i)
    {
        mModule->Constant(i) = 10.0f;
        mModule->Length(i) = restLength;
    }
}
//----------------------------------------------------------------------------
void Rope::CreateRope ()
{
    // Create a quadratic spline using particles as control points.
    int numCtrlPoints = mModule->GetNumParticles();
    Vector3f* ctrlPoints = mModule->Positions();
    const int degree = 2;
    mSpline = new0 BSplineCurve3f(numCtrlPoints, ctrlPoints, degree, false,
        true);

    // Generate a tube surface whose medial axis is the spline.
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);

    Float2 tcoordMin(0.0f, 0.0f), tcoordMax(1.0f, 1.0f);

    mRope = new0 TubeSurface(mSpline, Radial, false, Vector3f::UNIT_Z, 64, 8,
        false, false, &tcoordMin, &tcoordMax, vformat);

    // Attach a texture for the rope.
    std::string path = Environment::GetPathR("Rope.wmtf");
    Texture2D* texture = Texture2D::LoadWMTF(path);
    mRope->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture,
        Shader::SF_LINEAR_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT));

    mTrnNode->AttachChild(mRope);
}
//----------------------------------------------------------------------------
void Rope::PhysicsTick ()
{
    // Forces are independent of time, just pass in t = 0.
    mModule->Update(0.0f);

    // Update spline curve.  Remember that the spline maintains its own copy
    // of the control points, so this update is necessary.
    int numCtrlPoints = mModule->GetNumParticles();
    Vector3f* ctrlPoints = mModule->Positions();
    for (int i = 0; i < numCtrlPoints; ++i)
    {
        mSpline->SetControlPoint(i, ctrlPoints[i]);
    }

    mRope->UpdateSurface();
}
//----------------------------------------------------------------------------
void Rope::GraphicsTick ()
{
    mCuller.ComputeVisibleSet(mScene);

    if (mRenderer->PreDraw())
    {
        mRenderer->ClearBuffers();
        mRenderer->Draw(mCuller.GetVisibleSet());
        mRenderer->PostDraw();
        mRenderer->DisplayColorBuffer();
    }
}
//----------------------------------------------------------------------------