1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}
|