sc0ttj / component

A tiny library for isomorphic JavaScript components
MIT License
2 stars 1 forks source link

Feature: Game loop addon #39

Closed sc0ttj closed 2 years ago

sc0ttj commented 3 years ago

Do a "Game loop" addon - a proper fixed-time loop, with variable render loop.

See these links:

Game loops:

Libraries & Game engines:

Articles on proper game loops:

NOTES:

Dont use Euler loops, use semi-implicit Euler loops, or RK4..

Euler will crash with springs that don't end, RK4 is most accurate, but might be too "heavy"..

See https://gafferongames.com/post/integration_basics/ for explanation of these 3 types of loops.

sc0ttj commented 3 years ago

Ideally, to make this as generic, and reusable as possible, it should be done as a "ticker" ... something like so:

import { Component, onTick } from '@scottjarvis/component' 

Component.onTick = onTick;

const Game = new Component({ ... });

Game.onTick(props => {
  // the code here executes on a fixed-time loop
  // ..update your game state here  
});

Game.view = props => {
  // define what to draw to canvas here  
};

// render executes at a variable frame rate, namely as close to 60fps as possible
Game.render('.container')
sc0ttj commented 2 years ago

Done, closing.