File size: 944 Bytes
7e312e3
7ffaa9e
7e312e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
    }
}