From 8b02f97579728fdbc160095073bf87c88bed3f07 Mon Sep 17 00:00:00 2001 From: mattkae Date: Sat, 22 Jan 2022 16:36:33 -0500 Subject: Randomly updating the vertices on my rectrangle --- 2d/softbody/softbody_2/SpringRectangle.h | 29 ++++++++++++++++++++++++++--- 2d/softbody/softbody_2/dist/output.wasm | Bin 36383 -> 44516 bytes 2 files changed, 26 insertions(+), 3 deletions(-) (limited to '2d') 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 index 4d1241e..35a32bd 100755 Binary files a/2d/softbody/softbody_2/dist/output.wasm and b/2d/softbody/softbody_2/dist/output.wasm differ -- cgit v1.2.1