Sway and matrices

This commit is contained in:
duck
2025-08-24 18:54:49 +05:00
parent b73f0d446a
commit f3d2eff20e
4 changed files with 42 additions and 20 deletions

View File

@@ -11,6 +11,8 @@ player: bool = false,
enemy: bool = false, enemy: bool = false,
controller: Controller = .{}, controller: Controller = .{},
next_update: Time = Time.ZERO, next_update: Time = Time.ZERO,
sway_x: math.Sway(f32) = .{ .amplitude = 1, .frequency = 0.11 },
sway_y: math.Sway(f32) = .{ .amplitude = 1, .frequency = 0.13 },
const Controller = struct { const Controller = struct {
const Action = union(enum) { const Action = union(enum) {
@@ -78,20 +80,23 @@ fn updateController(self: *Self) void {
} }
pub fn draw(self: *Self, delta: f32) void { pub fn draw(self: *Self, delta: f32) void {
self.sway_x.update(delta);
self.sway_y.update(delta);
const transform = Graphics.Transform{ const transform = Graphics.Transform{
.rotation = Graphics.Transform.rotationByAxis(.{ self.sway_x.value, self.sway_y.value, 0 }, 0.05),
.position = .{ .position = .{
@floatFromInt(self.position[0]), @floatFromInt(self.position[0]),
@floatFromInt(self.position[1]), @floatFromInt(self.position[1]),
0.5, 0.5,
}, },
}; };
Graphics.drawMesh(World.cube_mesh, World.texture, transform); Graphics.drawMesh(World.plane_mesh, World.texture, transform.matrix());
if (!self.player) return; if (!self.player) return;
Graphics.camera.transform.position = math.lerpTimeLn( Graphics.camera.transform.position = math.lerpTimeLn(
Graphics.camera.transform.position, Graphics.camera.transform.position,
transform.position + @Vector(3, f32){ 0.0, -2.0, 5.0 }, transform.position + @Vector(3, f32){ 0.0, -1.0, 4.0 },
delta, delta,
-25, -25,
); );

View File

@@ -385,11 +385,11 @@ pub fn beginDraw() bool {
return true; return true;
} }
pub fn drawMesh(mesh: Mesh, texture: Assets.Texture, transform: Transform) void { pub fn drawMesh(mesh: Mesh, texture: Assets.Texture, matrix: Transform.TMatrix) void {
if (Graphics.render_pass == null) return; if (Graphics.render_pass == null) return;
const asset_texture = Assets.get(texture) orelse return; const asset_texture = Assets.get(texture) orelse return;
sdl.PushGPUVertexUniformData(Graphics.command_buffer, 1, &transform.matrix(), 16 * 4); sdl.PushGPUVertexUniformData(Graphics.command_buffer, 1, &matrix, 16 * 4);
sdl.BindGPUFragmentSamplers(Graphics.render_pass, 0, &sdl.GPUTextureSamplerBinding{ sdl.BindGPUFragmentSamplers(Graphics.render_pass, 0, &sdl.GPUTextureSamplerBinding{
.texture = asset_texture.texture, .texture = asset_texture.texture,
.sampler = asset_texture.sampler, .sampler = asset_texture.sampler,

View File

@@ -106,3 +106,34 @@ pub fn dotInt(a: anytype, b: anytype) (@typeInfo(@TypeOf(a)).vector.child) {
return @reduce(.Add, a * b); return @reduce(.Add, a * b);
} }
pub fn Sway(comptime T: type) type {
const STABILIZATION = -1;
return packed struct {
value: T = 0,
velocity: T = 0,
frequency: T,
amplitude: T,
const Self = @This();
pub fn update(self: *Self, delta: f32) void {
@setFloatMode(.optimized);
const dist = delta * -2 * std.math.pi * self.frequency;
const sin = std.math.sin(dist);
const cos = std.math.cos(dist);
var len = length(@Vector(2, T){ self.value, self.velocity });
if(len < 0.001) {
self.value = 0.001;
self.velocity = 0;
len = 0.001;
}
const new_value = self.value * cos - self.velocity * sin;
const new_velocity = self.value * sin + self.velocity * cos;
const target_len = lerpTimeLn(len, self.amplitude, delta, STABILIZATION);
const mult = target_len / len;
self.value = new_value * mult;
self.velocity = new_velocity * mult;
}
};
}

View File

@@ -6,7 +6,7 @@ const comp = @import("components.zig");
pub var time: Time = undefined; pub var time: Time = undefined;
var next_stop: Time = undefined; var next_stop: Time = undefined;
var entities: comp.Storage(Entity, .{}) = undefined; pub var entities: comp.Storage(Entity, .{}) = undefined;
pub var plane_mesh: Graphics.Mesh = undefined; pub var plane_mesh: Graphics.Mesh = undefined;
pub var cube_mesh: Graphics.Mesh = undefined; pub var cube_mesh: Graphics.Mesh = undefined;
@@ -19,20 +19,6 @@ pub fn initDebug() void {
.position = .{ 0, 0 }, .position = .{ 0, 0 },
.player = true, .player = true,
}); });
_ = entities.add(.{
.position = .{ 2, 0 },
.enemy = true,
.controller = .{
.move_units = 0.25,
},
});
_ = entities.add(.{
.position = .{ 3, 0 },
.enemy = true,
.controller = .{
.move_units = 0.25,
},
});
time = Time.ZERO; time = Time.ZERO;
World.plane_mesh = Graphics.loadMesh(@ptrCast(&PLANE_MESH_DATA)); World.plane_mesh = Graphics.loadMesh(@ptrCast(&PLANE_MESH_DATA));
World.cube_mesh = Graphics.loadMesh(@ptrCast(&CUBE_MESH_DATA)); World.cube_mesh = Graphics.loadMesh(@ptrCast(&CUBE_MESH_DATA));
@@ -60,7 +46,7 @@ pub fn updateReal(delta: f32) void {
} }
pub fn draw(delta: f32) void { pub fn draw(delta: f32) void {
Graphics.drawMesh(World.plane_mesh, World.texture, .{ .scale = @splat(5) }); Graphics.drawMesh(World.plane_mesh, World.texture, Graphics.Transform.matrix(.{ .scale = @splat(5) }));
var iter = World.entities.iter(); var iter = World.entities.iter();
while (iter.next()) |entity| { while (iter.next()) |entity| {
entity.draw(delta); entity.draw(delta);