From eb73783580ad8edf0b796b7d88fb5447b62f1f14 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sat, 22 Jan 2022 16:10:03 -0700 Subject: [PATCH] Make wire physics better --- src/main.zig | 30 +++++------------------------- src/util.zig | 9 +-------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/main.zig b/src/main.zig index 8d40ca2..b60af40 100644 --- a/src/main.zig +++ b/src/main.zig @@ -193,8 +193,6 @@ export fn start() void { circuit.load(mapPos, &assets.conduit, assets.conduit_size); map.load(mapPos, &assets.solid, assets.solid_size); - // w4.trace("{}, {}, {}", .{ assets.spawn, mapPos, assets.spawn - mapPos }); - player = .{ .pos = Pos.init(util.vec2ToVec2f((assets.spawn - mapPos) * Map.tile_size) + Vec2f{ 4, 8 }), .control = .{ .controller = .player, .state = .stand }, @@ -415,7 +413,6 @@ fn manipulationProcess(pos: *Pos, control: *Control) void { if (input.btnp(.one, .two)) { nodes[details.which].pinned = true; nodes[details.which].pos = vec2tovec2f(indicator.?.pos); - nodes[details.which].last = vec2tovec2f(indicator.?.pos); control.grabbing = null; } } else if (input.btnp(.one, .two)) { @@ -428,12 +425,13 @@ fn manipulationProcess(pos: *Pos, control: *Control) void { fn wirePhysicsProcess(dt: f32, wire: *Wire) void { var nodes = wire.nodes.slice(); if (nodes.len == 0) return; + var physics = Physics{ .gravity = Vec2f{ 0, 0.25 }, .friction = Vec2f{ 0.1, 0.1 } }; + var kinematic = Kinematic{ .col = AABB{ .pos = Vec2f{ -1, -1 }, .size = Vec2f{ 1, 1 } } }; for (nodes) |*node| { - var physics = Physics{ .gravity = Vec2f{ 0, 0.25 }, .friction = Vec2f{ 0.1, 0.1 } }; velocityProcess(dt, node); physicsProcess(dt, node, &physics); - collideNode(node); + kinematicProcess(dt, node, &kinematic); } var iterations: usize = 0; @@ -442,31 +440,13 @@ fn wirePhysicsProcess(dt: f32, wire: *Wire) void { while (left < nodes.len) : (left += 1) { // Left side constrainNodes(&nodes[left - 1], &nodes[left]); - collideNode(&nodes[left - 1]); - collideNode(&nodes[left]); + kinematicProcess(dt, &nodes[left - 1], &kinematic); + kinematicProcess(dt, &nodes[left], &kinematic); } } } -/// Returns normal of collision -fn collideNode(node: *Pos) void { - if (node.pinned) return; - const tileSize = Vec2{ 8, 8 }; - const tileSizef = vec2tovec2f(tileSize); - const iPos = vec2ftovec2(node.pos); - const mapPos = @divTrunc(iPos, tileSize); - if (map.isSolid(mapPos)) { - const velNorm = util.normalizef(node.pos - node.last); - var collideVec = node.last; - while (!map.isSolid(vec2ftovec2((collideVec + velNorm) / tileSizef))) { - collideVec += velNorm; - } - node.pos = collideVec; - } -} - const wireSegmentMaxLength = 4; -const wireSegmentMaxLengthV = @splat(2, @as(f32, wireSegmentMaxLength)); fn wireMaxLength(wire: *Wire) f32 { return @intToFloat(f32, wire.nodes.len) * wireSegmentMaxLength; diff --git a/src/util.zig b/src/util.zig index bd58fa1..ee1092e 100644 --- a/src/util.zig +++ b/src/util.zig @@ -18,16 +18,9 @@ pub const DirF = struct { pub const right = Vec2f{ 1, 0 }; }; -pub fn distance(a: Vec2, b: Vec2) i32 { - var subbed = a - b; - subbed[0] = std.math.absInt(subbed[0]) catch unreachable; - subbed[1] = std.math.absInt(subbed[1]) catch unreachable; - return @reduce(.Max, subbed); -} - pub fn distancef(a: Vec2f, b: Vec2f) f32 { var subbed = @fabs(a - b); - return @reduce(.Max, subbed); + return lengthf(subbed); } pub fn lengthf(vec: Vec2f) f32 {