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

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