1
Fork 0

implemented a minimum physics rate

This commit is contained in:
Andy Killorin 2023-09-16 20:19:35 -05:00
parent b221f65748
commit 4e95e2efca
Signed by: ank
GPG key ID: B6241CA3B552BCA4
2 changed files with 25 additions and 19 deletions

View file

@ -4,10 +4,11 @@ var memory;
var exports; var exports;
const width = 160; const width = 160;
const height = 144; const height = 144;
const TIME_GAIN = 0.1; // game is unplayable at full speed with wasm refresh rates
function blit_frame() { function blit_frame() {
// this has an insane performance impact, but it is required when using an // this is required when using an allocator from wasm as there is
// allocator from wasm, as there is no way to update the internal pointer // no way to update the internal pointer when linear memory shifts
image = new ImageData( image = new ImageData(
new Uint8ClampedArray( new Uint8ClampedArray(
memory.buffer, memory.buffer,
@ -17,7 +18,7 @@ function blit_frame() {
width, width,
); );
ctx.putImageData(image, 0, 0, 0, 0, width-1, height-1); ctx.putImageData(image, 0, 0);
} }
function blit_text(text, len, x, y, size) { function blit_text(text, len, x, y, size) {
@ -77,7 +78,7 @@ async function init() {
} }
const FRAME_TIME = new Float32Array(exports.memory.buffer, exports.LAST_FRAME_TIME, 1); const FRAME_TIME = new Float32Array(exports.memory.buffer, exports.LAST_FRAME_TIME, 1);
FRAME_TIME[0] = elapsed; FRAME_TIME[0] = elapsed * TIME_GAIN;
instance.exports.frame_entry(); instance.exports.frame_entry();

View file

@ -111,8 +111,8 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
let mut camera = CAMERA.lock(); let mut camera = CAMERA.lock();
let mut boat = BOAT.lock(); let mut boat = BOAT.lock();
let gain = unsafe { // very much an approximation of constant-velocity animation let mut gain = unsafe { // very much an approximation of constant-velocity animation
LAST_FRAME_TIME.min(100.0) / (1000.0 / 20.0) // normalize to 20fps, cap simulation at 10fps LAST_FRAME_TIME / (1000.0 / 20.0) // normalize to 20fps, cap simulation at 10fps
}; };
while let Some(key) = INPUTS.lock().pop() { while let Some(key) = INPUTS.lock().pop() {
match key[0] { // [tag:input_handler] match key[0] { // [tag:input_handler]
@ -141,12 +141,15 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
} }
let wind = 0.0/RAD_TO_DEG; let wind = 0.0/RAD_TO_DEG;
let step = 1.0; // 50ms
while gain > 0.0 { // when draw fps is low, physics will still run at a minimum of 20fps
gain -= step;
let gain = gain + step;
let vel = boat.get_velocity(wind); let vel = boat.get_velocity(wind);
let camera_vec = Vector2::new(camera[0],camera[1]); let depth = -sample_world(boat.get_pos()+HALF, rand);
let boat_pos = boat.get_pos();
let depth = -sample_world(boat_pos+HALF, rand);
if depth < -0.04 { if depth < -0.04 {
boat.vel = 0.0; boat.vel = 0.0;
} else if depth < 0.0 { } else if depth < 0.0 {
@ -157,9 +160,11 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
boat.vel = noise::lerp(boat.vel, vel * 0.82, 0.13 * gain); boat.vel = noise::lerp(boat.vel, vel * 0.82, 0.13 * gain);
boat.go(gain); boat.go(gain);
} }
}
// draw sea // draw sea
const HALF: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0); const HALF: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0);
let camera_vec = Vector2::new(camera[0],camera[1]);
#[cfg(feature = "rayon")] #[cfg(feature = "rayon")]
let buffer_iter = buffer.par_iter_mut(); let buffer_iter = buffer.par_iter_mut();
@ -173,7 +178,7 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
point -= HALF; point -= HALF;
point *= camera[2]; point *= camera[2];
point += HALF; point += HALF;
let n = sample_world(point+camera_vec+boat_pos, rand); let n = sample_world(point+camera_vec+boat.get_pos(), rand);
*pix.1 = *pix.1 =
if n > 0.1 { if n > 0.1 {
let n = (n+0.1) * 300.0; let n = (n+0.1) * 300.0;