summaryrefslogtreecommitdiff
path: root/2d/softbody/softbody_2/SpringRectangle.h
diff options
context:
space:
mode:
Diffstat (limited to '2d/softbody/softbody_2/SpringRectangle.h')
-rw-r--r--2d/softbody/softbody_2/SpringRectangle.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/2d/softbody/softbody_2/SpringRectangle.h b/2d/softbody/softbody_2/SpringRectangle.h
new file mode 100644
index 0000000..b45bd3c
--- /dev/null
+++ b/2d/softbody/softbody_2/SpringRectangle.h
@@ -0,0 +1,63 @@
+#include "../../../shared_cpp/Renderer2d.h"
+#include "../../../shared_cpp/types.h"
+#include "../../../shared_cpp/mathlib.h"
+
+
+struct SoftbodyRectangle {
+ float32 width = 100;
+ float32 height = 100;
+
+ // Represents the density of springs that will be in the rectangle.
+ int32 springDensity = 10;
+
+ int32 numSubdivisions = 0;
+
+ Mesh2d mesh;
+
+ 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];
+ auto indices = new GLuint[numIndices];
+
+ int32 vIdx = 0;
+ int32 iIdx = 0;
+ float32 inverseDensity = 1.f / springDensity;
+ float32 halfInv = inverseDensity - (inverseDensity / 2.f);
+ for (int32 y = 0; y < springDensity; y++) { // Rows
+ for (int32 x = 0; x < springDensity; x++) { // Columns
+ Vector2 position = Vector2(x * inverseDensity - halfInv, y * inverseDensity- halfInv);
+ vertices[vIdx] = { position, Vector4(1, 0, 0, 1) };
+
+ if (y != springDensity - 1 && x != springDensity - 1) {
+ indices[iIdx++] = vIdx;
+ indices[iIdx++] = vIdx + 1;
+ indices[iIdx++] = vIdx + springDensity;
+ indices[iIdx++] = vIdx + springDensity;
+ indices[iIdx++] = vIdx + springDensity + 1;
+ indices[iIdx++] = vIdx + 1;
+ }
+
+ vIdx++;
+ }
+ }
+
+ mesh.load(vertices, numVertices, indices, numIndices, renderer);
+ mesh.model = Mat4x4().scale(Vector3(width, height, 0)).translateByVec2(Vector2(300, 400));
+
+ delete [] vertices;
+ delete [] indices;
+ }
+
+ void update(float32 dtSeconds) {
+
+ }
+
+ void render(Renderer2d* renderer) {
+ mesh.render(renderer);
+ }
+
+ void unload() {
+ mesh.unload();
+ }
+};