From d38e35b0929c783596d674746c544e6b46dceab8 Mon Sep 17 00:00:00 2001
From: nemo <n3m0o7@hotmail.com>
Date: Wed, 5 Feb 2025 15:41:06 +0100
Subject: [PATCH] begin rop

---
 06-rope/.gitignore |  1 +
 06-rope/Makefile   |  8 ++++++
 06-rope/main.c     | 62 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
 create mode 100644 06-rope/.gitignore
 create mode 100644 06-rope/Makefile
 create mode 100644 06-rope/main.c

diff --git a/06-rope/.gitignore b/06-rope/.gitignore
new file mode 100644
index 0000000..649b5f0
--- /dev/null
+++ b/06-rope/.gitignore
@@ -0,0 +1 @@
+rope
diff --git a/06-rope/Makefile b/06-rope/Makefile
new file mode 100644
index 0000000..e776ace
--- /dev/null
+++ b/06-rope/Makefile
@@ -0,0 +1,8 @@
+all:
+	gcc main.c -lraylib -o rope
+
+run: all
+	./rope
+
+clean:
+	rm rope
diff --git a/06-rope/main.c b/06-rope/main.c
new file mode 100644
index 0000000..7d46a30
--- /dev/null
+++ b/06-rope/main.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+
+#include "raylib.h"
+#include "raymath.h"
+
+#define ROPE_LEN 20
+#define ROPE_WIDTH 10
+
+#define NB_ROPE 50
+Vector2 ropes[NB_ROPE];
+Vector2 forces[NB_ROPE];
+
+int main()
+{
+    InitWindow(1920, 1080, "speedrun");
+    SetWindowState(FLAG_FULLSCREEN_MODE|FLAG_WINDOW_RESIZABLE);
+    SetTargetFPS(60);
+
+    while (!WindowShouldClose()) {
+        BeginDrawing();
+        {
+            ClearBackground(GRAY);
+
+            ropes[0] = GetMousePosition();
+            DrawCircleV(ropes[0], 5, YELLOW);
+
+            for (int i = 1; i < NB_ROPE; i++) {
+                Vector2 old = ropes[i];
+
+                if (!IsKeyDown(KEY_SPACE))
+                    forces[i].y += 9.8*100*GetFrameTime();
+
+                ropes[i] = Vector2Add(ropes[i], forces[i]);
+
+                float dist = Vector2Distance(ropes[i-1], ropes[i]);
+                if (!dist) dist = ROPE_LEN;
+                ropes[i] = Vector2Lerp(ropes[i-1], ropes[i], ROPE_LEN/dist);
+
+                Vector2 snap = Vector2Subtract(old, ropes[i]);
+                snap = Vector2Negate(snap);
+                snap = Vector2Scale(snap, GetFrameTime()*37);
+
+                forces[i] = snap;
+
+                DrawCircleV(ropes[i], ROPE_WIDTH/2, YELLOW);
+                DrawLineEx(ropes[i-1], ropes[i], ROPE_WIDTH, YELLOW);
+
+                // DEBUG:
+                // DrawLineEx(old, ropes[i], ROPE_WIDTH, RED);
+                // DrawText(TextFormat("%.3f %.3f", forces[i].x, forces[i].y), 0, (i-1)*25, 25, ORANGE);
+            }
+
+            DrawText("Mode: ", 0, 1080-25, 25, ORANGE);
+            if (IsKeyDown(KEY_SPACE)) {
+                DrawText("SPACE", MeasureText("Mode: ", 25), 1080-25, 25, DARKBLUE);
+            } else {
+                DrawText("GRAVITY", MeasureText("Mode: ", 25), 1080-25, 25, BROWN);
+            }
+        }
+        EndDrawing();
+    }
+}