Skip to content

Commit 93e759a

Browse files
committed
Simple collision checks for quake map solids
1 parent aa6092d commit 93e759a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/framework/utils/quakemap.zig

+49
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std");
22
const debug = @import("../debug.zig");
33
const math = @import("../math.zig");
44
const plane = @import("../spatial/plane.zig");
5+
const boundingbox = @import("../spatial/boundingbox.zig");
56
const mesh = @import("../graphics/mesh.zig");
67
const colors = @import("../colors.zig");
78
const graphics = @import("../platform/graphics.zig");
@@ -15,6 +16,7 @@ const Vec3 = math.Vec3;
1516
const Vec2 = math.Vec2;
1617
const Plane = plane.Plane;
1718
const Mesh = mesh.Mesh;
19+
const BoundingBox = boundingbox.BoundingBox;
1820

1921
// From https://github.com/fabioarnold/3d-game/blob/master/src/QuakeMap.zig
2022
// This is so cool!
@@ -182,6 +184,53 @@ pub const Solid = struct {
182184
}
183185
}
184186
}
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+
}
185234
};
186235

187236
pub const QuakeMaterial = struct {

0 commit comments

Comments
 (0)