Stress-testing the physics engine

This commit is contained in:
2024-06-11 01:19:48 +02:00
parent 6d20041842
commit ca7b1d1956
7 changed files with 192 additions and 40 deletions

View File

@@ -1,5 +1,7 @@
import * as Three from 'three';
import { FRUSTUMSIZE } from '../consts';
import Stats from 'stats.js';
import { Point } from '../physics/common';
export default class Renderer
{
@@ -8,6 +10,11 @@ export default class Renderer
static renderer: Three.WebGLRenderer;
static camera: Three.OrthographicCamera;
static #zoom: number;
static #pos: Point = { x: 0, y: 0 };
static #stats: Stats;
static init(): Boolean
{
try {
@@ -19,6 +26,12 @@ export default class Renderer
this.camera = new Three.OrthographicCamera();
this.camera.position.z = 500;
this.#zoom = 1;
const stats = this.#stats = new Stats();
stats.showPanel(0);
document.body.appendChild(stats.dom);
this.#resize();
window.addEventListener("resize", this.#resize.bind(this));
@@ -30,15 +43,35 @@ export default class Renderer
return false;
}
}
static get zoom(): number
{
return this.#zoom;
}
static set zoom(v: number)
{
this.#zoom = v;
this.#resize();
}
static move(x: number, y: number): void
{
this.#pos.x += x;
this.#pos.y += y;
this.camera.position.x += x;
this.camera.position.y += y;
}
static screenSpaceToCameraSpace(x: number, y: number, offset: boolean): Point {
return { x: ((x / window.innerWidth - 0.5) * FRUSTUMSIZE * this.aspect - (offset ? this.#pos.x : 0)) / this.#zoom, y: (- (y / window.innerHeight - 0.5) * FRUSTUMSIZE - (offset ? this.#pos.x : 0)) / this.zoom };
}
static #resize(): void
{
const aspect = this.aspect = window.innerWidth / window.innerHeight;
this.renderer.setSize( window.innerWidth, window.innerHeight );
this.camera.left = FRUSTUMSIZE * aspect / - 2;
this.camera.right = FRUSTUMSIZE * aspect / 2;
this.camera.top = FRUSTUMSIZE / 2;
this.camera.bottom = FRUSTUMSIZE / - 2;
this.camera.left = FRUSTUMSIZE * aspect / - 2 / this.#zoom;
this.camera.right = FRUSTUMSIZE * aspect / 2 / this.#zoom;
this.camera.top = FRUSTUMSIZE / 2 / this.#zoom;
this.camera.bottom = FRUSTUMSIZE / - 2 / this.#zoom;
this.camera.updateProjectionMatrix();
@@ -46,7 +79,9 @@ export default class Renderer
}
static render(delta?: number): void
{
this.#stats.begin();
this.renderer.render(this.scene, this.camera);
this.#stats.end();
}
static startRendering(): void
{