Separate and improve player movement
This commit is contained in:
109
src/Game.zig
109
src/Game.zig
@@ -15,6 +15,7 @@ const Engine = @import("engine/Engine.zig");
|
||||
const GenericBuffer = @import("engine/GenericBuffer.zig").GenericBuffer;
|
||||
const StagingBuffer = @import("engine/StagingBuffer.zig");
|
||||
const Swapchain = @import("engine/Swapchain.zig");
|
||||
const Player = @import("Player.zig");
|
||||
const Interator2 = math.Interator2;
|
||||
const Matrix4x4 = math.Matrix4x4;
|
||||
const Quaternion = math.Quaternion;
|
||||
@@ -128,16 +129,7 @@ materials: Materials,
|
||||
textures: Textures,
|
||||
chunks: std.AutoHashMapUnmanaged([3]i16, Chunk),
|
||||
|
||||
camera_position: Vector3 = .init(0, 0, 1.62),
|
||||
camera_pitch: f32 = 0,
|
||||
camera_yaw: f32 = 0,
|
||||
|
||||
input_forwards: bool = false,
|
||||
input_backwards: bool = false,
|
||||
input_left: bool = false,
|
||||
input_right: bool = false,
|
||||
input_up: bool = false,
|
||||
input_down: bool = false,
|
||||
player: Player,
|
||||
|
||||
const max_textures = 1024;
|
||||
const max_point_lights = 1024;
|
||||
@@ -145,12 +137,6 @@ const max_directional_lights = 4;
|
||||
const chunk_descriptor_pool = 512;
|
||||
|
||||
const camera_near_plane = 0.1;
|
||||
const camera_vertical_fov_deg = 80.0;
|
||||
const camera_half_vertical_fov_rad = 0.5 * camera_vertical_fov_deg * std.math.rad_per_deg;
|
||||
const camera_mouse_sensitivity = 0.002;
|
||||
|
||||
const player_horizontal_speed = 11.0;
|
||||
const player_vertical_speed = 7.49;
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain) !Game {
|
||||
var materials = try Materials.init(engine, allocator);
|
||||
@@ -664,7 +650,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
}
|
||||
}
|
||||
|
||||
const camera_position = Vector3.init(0, 0, @as(f32, @floatFromInt(worldHeightI(.zero))) + 0.5 + 1.62);
|
||||
const player_position = Vector3.init(0, 0, @as(f32, @floatFromInt(worldHeightI(.zero))) + 0.5);
|
||||
|
||||
var chunk_it = chunks.iterator();
|
||||
while (chunk_it.next()) |entry| {
|
||||
@@ -724,7 +710,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
.textures = textures,
|
||||
.chunks = chunks,
|
||||
|
||||
.camera_position = camera_position,
|
||||
.player = .init(player_position, 0, 0),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -765,34 +751,25 @@ pub fn deinit(self: *Game) void {
|
||||
}
|
||||
|
||||
pub fn update(self: *Game, dt: f32) void {
|
||||
const camera_d = Vector2
|
||||
.init(
|
||||
@as(f32, @floatFromInt(@intFromBool(self.input_right))) - @as(f32, @floatFromInt(@intFromBool(self.input_left))),
|
||||
@as(f32, @floatFromInt(@intFromBool(self.input_forwards))) - @as(f32, @floatFromInt(@intFromBool(self.input_backwards))),
|
||||
)
|
||||
.rotate(self.camera_yaw)
|
||||
.mulScalar(player_horizontal_speed)
|
||||
.asVector3(player_vertical_speed * (@as(f32, @floatFromInt(@intFromBool(self.input_up))) - @as(f32, @floatFromInt(@intFromBool(self.input_down)))))
|
||||
.mulScalar(dt);
|
||||
|
||||
self.camera_position = Vector3.add(self.camera_position, camera_d);
|
||||
self.player.update(dt);
|
||||
|
||||
const framebuffer_size = Vector2.init(
|
||||
@floatFromInt(self.swapchain.extent.width),
|
||||
@floatFromInt(self.swapchain.extent.height),
|
||||
);
|
||||
|
||||
const camera_position = self.player.position.add(.init(0, Player.camera_height, 0));
|
||||
const camera_rotation = Quaternion.mulQuaternion(
|
||||
.initRotationXY(self.camera_yaw),
|
||||
.initRotationYZ(self.camera_pitch),
|
||||
.initRotationXY(self.player.yaw_rad),
|
||||
.initRotationYZ(self.player.pitch_rad),
|
||||
);
|
||||
const camera_aspect_ratio = framebuffer_size.getX() / framebuffer_size.getY();
|
||||
const camera_yscale = 1.0 / @tan(camera_half_vertical_fov_rad);
|
||||
const camera_yscale = 1.0 / @tan(0.5 * self.player.vertical_fov_deg * std.math.rad_per_deg);
|
||||
const camera_xscale = camera_yscale / camera_aspect_ratio;
|
||||
|
||||
const matrix_ws_to_vs = Matrix4x4.mulMatrix(
|
||||
Matrix4x4.initRotation(camera_rotation.conjugate()),
|
||||
Matrix4x4.initTranslation(self.camera_position.negate()),
|
||||
Matrix4x4.initTranslation(camera_position.negate()),
|
||||
);
|
||||
|
||||
// zig fmt: off
|
||||
@@ -832,78 +809,26 @@ pub fn update(self: *Game, dt: f32) void {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn onKeyDown(self: *Game, key_code: glfw.Key, mods: glfw.Mods) void {
|
||||
const no_mods = @as(c_int, @bitCast(mods)) == 0;
|
||||
|
||||
if (key_code == .escape and no_mods) {
|
||||
self.engine.mode.surface.window.setShouldClose(true);
|
||||
}
|
||||
|
||||
if (key_code == .w) {
|
||||
self.input_forwards = true;
|
||||
self.input_backwards = false;
|
||||
}
|
||||
if (key_code == .s) {
|
||||
self.input_backwards = true;
|
||||
self.input_forwards = false;
|
||||
}
|
||||
if (key_code == .a) {
|
||||
self.input_left = true;
|
||||
self.input_right = false;
|
||||
}
|
||||
if (key_code == .d) {
|
||||
self.input_right = true;
|
||||
self.input_left = false;
|
||||
}
|
||||
if (key_code == .space) {
|
||||
self.input_up = true;
|
||||
}
|
||||
if (key_code == .left_shift) {
|
||||
self.input_down = true;
|
||||
}
|
||||
pub fn onKeyDown(self: *Game, key: glfw.Key) void {
|
||||
self.player.onKeyDown(key);
|
||||
}
|
||||
|
||||
pub fn onKeyUp(self: *Game, key_code: glfw.Key, mods: glfw.Mods) void {
|
||||
_ = mods;
|
||||
|
||||
if (key_code == .w) {
|
||||
self.input_forwards = false;
|
||||
}
|
||||
if (key_code == .s) {
|
||||
self.input_backwards = false;
|
||||
}
|
||||
if (key_code == .a) {
|
||||
self.input_left = false;
|
||||
}
|
||||
if (key_code == .d) {
|
||||
self.input_right = false;
|
||||
}
|
||||
if (key_code == .space) {
|
||||
self.input_up = false;
|
||||
}
|
||||
if (key_code == .left_shift) {
|
||||
self.input_down = false;
|
||||
}
|
||||
pub fn onKeyUp(self: *Game, key: glfw.Key) void {
|
||||
self.player.onKeyUp(key);
|
||||
}
|
||||
|
||||
pub fn onMouseMove(self: *Game, dx: f32, dy: f32) void {
|
||||
self.camera_pitch -= dy * camera_mouse_sensitivity;
|
||||
self.camera_yaw -= dx * camera_mouse_sensitivity;
|
||||
|
||||
self.camera_pitch = std.math.clamp(self.camera_pitch, -0.5 * std.math.pi, 0.5 * std.math.pi);
|
||||
self.camera_yaw = @mod(self.camera_yaw, 2 * std.math.pi);
|
||||
self.player.onMouseMove(dx, dy);
|
||||
}
|
||||
|
||||
pub fn onMouseDown(self: *Game, button: glfw.MouseButton, mods: glfw.Mods) void {
|
||||
pub fn onMouseDown(self: *Game, button: glfw.MouseButton) void {
|
||||
_ = self;
|
||||
_ = button;
|
||||
_ = mods;
|
||||
}
|
||||
|
||||
pub fn onMouseUp(self: *Game, button: glfw.MouseButton, mods: glfw.Mods) void {
|
||||
pub fn onMouseUp(self: *Game, button: glfw.MouseButton) void {
|
||||
_ = self;
|
||||
_ = button;
|
||||
_ = mods;
|
||||
}
|
||||
|
||||
fn render(self: *Game) !void {
|
||||
|
||||
Reference in New Issue
Block a user