@@ -3,6 +3,7 @@ const math = @import("../math.zig");
33const debug = @import ("../debug.zig" );
44const boundingbox = @import ("boundingbox.zig" );
55const assert = std .debug .assert ;
6+ const testing = std .testing ;
67
78const Vec3 = math .Vec3 ;
89const 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
243244test "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 )));
0 commit comments