Nimbin[12]?Games / strichmaennchen / commits / b3e509a

strichmaennchen git · main

Entropie festival game; first steps, second try

opengl glsl c 3d game entropie · first commit 2025-09-16 · last commit 2025-10-05 (1 year ago) · synced 1 hour ago

C 95% GLSL 4.7%
git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gz

rotate if pushed

Christian Immanuel · 2025-09-27 11:58 · b3e509a7bd426a7288fbd77d69c9826948d279cb

 game.c   |  8 ++++----
 linalg.c |  9 +++++++++
 linalg.h |  4 +++-
 object.c | 50 +++++++++++++++++++++++++++++++++++++++++---------
 object.h |  1 +
 5 files changed, 58 insertions(+), 14 deletions(-)

diff --git a/game.c b/game.c
index c21ab8f..f7a8898 100644
--- a/game.c
+++ b/game.c
@@ -8,7 +8,7 @@
 
 bool Game_init(Game* game) {
     //game->time = Time_init(3600.0f);
-    game->time = Time_init(3600.0f);
+    game->time = Time_init(3.0f);
 
     World_init(&game->world);
 
@@ -17,9 +17,9 @@ bool Game_init(Game* game) {
     Human  *human      = Human_create(vec3(0,4,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60);
     Human  *human_1    = Human_create(vec3(-1,2,1), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60);
 
-    Object* ground     = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(10, 1, 10), 0);
+    Object* ground     = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(30, 1, 30), 0);
     Object* tree       = Object_create(OBJ_TREE,   CUBE_VERTICES_WARM,   vec3(3,0,2), vec3(1, 1, 1), 0);
-    Object* cube       = Object_create(OBJ_CUBE,   CUBE_VERTICES_WARM,   vec3(-0.2f,0,1.5f), vec3(1,10,1), 20);
+    Object* cube       = Object_create(OBJ_CUBE,   CUBE_VERTICES_WARM,   vec3(-0.2f,0,1.5f), vec3(2,1,2), 20);
 
     // for (int j = 0; j<10; j++) for (int i = 0; i<10; i++) World_addHuman(&game->world, Human_create(vec3(i,5,j), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60));
 
@@ -49,7 +49,7 @@ bool Game_init(Game* game) {
     World_addObject(&game->world, ground);
     World_addObject(&game->world, tree);
     World_addObject(&game->world, cube);
-    World_addObject(&game->world, Object_create(OBJ_CUBE,   CUBE_VERTICES_WARM,   vec3(-0.2f,0,2.5f), vec3(1,1,1), 20));
+    //World_addObject(&game->world, Object_create(OBJ_CUBE,   CUBE_VERTICES_WARM,   vec3(-0.2f,0,2.5f), vec3(1,1,1), 20));
 
     //World_addObject(&game->world, axes);
     /*
diff --git a/linalg.c b/linalg.c
index 32c7f19..31e2e5b 100644
--- a/linalg.c
+++ b/linalg.c
@@ -33,6 +33,10 @@ Vec3 vec3(float x, float y, float z) {
     return v;
 }
 
+Vec3 vec3_negate(Vec3 v) {
+    return (Vec3){ -v.x, -v.y, -v.z };
+}
+
 float vec3_dot(Vec3 a, Vec3 b) {
     return a.x*b.x + a.y*b.y + a.z*b.z;
 }
@@ -70,6 +74,11 @@ Vec3 vec3_div_scalar(Vec4 v) {
     return r;
 }
 
+
+Vec3 vec3_div_scalar_f(Vec3 v, float s) {
+    return (Vec3){v.x / s, v.y / s, v.z / s};
+}
+
 Vec3 vec3_cross(Vec3 a, Vec3 b) {
     return (Vec3){
         a.y*b.z - a.z*b.y,
diff --git a/linalg.h b/linalg.h
index 4b38cbf..af9a0ae 100644
--- a/linalg.h
+++ b/linalg.h
@@ -21,7 +21,9 @@ Vec3  vec3_normalize(Vec3 v);
 float vec3_length(Vec3 v);
 Vec3  vec3_add(Vec3 a, Vec3 b);
 Vec3  vec3_sub(Vec3 a, Vec3 b);
-Vec3 vec3_div_scalar(Vec4 v);
+Vec3  vec3_div_scalar(Vec4 v);
+Vec3 vec3_div_scalar_f(Vec3 v, float s);
+Vec3  vec3_negate(Vec3 v);
 
 Vec4 vec4(float x, float y, float z, float w);
 Vec4 mat4_mul_vec4(Mat4 m, Vec4 v);
diff --git a/object.c b/object.c
index 27cc79f..6a6497b 100644
--- a/object.c
+++ b/object.c
@@ -20,6 +20,7 @@ Object *Object_create(ObjectType type, VertexPreset vertex, Vec3 position, Vec3
     obj->scale    = (Vec3){1,1,1};
     obj->mesh     = mesh_default;
     obj->velocity = (Vec3){0,0,0};
+    obj->angular_velocity = (Vec3){0,0,0};
 
     switch (type) {
         case OBJ_TRIANGLE:
@@ -156,30 +157,28 @@ void Object_applyGravity(Object *obj, float dt) {
 
 void Object_resolveDynamicCollision(Object *obj, Object *other) {
     if (obj->collider.isStatic || other->collider.isStatic) return;
-
     if (!AABB_overlap(obj, other)) return;
 
-    // 1. Compute the push direction in XZ plane (ignore Y for now)
+    // 1. Horizontal push direction
     Vec3 delta = vec3_sub(obj->position, other->position);
-    delta.y = 0.0f; // horizontal push only
+    delta.y = 0.0f;
     if (vec3_length(delta) == 0) {
-        delta.x = 0.01f; // avoid zero-length vector
+        delta.x = 0.01f;
         delta.z = 0.01f;
     }
     Vec3 normal = vec3_normalize(delta);
 
-    // 2. Compute weight-based impulse
-    float totalWeight  = obj->weight + other->weight;
-    float pushStrength = 5.0f; // tweak this to control push force
+    // 2. Weight-based impulse
+    float totalWeight = obj->weight + other->weight;
+    float pushStrength = 5.0f;
 
     Vec3 impulseObj   = vec3_scale(normal, other->weight / totalWeight * pushStrength);
     Vec3 impulseOther = vec3_scale(normal, obj->weight / totalWeight * pushStrength);
 
-    // 3. Apply impulses to velocity
     obj->velocity   = vec3_add(obj->velocity, impulseObj);
     other->velocity = vec3_add(other->velocity, vec3_scale(impulseOther, -1.0f));
 
-    // 4. Optional: separate objects slightly to prevent overlap
+    // 3. Slight separation
     float overlapX = (obj->collider.size.x + other->collider.size.x)/2 - fabs(obj->position.x - other->position.x);
     float overlapZ = (obj->collider.size.z + other->collider.size.z)/2 - fabs(obj->position.z - other->position.z);
 
@@ -191,6 +190,33 @@ void Object_resolveDynamicCollision(Object *obj, Object *other) {
         obj->position.z += normal.z * (overlapZ * 0.5f);
         other->position.z -= normal.z * (overlapZ * 0.5f);
     }
+
+    // 4. Compute offset from obj center to other center in XZ plane
+    Vec3 offset = vec3_sub(other->position, obj->position);
+    offset.y = 0.0f;
+
+    // 5. Transform offset into obj's local space using rotation.y (yaw)
+    float cosYaw = cosf(-obj->rotation.y);
+    float sinYaw = sinf(-obj->rotation.y);
+    float localX = offset.x * cosYaw - offset.z * sinYaw;
+
+    // 6. Torque based on lateral local offset
+    float maxOffset = fmaxf(obj->collider.size.x, obj->collider.size.z) * 0.5f;
+    float normalizedTorque = localX / maxOffset;
+
+    if (fabs(normalizedTorque) < 0.1f)
+        normalizedTorque = 0.0f;
+
+    // Print left/right
+    if (normalizedTorque > 0.0f)
+        printf("Object collided on the right side\n");
+    else if (normalizedTorque < 0.0f)
+        printf("Object collided on the left side\n");
+
+    // 7. Apply angular velocity
+    float angularFactor = 1.0f;
+    obj->angular_velocity.y   += normalizedTorque * vec3_length(impulseObj)   / obj->weight * angularFactor;
+    other->angular_velocity.y += normalizedTorque * vec3_length(impulseOther) / other->weight * angularFactor;
 }
 
 void Object_applyFriction(Object *obj, float dt) {
@@ -240,6 +266,12 @@ void Object_update(Object *obj, Object **worldObjects, int count, float dt) {
 
     Object_applyFriction(obj, dt);
 
+    // Apply angular velocity to rotation
+    obj->rotation = vec3_add(obj->rotation, vec3_scale(obj->angular_velocity, dt));
+
+    // Dampen angular velocity (simulate rotational friction)
+    obj->rotation.y += obj->angular_velocity.y * dt;
+    obj->angular_velocity.y *= 0.9f;
     obj->movement = (Vec3){0,0,0};
 }
 
diff --git a/object.h b/object.h
index 2e45434..bfad1bc 100644
--- a/object.h
+++ b/object.h
@@ -36,6 +36,7 @@ typedef struct {
     Vec3 color;
     Collider collider;
     Vec3 velocity; // for gravity/getting pushed/throwing
+    Vec3 angular_velocity; // for gravity/getting pushed/throwing
     Vec3 movement; // for movement
     bool is_grounded;
     float weight; // in kilogramm