math: zig fmt

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-10-08 22:48:45 -07:00
parent 12e69752d3
commit dc5c1f69a6

View file

@ -85,14 +85,13 @@ pub const Rectangle = struct {
}
test vertices {
const rect = Rectangle{.pos = vec2(0.0, 0.0), .size = vec2(1.0, 1.0)};
const rect = Rectangle{ .pos = vec2(0.0, 0.0), .size = vec2(1.0, 1.0) };
const v = rect.vertices();
try testing.expect(Vec2, vec2(0.0, 0.0)).eql(v[0]);
try testing.expect(Vec2, vec2(1.0, 0.0)).eql(v[1]);
try testing.expect(Vec2, vec2(1.0, 1.0)).eql(v[2]);
try testing.expect(Vec2, vec2(0.0, 1.0)).eql(v[3]);
}
};
// A circle shape defined by position and radius.
@ -169,7 +168,7 @@ pub const Point = struct {
test collidesRect {
const a: Point = .{ .pos = vec2(6, 4) };
const c = Point{.pos = vec2(6.0, 3.0)};
const c = Point{ .pos = vec2(6.0, 3.0) };
var b: Rectangle = .{ .pos = vec2(6, 3), .size = vec2(3, 2) };
try testing.expect(bool, a.collidesRect(b)).eql(true);
try testing.expect(bool, c.collidesRect(b)).eql(true);
@ -178,7 +177,7 @@ pub const Point = struct {
try testing.expect(bool, a.collidesRect(b)).eql(false);
const p = Point{ .pos = vec2(0.0, 0.0) };
const r = Rectangle{.pos = vec2(0.0, 0.0), .size = vec2(1.0, 1.0)};
const r = Rectangle{ .pos = vec2(0.0, 0.0), .size = vec2(1.0, 1.0) };
try testing.expect(bool, p.collidesRect(r)).eql(true);
}
@ -225,18 +224,18 @@ pub const Point = struct {
}
test collidesPoly {
const poly = [_]Vec2 {
const poly = [_]Vec2{
vec2(-1.0, -1.0),
vec2(1.0, -1.0),
vec2(1.0, 1.0),
vec2(-1.0, 1.0),
};
try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(0.0, 0.0)}, &poly)).eql(true);
try testing.expect(bool, Point.collidesPoly(Point{ .pos = vec2(0.0, 0.0) }, &poly)).eql(true);
// TODO: decide if boundary is inside or not
//try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(1.0, 1.0)}, &poly)).eql(true);
try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(2.0, 2.0)}, &poly)).eql(false);
try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(-2.0, 2.0)}, &poly)).eql(false);
try testing.expect(bool, Point.collidesPoly(Point{ .pos = vec2(2.0, 2.0) }, &poly)).eql(false);
try testing.expect(bool, Point.collidesPoly(Point{ .pos = vec2(-2.0, 2.0) }, &poly)).eql(false);
}
/// Returns true if point is inside triangle.
@ -259,17 +258,17 @@ pub const Point = struct {
return (alpha > 0) and (beta > 0) and (gamma > 0);
}
test collidesTriangle {
const triangle = [_]Vec2 {
const triangle = [_]Vec2{
vec2(-1.0, -1.0),
vec2(1.0, -1.0),
vec2(0.0, 1.0),
};
try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(0.0, 0.0)}, &triangle)).eql(true);
try testing.expect(bool, Point.collidesPoly(Point{ .pos = vec2(0.0, 0.0) }, &triangle)).eql(true);
// TODO: decide if boundary is inside or not
//try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(0.0, 1.0)}, &triangle)).eql(true);
try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(2.0, 2.0)}, &triangle)).eql(false);
try testing.expect(bool, Point.collidesPoly(Point{.pos = vec2(-2.0, 2.0)}, &triangle)).eql(false);
try testing.expect(bool, Point.collidesPoly(Point{ .pos = vec2(2.0, 2.0) }, &triangle)).eql(false);
try testing.expect(bool, Point.collidesPoly(Point{ .pos = vec2(-2.0, 2.0) }, &triangle)).eql(false);
// TODO: decide if boundary is inside or not
// const t = [_]Vec2 {
@ -314,13 +313,12 @@ pub const Point = struct {
.end = vec2(1.0, 1.0),
.threshold = 0.1,
};
try testing.expect(bool, Point.collidesLine(Point{.pos = vec2(0.0, 0.0)}, l)).eql(true);
try testing.expect(bool, Point.collidesLine(Point{ .pos = vec2(0.0, 0.0) }, l)).eql(true);
// TODO: decide if boundary is inside or not
//try testing.expect(bool, Point.collidesLine(Point{.pos = vec2(0.0, 0.1)}, l)).eql(true);
try testing.expect(bool, Point.collidesLine(Point{.pos = vec2(0.0, 0.09)}, l)).eql(true);
try testing.expect(bool, Point.collidesLine(Point{.pos = vec2(0.0, 1.0)}, l)).eql(false);
try testing.expect(bool, Point.collidesLine(Point{ .pos = vec2(0.0, 0.09) }, l)).eql(true);
try testing.expect(bool, Point.collidesLine(Point{ .pos = vec2(0.0, 1.0) }, l)).eql(false);
}
};
/// A line specified by a start and endpoint and a threshold for the line thickness.
@ -344,25 +342,18 @@ pub const Line = struct {
}
test collidesLine {
const l0 = Line{.start = vec2(-1.0, -1.0), .end = vec2(1.0, 1.0), .threshold = 0.0};
try testing.expect(bool, true).eql(
l0.collidesLine(Line{.start = vec2(-1.0, 1.0), .end = vec2(1.0, -1.0), .threshold=0.0}));
try testing.expect(bool, true).eql(
l0.collidesLine(Line{.start = vec2(-10.0, 0.0), .end = vec2(10.0, 0.0), .threshold=0.0}));
try testing.expect(bool, true).eql(
l0.collidesLine(Line{.start = vec2(-10.0, 1.0), .end = vec2(10.0, 1.0), .threshold=0.0}));
try testing.expect(bool, true).eql(
l0.collidesLine(Line{.start = vec2(-10.0, -1.0), .end = vec2(10.0, -1.0), .threshold=0.0}));
const l0 = Line{ .start = vec2(-1.0, -1.0), .end = vec2(1.0, 1.0), .threshold = 0.0 };
try testing.expect(bool, true).eql(l0.collidesLine(Line{ .start = vec2(-1.0, 1.0), .end = vec2(1.0, -1.0), .threshold = 0.0 }));
try testing.expect(bool, true).eql(l0.collidesLine(Line{ .start = vec2(-10.0, 0.0), .end = vec2(10.0, 0.0), .threshold = 0.0 }));
try testing.expect(bool, true).eql(l0.collidesLine(Line{ .start = vec2(-10.0, 1.0), .end = vec2(10.0, 1.0), .threshold = 0.0 }));
try testing.expect(bool, true).eql(l0.collidesLine(Line{ .start = vec2(-10.0, -1.0), .end = vec2(10.0, -1.0), .threshold = 0.0 }));
// TODO: fails if same line
//try testing.expect(bool, true).eql(
// l0.collidesLine(l0));
try testing.expect(bool, false).eql(
l0.collidesLine(Line{.start = vec2(-1.1, -1.1), .end = vec2(1.1, 1.1), .threshold=0.0}));
try testing.expect(bool, false).eql(
l0.collidesLine(Line{.start = vec2(-10.0, 2.0), .end = vec2(10.0, 2.0), .threshold=0.0}));
try testing.expect(bool, false).eql(l0.collidesLine(Line{ .start = vec2(-1.1, -1.1), .end = vec2(1.1, 1.1), .threshold = 0.0 }));
try testing.expect(bool, false).eql(l0.collidesLine(Line{ .start = vec2(-10.0, 2.0), .end = vec2(10.0, 2.0), .threshold = 0.0 }));
}
};
@ -396,20 +387,14 @@ pub fn minmaxProjectionDistance(n: Vec2, v0: Vec2, v: []const Vec2) [2]f32 {
max_d = d;
}
}
return [2]f32{min_d, max_d};
return [2]f32{ min_d, max_d };
}
test minmaxProjectionDistance {
const n_up = vec2(0.0, 1.0);
const n_right = vec2(1.0, 0.0);
const v0 = vec2(0.0, 0.0);
const v = [_]Vec2{
vec2(2.0, 0.0),
vec2(1.0, 1.0),
vec2(0.0, -2.0),
vec2(-1.0, 2.0),
vec2(-2.0, 1.0)
};
const v = [_]Vec2{ vec2(2.0, 0.0), vec2(1.0, 1.0), vec2(0.0, -2.0), vec2(-1.0, 2.0), vec2(-2.0, 1.0) };
{
const minmax = minmaxProjectionDistance(n_up, v0, &v);
@ -422,22 +407,18 @@ test minmaxProjectionDistance {
try testing.expect(f32, -2.0).eql(minmax[0]);
try testing.expect(f32, 2.0).eql(minmax[1]);
}
}
const VertexDepthResult = struct {
v0: Vec2 = undefined,
v1: ?Vec2 = null,
/// Depth of vertex. Positive in the opposite direction of the normal.
d: f32
d: f32,
};
/// Find the vertex in v that is deepest behind the line defined by the point v0 and normal n.
pub fn findDeepestVertex(n: Vec2, v0: Vec2, v: []const Vec2) VertexDepthResult {
var min_depth = VertexDepthResult{
.v0 = v[0],
.d = n.dot(&v[0].sub(&v0))
};
var min_depth = VertexDepthResult{ .v0 = v[0], .d = n.dot(&v[0].sub(&v0)) };
for (v[1..]) |vb| {
const d = n.dot(&vb.sub(&v0));
if (d < min_depth.d) {
@ -463,7 +444,7 @@ test findDeepestVertex {
vec2(1.0, 1.0),
vec2(0.0, -2.0), // Deepest
vec2(-1.0, 2.0),
vec2(-2.0, 1.0)
vec2(-2.0, 1.0),
};
const depth = findDeepestVertex(n_up, v0, &v);
@ -478,7 +459,7 @@ test findDeepestVertex {
vec2(1.0, 1.0),
vec2(0.0, -3.0), // Deepest
vec2(-1.0, -3.0), // Deepest
vec2(-2.0, 1.0)
vec2(-2.0, 1.0),
};
const depth = findDeepestVertex(n_up, v0, &v);
@ -493,7 +474,7 @@ test findDeepestVertex {
vec2(2.0, 0.5), // Closest
vec2(1.0, 1.0),
vec2(0.0, 3.0),
vec2(-2.0, 1.0)
vec2(-2.0, 1.0),
};
const depth = findDeepestVertex(n_up, v0, &v);
@ -501,13 +482,12 @@ test findDeepestVertex {
try testing.expect(?Vec2, null).eql(depth.v1);
try testing.expect(f32, -0.5).eql(depth.d);
}
}
/// Contains information to separate two colliding shapes.
const SeparationResult = struct {
// i0: usize = undefined, // Vertex idx
// i1: ?usize = null,
// i0: usize = undefined, // Vertex idx
// i1: ?usize = null,
v0: Vec2 = undefined,
v1: ?Vec2 = null,
e: usize = undefined, // Edge idx
@ -521,7 +501,7 @@ const SeparationResult = struct {
pub fn findMinSeparation(polygon_a: []const Vec2, polygon_b: []const Vec2) ?SeparationResult {
var min_result = SeparationResult{};
var v0 = polygon_b[polygon_b.len-1];
var v0 = polygon_b[polygon_b.len - 1];
for (polygon_b[0..], 0..) |v1, i| {
const edge = v1.sub(&v0);
const n = vec2(edge.y(), -edge.x()).normalize(0.0);
@ -571,10 +551,7 @@ test findMinSeparation {
try testing.expect(f32, 0.25).eql(result.?.d);
// Not colliding - bottom edge of triangle_0 separates the two
try testing.expect(?SeparationResult, null).eql(
findMinSeparation(&triangle_2, &triangle_0)
);
try testing.expect(?SeparationResult, null).eql(findMinSeparation(&triangle_2, &triangle_0));
}
/// Compute a Contact report between polygon_a and polygon_b if they are colliding.
@ -612,20 +589,16 @@ pub fn polygonPolygonContact(polygon_a: []const Vec2, polygon_b: []const Vec2) ?
} else {
// Paralell edges - find two contact points
const edge = vec2(min_separation_a.n.y(), -min_separation_a.n.x());
const vertices = [_]Vec2{
min_separation_a.v0,
min_separation_a.v1.?,
min_separation_b.v0,
min_separation_b.v1.?};
const from_a = [4]bool {true, true, false, false};
const vertices = [_]Vec2{ min_separation_a.v0, min_separation_a.v1.?, min_separation_b.v0, min_separation_b.v1.? };
const from_a = [4]bool{ true, true, false, false };
var distances: [4]f32 = undefined;
for (vertices, &distances) |v, *d| {
d.* = edge.dot(&v);
}
// Sort vertices along the edge
var idx = [_]u8{0,1,2,3};
var idx = [_]u8{ 0, 1, 2, 3 };
for (0..3) |i| {
for (i+1..4) |j| {
for (i + 1..4) |j| {
if (distances[idx[i]] > distances[idx[j]]) {
const t = idx[i];
idx[i] = idx[j];
@ -636,10 +609,8 @@ pub fn polygonPolygonContact(polygon_a: []const Vec2, polygon_b: []const Vec2) ?
depth = min_separation_a.d;
normal = min_separation_a.n.mulScalar(-1.0);
cp1_a = if (from_a[idx[1]]) vertices[idx[1]]
else vertices[idx[1]].add(&normal.mulScalar(depth));
cp2_a = if (from_a[idx[2]]) vertices[idx[2]]
else vertices[idx[2]].add(&normal.mulScalar(depth));
cp1_a = if (from_a[idx[1]]) vertices[idx[1]] else vertices[idx[1]].add(&normal.mulScalar(depth));
cp2_a = if (from_a[idx[2]]) vertices[idx[2]] else vertices[idx[2]].add(&normal.mulScalar(depth));
}
}
@ -689,10 +660,10 @@ test polygonPolygonContact {
try testing.expect(?Contact, null).eql(polygonPolygonContact(&triangle_0, &triangle_2));
try testing.expect(?Contact, null).eql(polygonPolygonContact(&triangle_2, &triangle_0));
const rect1 = Rectangle{.pos = vec2(-1.0, -1.0), .size = vec2(2.0, 2.0)};
const rect2 = Rectangle{.pos = vec2(-1.5, -2.25), .size = vec2(1.0, 1.5)};
const rect3 = Rectangle{.pos = vec2(-0.5, -2.25), .size = vec2(1.0, 1.5)};
const rect4 = Rectangle{.pos = vec2( 0.5, -2.25), .size = vec2(1.0, 1.5)};
const rect1 = Rectangle{ .pos = vec2(-1.0, -1.0), .size = vec2(2.0, 2.0) };
const rect2 = Rectangle{ .pos = vec2(-1.5, -2.25), .size = vec2(1.0, 1.5) };
const rect3 = Rectangle{ .pos = vec2(-0.5, -2.25), .size = vec2(1.0, 1.5) };
const rect4 = Rectangle{ .pos = vec2(0.5, -2.25), .size = vec2(1.0, 1.5) };
const r1 = rect1.vertices();
const r2 = rect2.vertices();
const r3 = rect3.vertices();
@ -729,16 +700,8 @@ pub fn circlePolygonContact(circle_a: Circle, polygon_b: []const Vec2) ?Contact
var depth: f32 = 0.0;
var cp1_a: ?Vec2 = null;
var v0 = polygon_b[polygon_b.len-1];
var min_result = struct {
n: Vec2,
d: f32,
i: usize
}{
.n = vec2(0.0, 0.0),
.d = std.math.floatMax(f32),
.i = undefined
};
var v0 = polygon_b[polygon_b.len - 1];
var min_result = struct { n: Vec2, d: f32, i: usize }{ .n = vec2(0.0, 0.0), .d = std.math.floatMax(f32), .i = undefined };
var closest_vertex = struct {
v: Vec2 = undefined,
d: f32 = std.math.floatMax(f32),
@ -779,7 +742,7 @@ pub fn circlePolygonContact(circle_a: Circle, polygon_b: []const Vec2) ?Contact
// Circle projects to +- radius
const vc = circle_a.pos.sub(&v0);
const d = vc.dot(&n);
const minmax_a = [2]f32{d - circle_a.radius, d + circle_a.radius};
const minmax_a = [2]f32{ d - circle_a.radius, d + circle_a.radius };
if ((minmax_a[0] > minmax_b[1]) or (minmax_a[1] < minmax_b[0])) {
// Circle does not intersect
@ -805,9 +768,9 @@ pub fn circlePolygonContact(circle_a: Circle, polygon_b: []const Vec2) ?Contact
}
test circlePolygonContact {
const rect1 = Rectangle{.pos = vec2(0.75, -1.0), .size = vec2(2.0, 2.0)};
const rect1 = Rectangle{ .pos = vec2(0.75, -1.0), .size = vec2(2.0, 2.0) };
const r1 = rect1.vertices();
if (circlePolygonContact(Circle{.pos=vec2(0.0, 0.0), .radius = 1.0}, &r1)) |contact| {
if (circlePolygonContact(Circle{ .pos = vec2(0.0, 0.0), .radius = 1.0 }, &r1)) |contact| {
try testing.expect(Vec2, vec2(1.0, 0.0)).eql(contact.normal);
try testing.expect(f32, 0.25).eql(contact.depth);
try testing.expect(Vec2, vec2(1.0, 0.0)).eql(contact.cp1.?);
@ -816,9 +779,7 @@ test circlePolygonContact {
try testing.expect(bool, true).eql(false);
}
try testing.expect(?Contact, null).eql(
circlePolygonContact(Circle{.pos=vec2(-1.0, 0.0), .radius = 1.0}, &r1)
);
try testing.expect(?Contact, null).eql(circlePolygonContact(Circle{ .pos = vec2(-1.0, 0.0), .radius = 1.0 }, &r1));
}
/// Compute a Contact report between two circles.
@ -841,9 +802,7 @@ pub fn circleCircleContact(circle_a: Circle, circle_b: Circle) ?Contact {
}
test circleCircleContact {
if (circleCircleContact(Circle{.pos=vec2(0.0, 0.0), .radius = 1.0},
Circle{.pos = vec2(1.75, 0.0), .radius = 1.0})) |contact| {
if (circleCircleContact(Circle{ .pos = vec2(0.0, 0.0), .radius = 1.0 }, Circle{ .pos = vec2(1.75, 0.0), .radius = 1.0 })) |contact| {
try testing.expect(Vec2, vec2(1.0, 0.0)).eql(contact.normal);
try testing.expect(f32, 0.25).eql(contact.depth);
try testing.expect(Vec2, vec2(1.0, 0.0)).eql(contact.cp1.?);
@ -852,9 +811,7 @@ test circleCircleContact {
try testing.expect(bool, true).eql(false);
}
if (circleCircleContact(Circle{.pos=vec2(0.0, 0.0), .radius = 1.0},
Circle{.pos = vec2(2.0, 0.0), .radius = 1.0})) |contact| {
if (circleCircleContact(Circle{ .pos = vec2(0.0, 0.0), .radius = 1.0 }, Circle{ .pos = vec2(2.0, 0.0), .radius = 1.0 })) |contact| {
try testing.expect(Vec2, vec2(1.0, 0.0)).eql(contact.normal);
try testing.expect(f32, 0.0).eql(contact.depth);
try testing.expect(Vec2, vec2(1.0, 0.0)).eql(contact.cp1.?);
@ -863,8 +820,5 @@ test circleCircleContact {
try testing.expect(bool, true).eql(false);
}
try testing.expect(?Contact, null).eql(
circleCircleContact(Circle{.pos=vec2(0.0, 0.0), .radius = 1.0},
Circle{.pos = vec2(2.01, 0.0), .radius = 1.0})
);
try testing.expect(?Contact, null).eql(circleCircleContact(Circle{ .pos = vec2(0.0, 0.0), .radius = 1.0 }, Circle{ .pos = vec2(2.01, 0.0), .radius = 1.0 }));
}