Use castle for vecmath (doesn't work fully yet)

This commit is contained in:
2026-01-04 17:13:58 +01:00
parent 21c1d2e139
commit fed1e982d2
23 changed files with 424 additions and 1894 deletions

View File

@@ -4,15 +4,11 @@ const std = @import("std");
const c = @import("const.zig");
const glfw = @import("zglfw");
const math = @import("math.zig");
const vm = @import("vecmath");
const Blocks = @import("assets/Blocks.zig");
const Chunks = @import("Chunks.zig");
const Iterator2 = math.Iterator2;
const Quaternion = math.Quaternion;
const Vector2 = math.Vector2;
const Vector3 = math.Vector3;
const Vector2Int = math.Vector2Int;
const Vector3Int = math.Vector3Int;
const AxisState = enum {
none,
@@ -157,25 +153,25 @@ const Button = struct {
const MovementState = union(enum) {
ground: struct {
horizontal_velocity_sv: Vector2Int,
horizontal_velocity_sv: vm.Vector2Int,
},
air: struct {
horizontal_velocity_sv: Vector2Int,
horizontal_velocity_sv: vm.Vector2Int,
vertical_velocity_sv: i32,
},
flight: void,
};
position_sv: Vector3Int,
pitch_rad: f32,
yaw_rad: f32,
position_sv: vm.Vector3Int,
pitch_turns: f32,
yaw_turns: f32,
x_input: Axis = .init(.d, .a),
y_input: Axis = .init(.w, .s),
z_input: Axis = .init(.space, .left_shift),
jump_input: Button = .init(.space),
mouse_sensitivity: f32 = 0.002,
mouse_sensitivity: f32 = 0.0003,
vertical_fov_deg: f32 = 80,
movement_state: MovementState,
@@ -185,8 +181,8 @@ pub const camera_height_vx = 1.62;
pub const horizontal_speed_sv = sv(11.0);
pub const vertical_speed_sv = sv(7.49);
pub const min_pitch_rad = -0.5 * std.math.pi;
pub const max_pitch_rad = 0.5 * std.math.pi;
pub const min_pitch_turns = -0.25;
pub const max_pitch_turns = 0.25;
pub const collision_half_width_sv = sv(0.4);
pub const collision_height_sv = sv(1.8);
@@ -203,11 +199,11 @@ pub const air_speed_limit_sv = sv(0.5612);
pub const raycast_length_sv = sv(5.0);
pub fn init(position_sv: Vector3Int, pitch_rad: f32, yaw_rad: f32) Player {
pub fn init(position_sv: vm.Vector3Int, pitch_turns: f32, yaw_turns: f32) Player {
return .{
.position_sv = position_sv,
.pitch_rad = pitch_rad,
.yaw_rad = yaw_rad,
.pitch_turns = pitch_turns,
.yaw_turns = yaw_turns,
.movement_state = .flight,
};
}
@@ -225,8 +221,8 @@ pub fn onKeyUp(self: *Player, key: glfw.Key) void {
}
pub fn onMouseMove(self: *Player, dx: f32, dy: f32) void {
self.pitch_rad = std.math.clamp(self.pitch_rad - dy * self.mouse_sensitivity, min_pitch_rad, max_pitch_rad);
self.yaw_rad = @mod(self.yaw_rad - dx * self.mouse_sensitivity, std.math.tau);
self.pitch_turns = std.math.clamp(self.pitch_turns - dy * self.mouse_sensitivity, min_pitch_turns, max_pitch_turns);
self.yaw_turns = @mod(self.yaw_turns - dx * self.mouse_sensitivity, 1);
}
pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
@@ -234,11 +230,11 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
//const raycast_origin_sv = self.position_sv
// .add(.init(0, 0, @intFromFloat(@round(c.sv_per_vx * camera_height_vx))));
//const raycast_ray_sv = Vector3
//const raycast_ray_sv = vm.Vector3
// .init(0, raycast_length_sv, 0)
// .rotate(.mulQuaternion(
// .initRotationXY(self.yaw_rad),
// .initRotationYZ(self.pitch_rad),
// .initRotation(.XY, self.yaw_turns),
// .initRotation(.YZ, self.pitch_turns),
// ))
// .asVector3Int();
//const raycast_hit = chunks.raycast(raycast_origin_sv, raycast_ray_sv);
@@ -246,14 +242,14 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
// --- GATHER INPUTS -------------------------------------------------------
const horizontal_input_vector_frac = blk: {
var ret = Vector2
var ret = vm.Vector2
.init(self.x_input.getComponentFloat(), self.y_input.getComponentFloat())
.rotate(self.yaw_rad);
.rotate(.initRotation(self.yaw_turns));
const len_squared = ret.lenSquared();
if (len_squared > 1) {
ret = ret.divScalar(@sqrt(len_squared));
}
break :blk ret.asVector2IntFrac();
break :blk math.asIntFrac2(ret);
};
const vertical_input_vector_frac = self.z_input.getComponentIntFrac();
@@ -279,10 +275,10 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
.ground => {},
.air => {},
.flight => {
const horizontal_velocity_sv = horizontal_input_vector_frac.mulScalarFrac(horizontal_speed_sv);
const horizontal_velocity_sv = math.mulFrac2(horizontal_input_vector_frac, horizontal_speed_sv);
const vertical_velocity_sv = math.mulFrac(vertical_input_vector_frac, vertical_speed_sv);
var horizontal_displacement_sv = horizontal_velocity_sv.mulScalarFloat(dt);
var horizontal_displacement_sv = math.mulIntFloat2(horizontal_velocity_sv, dt);
var vertical_displacement_sv = math.mulIntFloat(vertical_velocity_sv, dt);
var position_sv = self.position_sv;
@@ -301,9 +297,7 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
var i: usize = 0;
while (i < 2) : (i += 1) {
if (chunks.sweepCastHorizontal(min_sv, max_sv, horizontal_displacement_sv)) |hit| {
const adjustment = hit.normal_frac
.asVector2Int()
.mulScalarFrac(hit.projected_distance_sv);
const adjustment = math.mulFrac2(hit.normal_frac.dropZ(), hit.projected_distance_sv);
horizontal_displacement_sv = .add(
horizontal_displacement_sv,
adjustment,
@@ -312,11 +306,11 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
break;
}
}
position_sv = .add(position_sv, horizontal_displacement_sv.asVector3Int(0));
position_sv = .add(position_sv, horizontal_displacement_sv.withZ(0));
if (vertical_displacement_sv < 0.0) {
min_sv = .add(min_sv, horizontal_displacement_sv.asVector3Int(0));
max_sv = .add(max_sv, horizontal_displacement_sv.asVector3Int(0));
min_sv = .add(min_sv, horizontal_displacement_sv.withZ(0));
max_sv = .add(max_sv, horizontal_displacement_sv.withZ(0));
if (chunks.sweepCastDown(min_sv, max_sv, -vertical_displacement_sv)) |hit| {
vertical_displacement_sv += hit.projected_distance_sv;
}