diff options
author | mattkae <mattkae@protonmail.com> | 2022-01-22 16:36:33 -0500 |
---|---|---|
committer | mattkae <mattkae@protonmail.com> | 2022-01-22 16:36:33 -0500 |
commit | 8b02f97579728fdbc160095073bf87c88bed3f07 (patch) | |
tree | 2c29d71b61573fdbb3d8cb82e0a91ef67d077387 /2d/softbody/softbody_2 | |
parent | 3f3d1ed1cba61015aa47a6ad9812ae0d30316cc5 (diff) |
Randomly updating the vertices on my rectrangle
Diffstat (limited to '2d/softbody/softbody_2')
-rw-r--r-- | 2d/softbody/softbody_2/SpringRectangle.h | 29 | ||||
-rwxr-xr-x | 2d/softbody/softbody_2/dist/output.wasm | bin | 36383 -> 44516 bytes |
2 files changed, 26 insertions, 3 deletions
diff --git a/2d/softbody/softbody_2/SpringRectangle.h b/2d/softbody/softbody_2/SpringRectangle.h index bc8eed8..47f4aff 100644 --- a/2d/softbody/softbody_2/SpringRectangle.h +++ b/2d/softbody/softbody_2/SpringRectangle.h @@ -4,6 +4,11 @@ struct SoftbodyUpdateVertexData { Vector2 position; + + SoftbodyUpdateVertexData* left; + SoftbodyUpdateVertexData* right; + SoftbodyUpdateVertexData* top; + SoftbodyUpdateVertexData* bottom; }; struct SoftbodyRectangle { @@ -14,17 +19,19 @@ struct SoftbodyRectangle { // Represents the density of springs that will be in the rectangle. int32 springDensity = 10; + float32 totalTimeSeconds = 0.f; SoftbodyUpdateVertexData* updateData = NULL; Mesh2d mesh; Mesh2d pointsMesh; + Vertex2d* vertices = NULL; Vertex2d* pointsVertices = NULL; void load(Renderer2d* renderer) { int32 numVertices = springDensity * springDensity; // Each subdivision is a square. int32 numIndices = 6 * ((springDensity - 1) * (springDensity - 1)); - auto vertices = new Vertex2d[numVertices]; + vertices = new Vertex2d[numVertices]; updateData = new SoftbodyUpdateVertexData[numVertices]; auto indices = new GLuint[numIndices]; @@ -39,6 +46,11 @@ struct SoftbodyRectangle { vertices[vIdx] = { position, Vector4(1, 0, 0, 1) }; updateData[vIdx].position = position; + if (x != 0) updateData[vIdx].left = &updateData[vIdx - 1]; + if (x != springDensity - 1) updateData[vIdx].right = &updateData[vIdx + 1]; + if (y != 0) updateData[vIdx].top = &updateData[vIdx - springDensity]; + if (y != springDensity - 1) updateData[vIdx].bottom = &updateData[vIdx + springDensity]; + if (y != springDensity - 1 && x != springDensity - 1) { indices[iIdx++] = vIdx; indices[iIdx++] = vIdx + 1; @@ -60,16 +72,26 @@ struct SoftbodyRectangle { pointsVertices[v].position = vertices[v].position; pointsVertices[v].color = Vector4(1, 1, 0, 1); } + pointsMesh.model = mesh.model; pointsMesh.load(pointsVertices, numVertices, renderer, GL_DYNAMIC_DRAW); - delete [] vertices; delete [] indices; } void update(float32 dtSeconds) { + totalTimeSeconds += dtSeconds; for (int32 v = 0; v < pointsMesh.numVertices; v++) { - pointsVertices[v].position = Vector2(updateData[v].position.x * width, updateData[v].position.y * height) + position; + float32 r = rand(); + updateData[v].position += (Vector2(cosf(r), sinf(r)) * 0.005f); + + + Vector2 nextPosition = Vector2(updateData[v].position.x, updateData[v].position.y); + + vertices[v].position = nextPosition; + pointsVertices[v].position = nextPosition; } + + mesh.updateVertices(vertices); pointsMesh.updateVertices(pointsVertices); } @@ -81,6 +103,7 @@ struct SoftbodyRectangle { void unload() { mesh.unload(); pointsMesh.unload(); + delete [] vertices; delete [] pointsVertices; } }; diff --git a/2d/softbody/softbody_2/dist/output.wasm b/2d/softbody/softbody_2/dist/output.wasm Binary files differindex 4d1241e..35a32bd 100755 --- a/2d/softbody/softbody_2/dist/output.wasm +++ b/2d/softbody/softbody_2/dist/output.wasm |