summaryrefslogtreecommitdiff
path: root/2d/softbody/softbody_1
diff options
context:
space:
mode:
authormattkae <mattkae@protonmail.com>2022-01-29 10:44:32 -0500
committermattkae <mattkae@protonmail.com>2022-01-29 10:44:32 -0500
commit7b4dedf0ce66c405d6a4c4006862350053c71127 (patch)
treea44b4f7634c035b366bdbc859596af91ba9b64d2 /2d/softbody/softbody_1
parent606d9ee1f0f11727471e133cc001c83c0d47e9df (diff)
Doing undamped simulation correctly
Diffstat (limited to '2d/softbody/softbody_1')
-rw-r--r--2d/softbody/softbody_1/dist/output.js26
-rwxr-xr-x2d/softbody/softbody_1/dist/output.wasmbin69005 -> 69604 bytes
-rw-r--r--2d/softbody/softbody_1/undamped.cpp37
3 files changed, 47 insertions, 16 deletions
diff --git a/2d/softbody/softbody_1/dist/output.js b/2d/softbody/softbody_1/dist/output.js
index 9de60fb..9992b73 100644
--- a/2d/softbody/softbody_1/dist/output.js
+++ b/2d/softbody/softbody_1/dist/output.js
@@ -2676,6 +2676,31 @@ var ASM_CONSTS = {
GL.postDrawHandleClientVertexAttribBindings();
}
+ function _glDrawElements(mode, count, type, indices) {
+ var buf;
+ if (!GLctx.currentElementArrayBufferBinding) {
+ var size = GL.calcBufLength(1, type, 0, count);
+ buf = GL.getTempIndexBuffer(size);
+ GLctx.bindBuffer(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, buf);
+ GLctx.bufferSubData(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/,
+ 0,
+ HEAPU8.subarray(indices, indices + size));
+ // the index is now 0
+ indices = 0;
+ }
+
+ // bind any client-side buffers
+ GL.preDrawHandleClientVertexAttribBindings(count);
+
+ GLctx.drawElements(mode, count, type, indices);
+
+ GL.postDrawHandleClientVertexAttribBindings(count);
+
+ if (!GLctx.currentElementArrayBufferBinding) {
+ GLctx.bindBuffer(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, null);
+ }
+ }
+
function _glEnable(x0) { GLctx['enable'](x0) }
function _glEnableVertexAttribArray(index) {
@@ -3059,6 +3084,7 @@ var asmLibraryArg = {
"glDepthFunc": _glDepthFunc,
"glDepthMask": _glDepthMask,
"glDrawArrays": _glDrawArrays,
+ "glDrawElements": _glDrawElements,
"glEnable": _glEnable,
"glEnableVertexAttribArray": _glEnableVertexAttribArray,
"glGenBuffers": _glGenBuffers,
diff --git a/2d/softbody/softbody_1/dist/output.wasm b/2d/softbody/softbody_1/dist/output.wasm
index 4c82a10..b034adc 100755
--- a/2d/softbody/softbody_1/dist/output.wasm
+++ b/2d/softbody/softbody_1/dist/output.wasm
Binary files differ
diff --git a/2d/softbody/softbody_1/undamped.cpp b/2d/softbody/softbody_1/undamped.cpp
index b041a6f..68e0636 100644
--- a/2d/softbody/softbody_1/undamped.cpp
+++ b/2d/softbody/softbody_1/undamped.cpp
@@ -36,13 +36,11 @@ namespace Undamped {
// -- Spring initial variables
float32 k = 4; // Spring Constant, in N / m
- float32 initialDisplacement = 3.f;
- float32 initialVelocity = 0.f;
// -- Spring runtime variables
- float32 angularVelocity = 0.f;
- float32 displacement = 0.f;
- float32 timeElapsed = 0.f;
+ float32 force = 0.f;
+ float32 velocity = 0.f;
+ float32 position = 0.f;
void load(Renderer2d* renderer, SpringWeight* inWieight, float32 length, float32 inInitialDisplacement, float32 inK, float32 loopRadius);
void update(float32 dtSeconds);
@@ -106,6 +104,7 @@ namespace Undamped {
void SpringWeight::load(Renderer2d* renderer, float32 inMass, Vector4 startColor, Vector4 endColor) {
mass = inMass;
radius = mass * 16.f;
+ if (radius > 42) radius = 42.f;
const int32 numSegments = 96;
const float32 radiansPerSegment = (2.f * PI) / static_cast<float>(numSegments);
const int32 numVertices = numSegments * 3;
@@ -149,14 +148,12 @@ namespace Undamped {
shape.unload();
}
- void Spring::load(Renderer2d* renderer, SpringWeight* inWeight, float32 length, float32 inInitialDisplacement, float32 inK, float32 loopRadius) {
+ void Spring::load(Renderer2d* renderer, SpringWeight* inWeight, float32 length, float32 initialDisplacement, float32 inK, float32 loopRadius) {
weight = inWeight;
- initialDisplacement = inInitialDisplacement;
- displacement = 0;
+ position = initialDisplacement;
k = inK;
-
- angularVelocity = sqrtf(k / weight->mass);
- timeElapsed = 0.f;
+ force = -k * initialDisplacement;
+ velocity = sqrtf(k / weight->mass);
const int32 verticesPerSegment = 6;
numSegments = 256;
@@ -170,12 +167,19 @@ namespace Undamped {
const float32 offset = 0.25f;
int32 vidx = 0;
+ float32 dx = position;
for (int pidx = 0; pidx < numSegments; pidx++) {
float32 y1 = lengthIncrement * pidx;
float32 x1 = loopWidth * sinf(frequency * y1 + offset);
float32 y2 = y1 + lengthIncrement;
float32 x2 = loopWidth * sinf(frequency * y2 + offset);
+
+ float32 y1Offset = dx * (1.f - pidx / static_cast<float32>(numSegments));
+ float32 y2Offset = dx * (1.f - (pidx + 1) / static_cast<float32>(numSegments));
+
+ y1 += y1Offset;
+ y2 += y2Offset;
vertices[vidx++].position = Vector2(x1, y1);
vertices[vidx++].position = Vector2(x1, y2);
@@ -188,15 +192,16 @@ namespace Undamped {
shape.load(vertices, numVertices, renderer, GL_DYNAMIC_DRAW);
shape.model = Mat4x4().translateByVec2(Vector2(400, 300));
- weight->shape.model = shape.model.translateByVec2(Vector2(0, -weight->radius));
+ weight->shape.model = shape.model.translateByVec2(Vector2(0, -weight->radius + dx));
}
void Spring::update(float32 dtSeconds) {
- timeElapsed += dtSeconds;
- float32 lastDisplacement = displacement;
- displacement = initialDisplacement * cosf(angularVelocity * timeElapsed - initialVelocity);
- float32 dx = displacement - lastDisplacement;
+ float32 lastPosition = position;
+ force = -k * position;
+ velocity = velocity + (force * dtSeconds) / weight->mass;
+ position = position + velocity * dtSeconds;
+ float32 dx = position - lastPosition;
int32 vidx = 0;
for (int pidx = 0; pidx < numSegments; pidx++) {
float32 y1Offset = dx * (1.f - pidx / static_cast<float32>(numSegments));