Added ball physics
This commit is contained in:
parent
a945de3218
commit
63029a3a27
3 changed files with 31 additions and 6 deletions
33
src/ball.zig
33
src/ball.zig
|
@ -3,21 +3,44 @@ const utils = @import("utils.zig");
|
||||||
const w4 = @import("wasm4.zig");
|
const w4 = @import("wasm4.zig");
|
||||||
|
|
||||||
pub const Ball = struct {
|
pub const Ball = struct {
|
||||||
|
// position of the top left corner
|
||||||
x: f64,
|
x: f64,
|
||||||
y: f64,
|
y: f64,
|
||||||
dx: f64,
|
vx: f64,
|
||||||
dy: f64,
|
vy: f64,
|
||||||
pub fn draw(self: Ball) void {
|
pub fn draw(self: Ball) void {
|
||||||
var y = @floatToInt(u8, std.math.round(self.y));
|
var y = @floatToInt(u8, std.math.round(self.y));
|
||||||
var x = @floatToInt(u8, std.math.round(self.x));
|
var x = @floatToInt(u8, std.math.round(self.x));
|
||||||
w4.DRAW_COLORS.* = 0x4321;
|
w4.DRAW_COLORS.* = 0x2400;
|
||||||
w4.blit(&ball, x, y, ball_width, ball_height, ball_flags);
|
w4.blit(&ball, x, y, ball_width, ball_height, ball_flags);
|
||||||
}
|
}
|
||||||
pub fn resetRound(self: *Ball, side: utils.side) void {
|
pub fn resetRound(self: *Ball, side: utils.side) void {
|
||||||
self.x = @intToFloat(f64, utils.startingX[@enumToInt(side)] + 4);
|
self.x = @intToFloat(f64, utils.startingX[@enumToInt(side)] + 4);
|
||||||
self.y = 160 - 32 - 8;
|
self.y = 160 - 32 - 8;
|
||||||
self.dx = 0;
|
self.vx = 0;
|
||||||
self.dy = 0;
|
self.vy = -250;
|
||||||
|
}
|
||||||
|
pub fn update(self: *Ball) void {
|
||||||
|
self.vy += utils.gravity;
|
||||||
|
self.x += self.vx * utils.frequency;
|
||||||
|
self.y += self.vy * utils.frequency;
|
||||||
|
// collisions handling
|
||||||
|
if (self.x < 0) { // left wall
|
||||||
|
self.vx = -self.vx * utils.bounce;
|
||||||
|
self.x = 0;
|
||||||
|
}
|
||||||
|
if (self.x >= 160 - ball_width) { // right wall
|
||||||
|
self.vx = -self.vx * utils.bounce;
|
||||||
|
self.x = 160 - ball_width - 1;
|
||||||
|
}
|
||||||
|
if (self.y < 0) { // ceiling
|
||||||
|
self.vy = -self.vy * utils.bounce;
|
||||||
|
self.y = 0;
|
||||||
|
}
|
||||||
|
if (self.y >= 160 - ball_height) { // floor
|
||||||
|
self.vy = 0;
|
||||||
|
self.y = 160 - ball_height;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,9 @@ pub const Game = struct {
|
||||||
playerSide: utils.side = undefined,
|
playerSide: utils.side = undefined,
|
||||||
|
|
||||||
pub fn draw(self: *Game) void {
|
pub fn draw(self: *Game) void {
|
||||||
self.ball.draw();
|
|
||||||
self.brothers[0].draw();
|
self.brothers[0].draw();
|
||||||
self.brothers[1].draw();
|
self.brothers[1].draw();
|
||||||
|
self.ball.draw();
|
||||||
// draw the net
|
// draw the net
|
||||||
w4.DRAW_COLORS.* = 0x42;
|
w4.DRAW_COLORS.* = 0x42;
|
||||||
w4.rect(78, 100, 4, 61);
|
w4.rect(78, 100, 4, 61);
|
||||||
|
@ -34,5 +34,6 @@ pub const Game = struct {
|
||||||
self.gamepads[1].update(w4.GAMEPAD2.*);
|
self.gamepads[1].update(w4.GAMEPAD2.*);
|
||||||
self.brothers[0].update(self.gamepads[0]);
|
self.brothers[0].update(self.gamepads[0]);
|
||||||
self.brothers[1].update(self.gamepads[1]);
|
self.brothers[1].update(self.gamepads[1]);
|
||||||
|
self.ball.update();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
//----- Physics ---------------------------------------------------------------
|
//----- Physics ---------------------------------------------------------------
|
||||||
|
pub const bounce: f64 = 0.7; // energy dispersion when bouncing
|
||||||
pub const gravity: f64 = 9.807; // m/s²
|
pub const gravity: f64 = 9.807; // m/s²
|
||||||
pub const scale: f64 = 1.0 / 30.0; // 30 pixels == 1m
|
pub const scale: f64 = 1.0 / 30.0; // 30 pixels == 1m
|
||||||
pub const frequency: f64 = 1.0 / 60.0; // 60 fps
|
pub const frequency: f64 = 1.0 / 60.0; // 60 fps
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue