strichmaennchen git · main
Entropie festival game; first steps, second try
C 95% GLSL 4.7%git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gzcollission can rotate, but something is off..troll
game.c | 52 +++++----- linalg.c | 27 +++-- linalg.h | 6 +- object.c | 264 ++++++++++++++++++++++++++++-------------------- object.h | 30 +++--- shaders/.basic.frag.swp | Bin 12288 -> 0 bytes world.c | 1 - 7 files changed, 222 insertions(+), 158 deletions(-) diff --git a/game.c b/game.c index ef6ba44..0622f14 100644 --- a/game.c +++ b/game.c @@ -16,25 +16,40 @@ bool Game_init(Game* game) { World_init(&game->world); - Human *player = Human_create(vec3(1,0,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), true, 60); + Human *player = Human_create(vec3(1,4,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), true, 60); player->isPlayer = true; 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,-1,0), vec3(130, 1, 130), 0); - Object* ground_1 = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(1, 1, 1), 0); + Object* ground_1 = Object_create(OBJ_GROUND, CUBE_VERTICES_WARM, vec3(-8,0,1), vec3(1, 1, 1), 20); Object* tree = Object_create(OBJ_TREE, CUBE_VERTICES_WARM, vec3(3,0,2), vec3(0.5f, 10, 0.5f), 0); + World_addHuman(&game->world, player); + World_addHuman(&game->world, human); + World_addHuman(&game->world, human_1); + + World_addObject(&game->world, ground); + World_addObject(&game->world, ground_1); + World_addObject(&game->world, tree); + Object* cylinder = Object_create(OBJ_CYLINDER, CUBE_VERTICES_WARM, vec3(-3,0,1), vec3(1,3,1), 100); + Object_setRotation(cylinder, (Vec3){DEG2RAD(90), 0, 0}); + Object* cylinder_1 = Object_create(OBJ_CYLINDER, CUBE_VERTICES_MODERN, vec3(-3,0,3), vec3(1,3,1), 100); - cylinder->rotation.x = DEG2RAD(90); World_addObject(&game->world, cylinder); World_addObject(&game->world, cylinder_1); + Object* cube = Object_create(OBJ_CUBE, CUBE_VERTICES_MODERN, vec3(2,2,8), vec3(1,3,1), 20); + Object_setRotation(cube, (Vec3){DEG2RAD(90), 0, 0}); + Object* cuber = Object_create(OBJ_CUBE, CUBE_VERTICES_MODERN, vec3(1,0,2), vec3(1,1,1), 20); + World_addObject(&game->world, cube); + World_addObject(&game->world, cuber); + /* Object* cube = Object_create(OBJ_WALL, CUBE_VERTICES_MODERN, vec3(1,0,8), vec3(1,1,1), 20); Object* cube_1 = Object_create(OBJ_WALL, CUBE_VERTICES_MODERN, vec3(3,0,8), vec3(1,1,1), 20); @@ -52,11 +67,6 @@ bool Game_init(Game* game) { World_addObject(&game->world, cube_4); */ - Object* cube = Object_create(OBJ_CUBE, CUBE_VERTICES_MODERN, vec3(2,2,8), vec3(1,3,1), 20); - cube->rotation.x = DEG2RAD(90); - Object* cuber = Object_create(OBJ_CYLINDER, CUBE_VERTICES_MODERN, vec3(1,0,8), vec3(1,1,1), 20); - World_addObject(&game->world, cube); - //World_addObject(&game->world, cuber); // 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)); @@ -79,15 +89,15 @@ bool Game_init(Game* game) { */ Object* house_main = Object_create(OBJ_HOUSE_MAIN, CUBE_VERTICES_WARM, vec3(10,0,20), vec3(18.0f,11.0f,12.0f), 20); - house_main->rotation.y = DEG2RAD(180); + Object_setRotation(house_main, (Vec3){0, DEG2RAD(180), 0}); World_addObject(&game->world, house_main); - Object* house_main_1 = Object_create(OBJ_HOUSE_MAIN, CUBE_VERTICES_WARM, vec3(-5,0,23), vec3(18.0f,11.0f,12.0f), 20); - house_main_1->rotation.y = DEG2RAD(90); + Object* house_main_1 = Object_create(OBJ_HOUSE_MAIN, CUBE_VERTICES_WARM, vec3(-5,0,23), vec3(12.0f,11.0f,18.0f), 20); + Object_setRotation(house_main_1, (Vec3){0, DEG2RAD(90), 0}); World_addObject(&game->world, house_main_1); Object* barn = Object_create(OBJ_BARN, CUBE_VERTICES_WARM, vec3(2,0,-20), vec3(24.0f,11.0f,12.0f), 20); - barn->rotation.y = DEG2RAD(180); + Object_setRotation(barn, (Vec3){0, DEG2RAD(180), 0}); World_addObject(&game->world, barn); Object* house_side_wall_back = Object_create(OBJ_WALL, CUBE_VERTICES_WARM, vec3(27,0,0), vec3(0.5,5,28), 20); @@ -130,24 +140,14 @@ bool Game_init(Game* game) { Object* hof_bar_pillar_top_b = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-16.2,-1.4, -9.4), vec3(0.4f,8.8,0.4f), 100); Object* hof_bar_pillar_top_r = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-13.2,0, -13.8), vec3(0.4f,6.0,0.4f), 100); Object* hof_bar_pillar_top_l = Object_create(OBJ_CYLINDER_STATIC, CUBE_VERTICES_WARM, vec3(-13.2,0, -5), vec3(0.4f,6.0,0.4f), 100); - hof_bar_pillar_top_f->rotation.x = DEG2RAD(90); - hof_bar_pillar_top_b->rotation.x = DEG2RAD(90); - hof_bar_pillar_top_r->rotation.x = DEG2RAD(90); - hof_bar_pillar_top_r->rotation.z = DEG2RAD(90); - hof_bar_pillar_top_l->rotation.x = DEG2RAD(90); - hof_bar_pillar_top_l->rotation.z = DEG2RAD(90); + Object_setRotation(hof_bar_pillar_top_f, (Vec3){DEG2RAD(90), 0, 0}); + Object_setRotation(hof_bar_pillar_top_b, (Vec3){DEG2RAD(90), 0, 0}); + Object_setRotation(hof_bar_pillar_top_r, (Vec3){DEG2RAD(90), 0, DEG2RAD(90)}); + Object_setRotation(hof_bar_pillar_top_l, (Vec3){DEG2RAD(90), 0, DEG2RAD(90)}); World_addObject(&game->world, hof_bar_pillar_top_f); World_addObject(&game->world, hof_bar_pillar_top_b); World_addObject(&game->world, hof_bar_pillar_top_r); World_addObject(&game->world, hof_bar_pillar_top_l); - - World_addHuman(&game->world, player); - World_addHuman(&game->world, human); - World_addHuman(&game->world, human_1); - - World_addObject(&game->world, ground); - World_addObject(&game->world, ground_1); - World_addObject(&game->world, tree); //World_addObject(&game->world, cube_1); //World_addObject(&game->world, Object_create(OBJ_CUBE, CUBE_VERTICES_WARM, vec3(-0.2f,0,2.5f), vec3(1,1,1), 20)); diff --git a/linalg.c b/linalg.c index 31e2e5b..1d69c09 100644 --- a/linalg.c +++ b/linalg.c @@ -87,6 +87,21 @@ Vec3 vec3_cross(Vec3 a, Vec3 b) { }; } +Vec3 vec3_rotate(Vec3 v, Vec3 rot) { + // Apply ZYX rotation (roll, pitch, yaw) + float cx = cosf(rot.x), sx = sinf(rot.x); + float cy = cosf(rot.y), sy = sinf(rot.y); + float cz = cosf(rot.z), sz = sinf(rot.z); + + // Rotation around X + Vec3 rx = {v.x, v.y*cx - v.z*sx, v.y*sx + v.z*cx}; + // Rotation around Y + Vec3 ry = {rx.x*cy + rx.z*sy, rx.y, -rx.x*sy + rx.z*cy}; + // Rotation around Z + Vec3 rz = {ry.x*cz - ry.y*sz, ry.x*sz + ry.y*cz, ry.z}; + return rz; +} + Vec3 vec3_add(Vec3 a, Vec3 b) { return (Vec3){ a.x + b.x, a.y + b.y, a.z + b.z }; } @@ -95,12 +110,12 @@ Vec3 vec3_sub(Vec3 a, Vec3 b) { return (Vec3){a.x - b.x, a.y - b.y, a.z - b.z}; } -Mat4 mat4_scale(Vec3 v) { - Mat4 m = mat4_identity(); - m.m[0] = v.x; - m.m[5] = v.y; - m.m[10] = v.z; - return m; +Vec3 mat4_mul_vec3_dir(Mat4 m, Vec3 v) { + Vec3 r; + r.x = m.m[0] * v.x + m.m[4] * v.y + m.m[8] * v.z; + r.y = m.m[1] * v.x + m.m[5] * v.y + m.m[9] * v.z; + r.z = m.m[2] * v.x + m.m[6] * v.y + m.m[10] * v.z; + return r; } // Placeholder for future perspective/lookAt diff --git a/linalg.h b/linalg.h index af9a0ae..881cce5 100644 --- a/linalg.h +++ b/linalg.h @@ -22,19 +22,21 @@ 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_f(Vec3 v, float s); +Vec3 vec3_div_scalar_f(Vec3 v, float s); Vec3 vec3_negate(Vec3 v); +Vec3 vec3_rotate(Vec3 v, Vec3 rot); Vec4 vec4(float x, float y, float z, float w); Vec4 mat4_mul_vec4(Mat4 m, Vec4 v); Vec4 vec4_sub(Vec4 a, Vec4 b); +Vec3 mat4_mul_vec3_dir(Mat4 m, Vec3 v); + // Matrix helpers Mat4 mat4_identity(); Mat4 mat4_mul(Mat4 a, Mat4 b); Mat4 mat4_inverse(Mat4 m); Mat4 mat4_translate(Vec3 v); -Mat4 mat4_scale(Vec3 v); Vec3 vec3_cross(Vec3 a, Vec3 b); Vec3 vec3_scale(Vec3 v, float s); Mat4 mat4_perspective(float fov_deg, float aspect, float near, float far); diff --git a/object.c b/object.c index d1f5ab1..e54f189 100644 --- a/object.c +++ b/object.c @@ -1,12 +1,14 @@ #include <stdlib.h> #include <stdio.h> #include <math.h> +#include <float.h> double fabs(double x); #include "object.h" #include "linalg.h" +void Collider_update(Collider *c); Object *Object_create(ObjectType type, VertexPreset vertex, Vec3 position, Vec3 size, float weight) { Object *obj = malloc(sizeof(Object)); if (!obj) return NULL; @@ -17,102 +19,89 @@ Object *Object_create(ObjectType type, VertexPreset vertex, Vec3 position, Vec3 obj->vertex = vertex; obj->rotation = (Vec3){0,0,0}; - obj->scale = (Vec3){1,1,1}; obj->mesh = mesh_default; obj->velocity = (Vec3){0,0,0}; obj->angular_velocity = (Vec3){0,0,0}; + obj->collider.half_extents = vec3_scale(size, 0.5); switch (type) { case OBJ_TRIANGLE: obj->mesh = Mesh_createTriangle(); - obj->collider.size = size; obj->collider.isStatic = true; break; case OBJ_AXES: obj->mesh = Mesh_createAxes(200); - obj->collider.size = size; obj->collider.isStatic = true; break; case OBJ_PYRAMID: obj->mesh = Mesh_createPyramid(4.0f, 4.0f); - obj->collider.size = size; obj->collider.isStatic = true; break; case OBJ_CUBE: obj->mesh = Mesh_createCube(size, vertex); - obj->collider.size = size; obj->collider.isStatic = false; break; case OBJ_CYLINDER: obj->mesh = Mesh_createCylinder(size.x, size.y, 30, vertex); - obj->collider.size = size; obj->collider.isStatic = false; break; case OBJ_CYLINDER_STATIC: obj->mesh = Mesh_createCylinder(size.x, size.y, 30, vertex); - obj->collider.size = size; obj->collider.isStatic = true; break; case OBJ_WALL: obj->mesh = Mesh_createCube(size, vertex); - obj->collider.size = size; obj->collider.isStatic = true; break; case OBJ_HUMAN: obj->mesh = Mesh_createHuman(vertex, false); - obj->collider.size = size; obj->collider.isStatic = false; break; case OBJ_HUMAN_PLAYER: obj->mesh = Mesh_createHuman(vertex, true); - obj->collider.size = size; obj->collider.isStatic = false; break; case OBJ_GROUND: obj->mesh = Mesh_createCube(size, vertex); - obj->collider.size = size; obj->collider.isStatic = true; break; case OBJ_TREE: obj->mesh = Mesh_createTree(3.0f, 0.3f, 8, 1.5f); - obj->collider.size = size; obj->collider.isStatic = true; - obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->position_center = (Vec3){0, obj->collider.half_extents.y, 0}; obj->position.y = -size.y/2.0f; obj->collider.position = obj->position_center; return obj; case OBJ_HOUSE_MAIN: obj->mesh = Mesh_createHouse(); - obj->collider.size = size; obj->collider.isStatic = true; - obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->position_center = (Vec3){0, obj->collider.half_extents.y, 0}; obj->position.y = -size.y/2.0f; obj->collider.position = obj->position_center; return obj; case OBJ_BARN: obj->mesh = Mesh_createBarn(); - obj->collider.size = size; obj->collider.isStatic = true; - obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->position_center = (Vec3){0, obj->collider.half_extents.y, 0}; obj->position.y = -size.y/2.0f; obj->collider.position = obj->position_center; return obj; - default: obj->mesh = mesh_default; - obj->collider.size = (Vec3){0,0,0}; + obj->collider.half_extents = (Vec3){0,0,0}; obj->collider.isStatic = true; fprintf(stderr, "Warning: unsupported ObjectType (%d)\n", type); break; } - obj->position_center = (Vec3){0, obj->collider.size.y/2.0f, 0}; + obj->position_center = (Vec3){0, obj->collider.half_extents.y, 0}; obj->collider.position = obj->position_center; + Collider_update(&obj->collider); return obj; } @@ -130,14 +119,13 @@ void Object_render(Object *obj, Renderer *renderer, Camera *camera, Time* time) m.view = camera->view; // Translate by bottom position + half height to center the mesh - Vec3 renderPos = vec3_add(obj->position, (Vec3){0, obj->collider.size.y / 2.0f, 0}); + Vec3 renderPos = vec3_add(obj->position, (Vec3){0, obj->collider.half_extents.y, 0}); m.model = mat4_identity(); m.model = mat4_mul(m.model, mat4_translate(renderPos)); m.model = mat4_mul(m.model, mat4_rotate_x(obj->rotation.x)); m.model = mat4_mul(m.model, mat4_rotate_y(obj->rotation.y)); m.model = mat4_mul(m.model, mat4_rotate_z(obj->rotation.z)); - m.model = mat4_mul(m.model, mat4_scale(obj->scale)); //Mat4 mvp = mat4_mul(m.projection, mat4_mul(m.view, m.model)); @@ -159,23 +147,75 @@ void Object_render(Object *obj, Renderer *renderer, Camera *camera, Time* time) - Vec3 colliderCenterWorld(Object *o) { return vec3_add(o->position, o->collider.position); } +float OBB_project(Collider *c, Vec3 axis) { + // Projection of half-extents along axis + return fabsf(c->half_extents.x * vec3_dot(axis, c->axes[0])) + + fabsf(c->half_extents.y * vec3_dot(axis, c->axes[1])) + + fabsf(c->half_extents.z * vec3_dot(axis, c->axes[2])); +} + +bool OBB_overlap_MTV(Object *o_a, Object *o_b, Vec3 *mtv_axis, float *penetration) { + Vec3 t = vec3_sub(colliderCenterWorld(o_b), colliderCenterWorld(o_a)); + + Collider* a = &o_a->collider; + Collider* b = &o_b->collider; + + Vec3 axesA[3] = {a->axes[0], a->axes[1], a->axes[2]}; + Vec3 axesB[3] = {b->axes[0], b->axes[1], b->axes[2]}; + + Vec3 testAxes[15]; + int idx = 0; + + for (int i = 0; i < 3; i++) testAxes[idx++] = axesA[i]; + for (int i = 0; i < 3; i++) testAxes[idx++] = axesB[i]; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + testAxes[idx++] = vec3_cross(axesA[i], axesB[j]); + + float minPen = FLT_MAX; + Vec3 minAxis = {0,0,0}; + + for (int i = 0; i < 15; i++) { + Vec3 axis = testAxes[i]; + if (vec3_length(axis) < 1e-6f) continue; + axis = vec3_normalize(axis); + + float projA = OBB_project(a, axis); + float projB = OBB_project(b, axis); + float dist = fabsf(vec3_dot(t, axis)); + float pen = projA + projB - dist; + + if (pen < 0) + return false; // separating axis found + + if (pen < minPen) { + minPen = pen; + minAxis = axis; + } + } + + *mtv_axis = minAxis; + *penetration = minPen; + return true; +} + + bool AABB_overlap(Object* o_a, Object* o_b) { + Vec3 n_a = colliderCenterWorld(o_a); Vec3 n_b = colliderCenterWorld(o_b); - Vec3 a = o_a->collider.size; - Vec3 b = o_b->collider.size; + Vec3 a = vec3_scale(o_a->collider.half_extents, 2); + Vec3 b = vec3_scale(o_b->collider.half_extents, 2); return (fabs(n_a.x - n_b.x) * 2 < (a.x + b.x)) && (fabs(n_a.y - n_b.y) * 2 < (a.y + b.y)) && (fabs(n_a.z - n_b.z) * 2 < (a.z + b.z)); } - void Object_applyGravity(Object *obj, float dt) { if (obj->collider.isStatic || obj->is_grounded) return; @@ -183,14 +223,14 @@ void Object_applyGravity(Object *obj, float dt) { } void Collider_positionGround(Object* obj, Object* ground) { - float ground_top = ground->position.y + ground->collider.size.y; + float ground_top = ground->position.y + ground->collider.half_extents.y * 2; float obj_bottom = obj->position.y; // maximum step height: 1/3 of object height float max_step = 100000; if (obj->type == OBJ_HUMAN || obj->type == OBJ_HUMAN_PLAYER) - max_step = obj->collider.size.y / 3.0f; + max_step = obj->collider.half_extents.y*2 / 3.0f; // distance from feet to ground top float height_diff = ground_top - obj_bottom; @@ -210,93 +250,59 @@ void Collider_positionGround(Object* obj, Object* ground) { obj->is_grounded = false; } } -void Collider_positionGround_(Object* obj, Object* ground) { - Vec3 gpos = colliderCenterWorld(ground); - float target_y = gpos.y + ground->collider.size.y / 2.0f; - Vec3 opos = colliderCenterWorld(obj); - float bottom_y = opos.y - obj->collider.size.y / 2.0f; - if (bottom_y < target_y) { float correction = target_y - bottom_y; - obj->position.y += correction; // shift object upward - obj->velocity.y = 0; - } obj->is_grounded = true; -} void Object_resolveDynamicCollision(Object *obj, Object *other) { - if (!AABB_overlap(obj, other)) return; + printf("AA%p %p\n", obj, other); + if (obj->collider.isStatic && other->collider.isStatic) return; - Vec3 posA = colliderCenterWorld(obj); - Vec3 posB = colliderCenterWorld(other); - Vec3 delta = vec3_sub(obj->position, other->position); + printf("BB%p %p\n", obj, other); - delta.y = 0.0f; - if (vec3_length(delta) == 0) { - delta.x = 0.01f; - delta.z = 0.01f; - } + Vec3 mtv_axis; + float penetration; - // 3. Slight separation - float overlapX = (obj->collider.size.x + other->collider.size.x) / 2 - fabs(posA.x - posB.x); - float overlapZ = (obj->collider.size.z + other->collider.size.z) / 2 - fabs(posA.z - posB.z); + // Check for collision and get Minimum Translation Vector (MTV) + if (!OBB_overlap_MTV(obj, other, &mtv_axis, &penetration)) return; - Vec3 normal = {0,0,0}; - if (overlapX < overlapZ) { - normal.x = (posA.x > posB.x) ? 1.0f : -1.0f; // push left or right - } else { - normal.z = (posA.z > posB.z) ? 1.0f : -1.0f; // push forward or back + // Ensure MTV points from obj to other + Vec3 dir = vec3_sub(colliderCenterWorld(other), colliderCenterWorld(obj)); + if (vec3_dot(dir, mtv_axis) < 0) { + mtv_axis = vec3_scale(mtv_axis, -1.0f); } - // 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); - - - - // 4. Compute offset from obj center to other center in XZ plane - Vec3 offset = vec3_sub(posB, posA); - offset.y = 0.0f; + float pushFactorObj = other->collider.isStatic ? 1.0f : other->weight / totalWeight; + float pushFactorOther = obj->collider.isStatic ? 1.0f : obj->weight / totalWeight; - // 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; + // 1. Separate objects along MTV + if (!obj->collider.isStatic) + obj->position = vec3_sub(obj->position, vec3_scale(mtv_axis, penetration * pushFactorObj)); + if (!other->collider.isStatic) + other->position = vec3_add(other->position, vec3_scale(mtv_axis, penetration * pushFactorOther)); - // 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; - - if (!obj->collider.isStatic) { - obj->velocity = vec3_add(obj->velocity, impulseObj); - if (overlapX > 0) - obj->position.x += normal.x * (overlapX * 0.5f); - if (overlapZ > 0) - obj->position.z += normal.z * (overlapZ * 0.5f); - obj->angular_velocity.y += normalizedTorque * vec3_length(impulseObj) / obj->weight * angularFactor; - } - if (!other->collider.isStatic) { - other->velocity = vec3_add(other->velocity, vec3_scale(impulseOther, -1.0f)); - if (overlapX > 0) - other->position.x -= normal.x * (overlapX * 0.5f); - if (overlapZ > 0) - other->position.z -= normal.z * (overlapZ * 0.5f); - other->angular_velocity.y += normalizedTorque * vec3_length(impulseOther) / other->weight * angularFactor; - } + // 2. Apply velocity impulses along collision normal, only horizontal if static + float pushStrength = 5.0f; + Vec3 horizontalAxis = {mtv_axis.x, 0, mtv_axis.z}; + if (vec3_length(horizontalAxis) > 1e-6f) + horizontalAxis = vec3_normalize(horizontalAxis); + else + horizontalAxis = (Vec3){0,0,0}; // no horizontal push + + if (!obj->collider.isStatic) + obj->velocity = vec3_sub(obj->velocity, vec3_scale(horizontalAxis, pushStrength * pushFactorObj)); + if (!other->collider.isStatic) + other->velocity = vec3_add(other->velocity, vec3_scale(horizontalAxis, pushStrength * pushFactorOther)); + + // 3. Apply angular impulse (torque) around Y axis for feedback + Vec3 offset = vec3_sub(colliderCenterWorld(other), colliderCenterWorld(obj)); + offset.y = 0; // only lateral torque for now + + float maxOffset = fmaxf(obj->collider.half_extents.x, obj->collider.half_extents.z) * 0.5f; + float torque = fmaxf(-1.0f, fminf(1.0f, offset.x / maxOffset)); + + if (!obj->collider.isStatic) + obj->angular_velocity.y += torque * pushStrength / obj->weight; + if (!other->collider.isStatic) + other->angular_velocity.y -= torque * pushStrength / other->weight; } void Object_resolveCollisions(Object *obj, Object **worldObjects, int count) { @@ -329,11 +335,54 @@ void Object_applyFriction(Object *obj, float dt) { if (fabs(obj->velocity.z) < 0.01f) obj->velocity.z = 0; } +void Collider_update(Collider *c) { + // Build rotation matrix from Euler angles + Mat4 rot = mat4_identity(); + rot = mat4_mul(rot, mat4_rotate_x(c->rotation.x)); + rot = mat4_mul(rot, mat4_rotate_y(c->rotation.y)); + rot = mat4_mul(rot, mat4_rotate_z(c->rotation.z)); + + // Local basis + Vec3 x = {1,0,0}; + Vec3 y = {0,1,0}; + Vec3 z = {0,0,1}; + + // Transform into world space + c->axes[0] = mat4_mul_vec3_dir(rot, x); + c->axes[1] = mat4_mul_vec3_dir(rot, y); + c->axes[2] = mat4_mul_vec3_dir(rot, z); + + // Normalize (in case scaling sneaks in later) + c->axes[0] = vec3_normalize(c->axes[0]); + c->axes[1] = vec3_normalize(c->axes[1]); + c->axes[2] = vec3_normalize(c->axes[2]); +} + +void Object_setRotation(Object *obj, Vec3 rot) { + obj->rotation = rot; + obj->collider.rotation = rot; + Collider_update(&obj->collider); +} + +void Object_rotate(Object *obj, Vec3 delta) { + obj->rotation = vec3_add(obj->rotation, delta); + obj->collider.rotation = obj->rotation; // keep collider in sync + Collider_update(&obj->collider); // update axes +} + void Object_update(Object *obj, Object **worldObjects, int count, float dt) { if (obj->collider.isStatic) return; Object_applyGravity(obj, dt); + // Apply angular velocity and update rotation & collider + Vec3 delta_rot = vec3_scale(obj->angular_velocity, dt); + Object_rotate(obj, delta_rot); + obj->angular_velocity.y *= 0.9f; // damping yaw + + // Update collider axes after rotation + Collider_update(&obj->collider); + Vec3 total = vec3_add(obj->movement, obj->velocity); if (vec3_length(total) > 0.0f) { obj->is_grounded = false; @@ -342,13 +391,6 @@ 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 14db597..1c032e8 100644 --- a/object.h +++ b/object.h @@ -7,9 +7,11 @@ #include "camera.h" typedef struct { - Vec3 position; - Vec3 size; // e.g. bounding box extents - bool isStatic; // ground, walls, etc. + Vec3 position; // world-space center of the collider + Vec3 half_extents; // half-width, half-height, half-depth (OBB) + Vec3 rotation; // orientation (Euler or quaternion) + Vec3 axes[3]; // cached orthonormal basis vectors (X, Y, Z of the OBB) + bool isStatic; } Collider; typedef enum { @@ -32,18 +34,19 @@ typedef enum { typedef struct { ObjectType type; VertexPreset vertex; - Vec3 position; // feet - Vec3 position_center; + Vec3 position; // object feet/base + Vec3 position_center; // center offset (mesh alignment) Vec3 rotation; - Vec3 scale; Mesh mesh; Vec3 color; - Collider collider; - Vec3 velocity; // for gravity/getting pushed/throwing - Vec3 angular_velocity; // for gravity/getting pushed/throwing - Vec3 movement; // for movement + + Collider collider; // attached collider + + Vec3 velocity; + Vec3 angular_velocity; + Vec3 movement; bool is_grounded; - float weight; // in kilogramm + float weight; } Object; @@ -53,9 +56,12 @@ void Object_destroy(Object* obj); bool AABB_overlap(Object* o_a, Object* o_b); void Collider_positionGround(Object* obj, Object* ground); +void Object_setRotation(Object *obj, Vec3 rot); +void Object_rotate(Object *obj, Vec3 delta); void Object_applyGravity(Object *obj, float dt); void Object_resolveCollisions(Object *obj, Object **worldObjects, int count); void Object_update(Object *obj, Object **worldObjects, int count, float dt); void Object_addMovement(Object *obj, Vec3 move); - +void Object_setRotation(Object *obj, Vec3 rotation); +void Object_rotate(Object *obj, Vec3 delta); #endif /* GAMEOBJECT_H */ diff --git a/shaders/.basic.frag.swp b/shaders/.basic.frag.swp deleted file mode 100644 index 61ef35c..0000000 Binary files a/shaders/.basic.frag.swp and /dev/null differ diff --git a/world.c b/world.c index 6d4c096..fcbafb7 100644 --- a/world.c +++ b/world.c @@ -94,7 +94,6 @@ void World_renderShadows(World* world, Renderer* renderer, Time* time) { model = mat4_mul(model, mat4_rotate_x(obj->rotation.x)); model = mat4_mul(model, mat4_rotate_y(obj->rotation.y)); model = mat4_mul(model, mat4_rotate_z(obj->rotation.z)); - model = mat4_mul(model, mat4_scale(obj->scale)); Mat4 lightSpace = mat4_mul(renderer->lightProjection, mat4_mul(renderer->lightView, model)); GLint loc = glGetUniformLocation(renderer->shadowShader, "lightSpaceMatrix");