@@ -2,6 +2,7 @@ const std = @import("std");
2
2
const debug = @import ("../debug.zig" );
3
3
const math = @import ("../math.zig" );
4
4
const plane = @import ("../spatial/plane.zig" );
5
+ const boundingbox = @import ("../spatial/boundingbox.zig" );
5
6
const mesh = @import ("../graphics/mesh.zig" );
6
7
const colors = @import ("../colors.zig" );
7
8
const graphics = @import ("../platform/graphics.zig" );
@@ -15,6 +16,7 @@ const Vec3 = math.Vec3;
15
16
const Vec2 = math .Vec2 ;
16
17
const Plane = plane .Plane ;
17
18
const Mesh = mesh .Mesh ;
19
+ const BoundingBox = boundingbox .BoundingBox ;
18
20
19
21
// From https://github.com/fabioarnold/3d-game/blob/master/src/QuakeMap.zig
20
22
// This is so cool!
@@ -182,6 +184,53 @@ pub const Solid = struct {
182
184
}
183
185
}
184
186
}
187
+
188
+ pub fn checkCollision (self : * const Solid , point : math.Vec3 ) bool {
189
+ for (self .faces .items ) | * face | {
190
+ if (face .plane .testPoint (point ) == .FRONT )
191
+ return false ;
192
+ }
193
+ return true ;
194
+ }
195
+
196
+ pub fn checkBoundingBoxCollision (self : * const Solid , bounds : BoundingBox ) bool {
197
+ const x_size = (bounds .max .x - bounds .min .x ) * 0.5 ;
198
+ const y_size = (bounds .max .y - bounds .min .y ) * 0.5 ;
199
+ const z_size = (bounds .max .z - bounds .min .z ) * 0.5 ;
200
+
201
+ const point = bounds .center ;
202
+ for (self .faces .items ) | * face | {
203
+ var expand_dist : f32 = 0 ;
204
+
205
+ // x_axis
206
+ const x_d = face .plane .normal .dot (Vec3 .x_axis );
207
+ if (x_d > 0 ) expand_dist += - x_d * x_size ;
208
+
209
+ const x_d_n = face .plane .normal .dot (Vec3 .x_axis .scale (-1 ));
210
+ if (x_d_n > 0 ) expand_dist += - x_d_n * x_size ;
211
+
212
+ // y_axis
213
+ const y_d = face .plane .normal .dot (Vec3 .y_axis );
214
+ if (y_d > 0 ) expand_dist += y_d * y_size ;
215
+
216
+ const y_d_n = face .plane .normal .dot (Vec3 .y_axis .scale (-1 ));
217
+ if (y_d_n > 0 ) expand_dist += y_d_n * y_size ;
218
+
219
+ // z_axis
220
+ const z_d = face .plane .normal .dot (Vec3 .z_axis );
221
+ if (z_d > 0 ) expand_dist += - z_d * z_size ;
222
+
223
+ const z_d_n = face .plane .normal .dot (Vec3 .z_axis .scale (-1 ));
224
+ if (z_d_n > 0 ) expand_dist += - z_d_n * z_size ;
225
+
226
+ var expandedface = face .plane ;
227
+ expandedface .d += expand_dist ;
228
+
229
+ if (expandedface .testPoint (point ) == .FRONT )
230
+ return false ;
231
+ }
232
+ return true ;
233
+ }
185
234
};
186
235
187
236
pub const QuakeMaterial = struct {
0 commit comments