smolworld / src /js /physics.js
p3nGu1nZz's picture
✨ Refactor physics module; add math utilities and event handling classes; update state structure for 3D entities
7ffaa9e
raw
history blame contribute delete
944 Bytes
import { state } from './state.js';
import { normalizeAngle } from './math.js';
export class Physics {
constructor(config) {
this.config = config;
state.angularAcceleration = config.angularAcceleration;
}
update(deltaTime) {
// Update angular velocity
state.angularSpeed += state.angularAcceleration * deltaTime;
state.angle += state.angularSpeed * deltaTime;
state.angle = normalizeAngle(state.angle);
// Calculate position
state.squareX = state.canvasWidth / 2 + Math.cos(state.angle) * this.config.radius;
state.squareY = state.canvasHeight / 2 + Math.sin(state.angle) * this.config.radius;
// Calculate actual velocity components in pixels per second
state.velocity.x = -Math.sin(state.angle) * state.angularSpeed * this.config.radius;
state.velocity.y = Math.cos(state.angle) * state.angularSpeed * this.config.radius;
}
}