diff options
Diffstat (limited to 'examples/noc/src/accel_to_mouse.zig')
| -rw-r--r-- | examples/noc/src/accel_to_mouse.zig | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/noc/src/accel_to_mouse.zig b/examples/noc/src/accel_to_mouse.zig new file mode 100644 index 0000000..9f6e8af --- /dev/null +++ b/examples/noc/src/accel_to_mouse.zig @@ -0,0 +1,31 @@ +//! Example 1.10: Accelerating towards the mouse. +const sk = @import("sketch"); +const vec = sk.math; + +pub const name = "accel_to_mouse"; +pub const width = 640; +pub const height = 360; + +const topspeed = 5; + +pub const State = struct { + position: vec.Vec = .{ width / 2, height / 2 }, + velocity: vec.Vec = .{ 0, 0 }, + clock: sk.Clock = .{}, +}; + +pub fn update(s: *State, in: *const sk.Input) void { + for (0..s.clock.steps(in)) |_| { + const mouse: vec.Vec = .{ in.mouse_x, in.mouse_y }; + const acceleration = vec.withLength(mouse - s.position, 0.2); + s.velocity = vec.clampLength(s.velocity + acceleration, topspeed); + s.position += s.velocity; + } +} + +const ball: sk.draw.Pen = .{ .fill = .gray(127), .stroke = .black, .width = 2 }; + +pub fn draw(s: *const State, f: *sk.Frame) void { + f.clear(sk.Color.white); + sk.draw.circle(f, s.position, 24, ball); +} |
