diff options
Diffstat (limited to '2d/softbody/softbody_1/undamped.cpp')
-rw-r--r-- | 2d/softbody/softbody_1/undamped.cpp | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/2d/softbody/softbody_1/undamped.cpp b/2d/softbody/softbody_1/undamped.cpp index ec53f50..a77e2a4 100644 --- a/2d/softbody/softbody_1/undamped.cpp +++ b/2d/softbody/softbody_1/undamped.cpp @@ -16,10 +16,10 @@ namespace Undamped { struct SpringWeight { Mesh2d shape; - float32 radius; - float32 mass = 2.f; + float32 radius = 2.f; + float32 mass = 1.f; - void load(Renderer2d* renderer, float32 inRadius, Vector4 startColor, Vector4 endColor); + void load(Renderer2d* renderer, float32 inMass, Vector4 startColor, Vector4 endColor); void update(float32 dtSeconds); void render(Renderer2d* renderer); void unload(); @@ -28,24 +28,31 @@ namespace Undamped { struct Spring { SpringWeight* weight; + // -- Rendering Mesh2d shape; - Vertex2d* vertices = NULL; int32 numSegments = 0; int32 numVertices = 0; + + // -- Spring initial variables float32 k = 4; // Spring Constant, in N / m float32 initialDisplacement = 3.f; float32 initialVelocity = 0.f; - float32 angularVelocity = 0.f; // To be calculated + + // -- Spring runtime variables + float32 angularVelocity = 0.f; float32 displacement = 0.f; float32 timeElapsed = 0.f; - void load(Renderer2d* renderer, SpringWeight* inWieight, float32 length, float32 loopRadius); + void load(Renderer2d* renderer, SpringWeight* inWieight, float32 length, float32 inInitialDisplacement, float32 inK, float32 loopRadius); void update(float32 dtSeconds); void render(Renderer2d* renderer); void unload(); }; + UndampedInitVariables initVariables; + void setInitVariables(UndampedInitVariables newVariables) { initVariables = newVariables; } + UndampedInitVariables getInitVariables() { return initVariables; } WebglContext* context; Renderer2d renderer; MainLoop mainLoop; @@ -69,8 +76,8 @@ namespace Undamped { renderer.load(context); - weight.load(&renderer, 32.f, Vector4 { 55.f, 235.f, 35.f, 255.f }, Vector4 { 235.f, 5.f, 235.f, 255.f }); - spring.load(&renderer, &weight, 250.f, 64.f); + weight.load(&renderer, initVariables.mass, Vector4 { 55.f, 235.f, 35.f, 255.f }, Vector4 { 235.f, 5.f, 235.f, 255.f }); + spring.load(&renderer, &weight, initVariables.springLength, initVariables.initialDisplacement, initVariables.k, 64.f); mainLoop.run(update); } @@ -96,8 +103,9 @@ namespace Undamped { } - void SpringWeight::load(Renderer2d* renderer, float32 inRadius, Vector4 startColor, Vector4 endColor) { - radius = inRadius; + void SpringWeight::load(Renderer2d* renderer, float32 inMass, Vector4 startColor, Vector4 endColor) { + mass = inMass; + radius = mass * 16.f; const int32 numSegments = 96; const float32 radiansPerSegment = (2.f * PI) / static_cast<float>(numSegments); const int32 numVertices = numSegments * 3; @@ -141,11 +149,15 @@ namespace Undamped { shape.unload(); } - void Spring::load(Renderer2d* renderer, SpringWeight* inWeight, float32 length, float32 loopRadius) { + void Spring::load(Renderer2d* renderer, SpringWeight* inWeight, float32 length, float32 inInitialDisplacement, float32 inK, float32 loopRadius) { weight = inWeight; + initialDisplacement = inInitialDisplacement; + displacement = initialDisplacement; + k = inK; + angularVelocity = sqrtf(k / weight->mass); timeElapsed = 0.f; - + const int32 verticesPerSegment = 6; numSegments = 256; numVertices = numSegments * verticesPerSegment; @@ -181,12 +193,14 @@ namespace Undamped { void Spring::update(float32 dtSeconds) { timeElapsed += dtSeconds; + float32 lastDisplacement = displacement; displacement = initialDisplacement * cosf(angularVelocity * timeElapsed - initialVelocity); + float32 dx = displacement - lastDisplacement; int32 vidx = 0; for (int pidx = 0; pidx < numSegments; pidx++) { - float32 y1Offset = displacement * (1.f - pidx / static_cast<float32>(numSegments)); - float32 y2Offset = displacement * (1.f - (pidx + 1) / static_cast<float32>(numSegments)); + float32 y1Offset = dx * (1.f - pidx / static_cast<float32>(numSegments)); + float32 y2Offset = dx * (1.f - (pidx + 1) / static_cast<float32>(numSegments)); vertices[vidx++].position.y += y1Offset; vertices[vidx++].position.y += y2Offset; vertices[vidx++].position.y += y1Offset; @@ -194,8 +208,8 @@ namespace Undamped { vertices[vidx++].position.y += y2Offset; vertices[vidx++].position.y += y2Offset; } - - weight->shape.model = weight->shape.model.translateByVec2(Vector2(0, displacement)); + + weight->shape.model = weight->shape.model.translateByVec2(Vector2(0, dx)); } void Spring::render(Renderer2d* renderer) { |