Skip to content

Commit 56c28fd

Browse files
committed
Fixing transforming a plane by a mat4
1 parent 4f9f0a8 commit 56c28fd

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/framework/math.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ pub const Vec4 = extern struct {
287287
pub const zero = Vec4.new(0.0, 0.0, 0.0, 1.0);
288288
};
289289

290+
// Mat4: 4x4 matrix stored in row-major
290291
pub const Mat4 = extern struct {
291292
m: [4][4]f32,
292293

src/framework/spatial/plane.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const math = @import("../math.zig");
33
const debug = @import("../debug.zig");
44
const boundingbox = @import("boundingbox.zig");
55
const assert = std.debug.assert;
6+
const testing = std.testing;
67

78
const Vec3 = math.Vec3;
89
const Vec4 = math.Vec4;
@@ -159,14 +160,14 @@ pub const Plane = struct {
159160
}
160161

161162
pub fn mulMat4(self: *const Plane, transform: math.Mat4) Plane {
162-
const on_plane = self.normal.scale(self.d).toVec4(1.0);
163+
const on_plane = self.normal.scale(-self.d).toVec4(1.0);
163164
const normal = self.normal.toVec4(0.0);
164165

165-
const transformed_on_plane = on_plane.mulMat4(transform);
166+
const transformed_on_plane = on_plane.mulMat4(transform).toVec3();
166167
const transformed_normal = normal.mulMat4(transform.invert().transpose());
167168

168169
const new_normal = transformed_normal.toVec3().norm();
169-
const new_distance = transformed_on_plane.toVec3().dot(new_normal);
170+
const new_distance = -new_normal.dot(transformed_on_plane);
170171

171172
return .{ .normal = new_normal, .d = new_distance };
172173
}
@@ -242,20 +243,21 @@ test "Plane.planeIntersectPoint" {
242243

243244
test "Plane.mulMat4" {
244245
const plane = Plane.init(Vec3.new(0, 0, 1), Vec3.new(1, 1, 1));
246+
try testing.expectEqual(-1.0, plane.d);
245247

246248
// multiplying by the identity should result in an identical plane
247249
const t1 = plane.mulMat4(math.Mat4.identity);
248-
assert(std.meta.eql(plane, t1));
250+
try testing.expectEqual(plane, t1);
249251

250252
// scaling should keep the normal, but increase the distance
251253
const t2 = plane.mulMat4(math.Mat4.scale(Vec3.new(2, 2, 2)));
252254
assert(std.meta.eql(plane.normal, t2.normal));
253255
assert(t2.d == plane.d * 2);
254256

255257
// translating should also just change the distance
256-
const t3 = plane.mulMat4(math.Mat4.translate(Vec3.new(1, 1, 1)));
257-
assert(std.meta.eql(plane.normal, t3.normal));
258-
assert(t3.d == 0);
258+
const t3 = plane.mulMat4(math.Mat4.translate(Vec3.new(102, 6, 10)));
259+
try testing.expectEqual(plane.normal, t3.normal);
260+
try testing.expectEqual(-11.0, t3.d); // remember, d is stored negated
259261

260262
// rotating should just change the normal
261263
const t4 = plane.mulMat4(math.Mat4.rotate(45, Vec3.new(1, 1, 1)));

src/framework/utils/quakemap.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,8 @@ pub const QuakeMap = struct {
601601
const v2 = try readPoint(&iter, math.Mat4.identity);
602602

603603
// map planes are clockwise, flip them around when computing the plane to get a counter-clockwise plane
604-
face.plane = Plane.initFromTriangle(v2.mulMat4(transform), v1.mulMat4(transform), v0.mulMat4(transform));
605604
face.untransformed_plane = Plane.initFromTriangle(v2, v1, v0);
605+
face.plane = face.untransformed_plane.mulMat4(transform);
606606
face.uv_direction = closestAxis(face.untransformed_plane.normal);
607607
face.u_axis = if (face.uv_direction.x == 1) Vec3.new(0, 1, 0) else Vec3.new(1, 0, 0);
608608
face.v_axis = if (face.uv_direction.z == 1) Vec3.new(0, -1, 0) else Vec3.new(0, 0, -1);

0 commit comments

Comments
 (0)