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.gzfixed collision with pushing rotation!
game.c | 2 +- human.c | 5 +- object.c | 185 +++++++++++++++++++++++++++++++++++++++++---------------------- 3 files changed, 125 insertions(+), 67 deletions(-) diff --git a/game.c b/game.c index 0622f14..4c6aca6 100644 --- a/game.c +++ b/game.c @@ -46,7 +46,7 @@ bool Game_init(Game* game) { 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); + Object* cuber = Object_create(OBJ_CUBE, CUBE_VERTICES_MODERN, vec3(1,0,2), vec3(2,1,2), 20); World_addObject(&game->world, cube); World_addObject(&game->world, cuber); diff --git a/human.c b/human.c index 8acd728..1e213d7 100644 --- a/human.c +++ b/human.c @@ -40,7 +40,7 @@ void Player_update(Human *player, Userinput *input, Camera *cam) { // Base speed float baseSpeed = 5.0f; - float sprintSpeed = 10.0f; + float sprintSpeed = 20.0f; float moveSpeed = input->sprint ? sprintSpeed : baseSpeed; // Build movement vector @@ -59,7 +59,8 @@ void Player_update(Human *player, Userinput *input, Camera *cam) { // Jump if (input->jump > 0 && player->object->is_grounded) { - float jump_vel = input->jump == 1 ? 5.0 : 8.0; + //float jump_vel = input->jump == 1 ? 5.0 : 8.0; + float jump_vel = input->jump == 1 ? 5.0 : 10.0; player->object->velocity.y = jump_vel; // tweak jump strength player->object->is_grounded = false; input->jump = 0; diff --git a/object.c b/object.c index e54f189..57c1f97 100644 --- a/object.c +++ b/object.c @@ -123,8 +123,12 @@ void Object_render(Object *obj, Renderer *renderer, Camera *camera, Time* time) 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)); + + // yaw around global Y (physics) m.model = mat4_mul(m.model, mat4_rotate_y(obj->rotation.y)); + + // then apply local tilt (visual placement) + m.model = mat4_mul(m.model, mat4_rotate_x(obj->rotation.x)); m.model = mat4_mul(m.model, mat4_rotate_z(obj->rotation.z)); //Mat4 mvp = mat4_mul(m.projection, mat4_mul(m.view, m.model)); @@ -204,18 +208,6 @@ bool OBB_overlap_MTV(Object *o_a, Object *o_b, Vec3 *mtv_axis, float *penetratio } -bool AABB_overlap(Object* o_a, Object* o_b) { - - Vec3 n_a = colliderCenterWorld(o_a); - Vec3 n_b = colliderCenterWorld(o_b); - 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; @@ -223,86 +215,137 @@ void Object_applyGravity(Object *obj, float dt) { } void Collider_positionGround(Object* obj, Object* ground) { - 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.half_extents.y*2 / 3.0f; - - // distance from feet to ground top - float height_diff = ground_top - obj_bottom; - - if (height_diff > 0 && height_diff <= max_step) { - // within step height → snap onto surface - obj->position.y += height_diff; - obj->velocity.y = 0; - obj->is_grounded = true; - } else if (height_diff <= 0) { - // object is already intersecting / below → push out - obj->position.y += height_diff; - obj->velocity.y = 0; - obj->is_grounded = true; - } else { - // too high → treat as wall, don't snap - obj->is_grounded = false; + if (!ground->collider.isStatic) return; + + Collider* g = &ground->collider; + Collider* o = &obj->collider; + + // Ground's Y-axis in world space + Vec3 up = g->axes[1]; // assuming Y is "up" + + // Vector from obj center to ground center + Vec3 delta = vec3_sub(colliderCenterWorld(obj), colliderCenterWorld(ground)); + + // Project delta onto ground up-axis + float distAlongUp = vec3_dot(delta, up); + + // Compute half extents along up-axis + float objHalf = OBB_project(o, up); + float groundHalf = OBB_project(g, up); + + // penetration = distance we need to move obj down to sit on top + float penetration = (distAlongUp - (groundHalf + objHalf)); + + // Only snap if penetrating (obj is above ground) + if (penetration < 0) { + // maximum step height: 1/3 of object height + float max_step = obj->collider.half_extents.y*2/3.0f; + if (obj->type != OBJ_HUMAN && obj->type != OBJ_HUMAN_PLAYER) + max_step = 1e6f; // effectively unlimited for other objects + + if (-penetration <= max_step) { + // move object along negative up-axis to sit on top + obj->position = vec3_add(obj->position, vec3_scale(up, -penetration)); + obj->velocity.y = 0; + obj->is_grounded = true; + } else { + obj->is_grounded = false; + } } } void Object_resolveDynamicCollision(Object *obj, Object *other) { - printf("AA%p %p\n", obj, other); if (obj->collider.isStatic && other->collider.isStatic) return; - printf("BB%p %p\n", obj, other); - Vec3 mtv_axis; float penetration; - // Check for collision and get Minimum Translation Vector (MTV) if (!OBB_overlap_MTV(obj, other, &mtv_axis, &penetration)) return; - // Ensure MTV points from obj to other + // Ensure MTV points from obj -> other Vec3 dir = vec3_sub(colliderCenterWorld(other), colliderCenterWorld(obj)); - if (vec3_dot(dir, mtv_axis) < 0) { - mtv_axis = vec3_scale(mtv_axis, -1.0f); - } + if (vec3_dot(dir, mtv_axis) < 0) mtv_axis = vec3_scale(mtv_axis, -1.0f); + + // Determine whether the collision is mainly vertical (landing) + const Vec3 world_up = {0,1,0}; + float verticalAlignment = fabsf(vec3_dot(mtv_axis, world_up)); // 1.0 == straight up/down + // compute base push factors float totalWeight = obj->weight + other->weight; float pushFactorObj = other->collider.isStatic ? 1.0f : other->weight / totalWeight; float pushFactorOther = obj->collider.isStatic ? 1.0f : obj->weight / totalWeight; - // 1. Separate objects along MTV + // --- Special handling for mostly-vertical collisions (landings) --- + if (verticalAlignment > 0.70f) { + // If other is static, snap obj to it and stop vertical movement + if (other->collider.isStatic) { + // Use your ground snap routine which uses OBB math + Collider_positionGround(obj, other); + // ensure no vertical impulse + obj->velocity.y = 0.0f; + obj->is_grounded = true; + // do not apply horizontal impulse if tiny + return; // already resolved vertical landing; optional: still apply horizontal push below if wanted + } else { + // Both dynamic: + // Prefer moving the incoming (obj) up rather than pushing other downward into the ground. + // Make obj take most of the separation. + pushFactorObj = 1.0f; + pushFactorOther = 0.0f; + + // Stop vertical velocity of obj so it doesn't bounce + obj->velocity.y = 0.0f; + obj->is_grounded = true; + // We will continue to apply horizontal impulse and torque below. + } + } + + // --- 1. Separate objects along MTV (respect push factors) --- 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)); - // 2. Apply velocity impulses along collision normal, only horizontal if static + // After separation: avoid leaving 'other' intersecting the ground. + // If other moved and could be intersecting a static collider below it, optionally fix it: + // (simple protective clamp) — compute other's bottom world Y and ensure it isn't below any static ground + // (You may implement a broader world-ground-check later if needed.) + + // --- 2. Apply horizontal velocity impulses only --- float pushStrength = 5.0f; - Vec3 horizontalAxis = {mtv_axis.x, 0, mtv_axis.z}; + // horizontal component of MTV (world-space) + Vec3 horizontalAxis = (Vec3){ mtv_axis.x, 0.0f, mtv_axis.z }; if (vec3_length(horizontalAxis) > 1e-6f) horizontalAxis = vec3_normalize(horizontalAxis); else - horizontalAxis = (Vec3){0,0,0}; // no horizontal push + horizontalAxis = (Vec3){0,0,0}; + // Apply impulse only along horizontalAxis so we don't reintroduce vertical bounce 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 + // --- 3. Angular impulse using torque = r × F --- Vec3 offset = vec3_sub(colliderCenterWorld(other), colliderCenterWorld(obj)); - offset.y = 0; // only lateral torque for now + offset.y = 0.0f; // we only care about horizontal offset + + Vec3 impulse = vec3_scale(horizontalAxis, pushStrength); // collision push direction - 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)); + // Torque (around Y) = r.x * F.z - r.z * F.x + float torque = -(offset.x * impulse.z - offset.z * impulse.x); if (!obj->collider.isStatic) - obj->angular_velocity.y += torque * pushStrength / obj->weight; + obj->angular_velocity.y += torque / (obj->weight > 0 ? obj->weight : 1.0f); + + // Now same for the other object (opposite offset, opposite force) + Vec3 offsetOther = vec3_scale(offset, -1.0f); + Vec3 impulseOther = vec3_scale(horizontalAxis, -pushStrength); + + float otherTorque = -(offsetOther.x * impulseOther.z - offsetOther.z * impulseOther.x); + if (!other->collider.isStatic) - other->angular_velocity.y -= torque * pushStrength / other->weight; + other->angular_velocity.y += otherTorque / (other->weight > 0 ? other->weight : 1.0f); } void Object_resolveCollisions(Object *obj, Object **worldObjects, int count) { @@ -310,9 +353,22 @@ void Object_resolveCollisions(Object *obj, Object **worldObjects, int count) { Object *other = worldObjects[j]; if (other == obj) continue; - if (AABB_overlap(obj, other)) { - Collider_positionGround(obj, other); + // Only snap to static ground objects + if (other->collider.isStatic) { + Vec3 mtv_axis; + float penetration; + + if (OBB_overlap_MTV(obj, other, &mtv_axis, &penetration)) { + // Check if collision is mainly vertical (i.e., obj landed on top) + float verticalAlignment = fabsf(vec3_dot(mtv_axis, other->collider.axes[1])); + + if (verticalAlignment > 0.7f) { // mostly vertical collision + Collider_positionGround(obj, other); + } + } } + + // Resolve dynamic collisions for everything Object_resolveDynamicCollision(obj, other); } } @@ -338,8 +394,8 @@ void Object_applyFriction(Object *obj, float dt) { 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_x(c->rotation.x)); rot = mat4_mul(rot, mat4_rotate_z(c->rotation.z)); // Local basis @@ -375,12 +431,13 @@ void Object_update(Object *obj, Object **worldObjects, int count, float dt) { 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 + // --- integrate physics yaw only (prevent physics from changing visual tilt) --- + float yaw_delta = obj->angular_velocity.y * dt; + obj->rotation.y += yaw_delta; // only affect yaw from physics + obj->angular_velocity.y *= 0.9f; // damping for yaw - // Update collider axes after rotation + // keep collider orientation in sync with current rotation + obj->collider.rotation = obj->rotation; Collider_update(&obj->collider); Vec3 total = vec3_add(obj->movement, obj->velocity);