blob: ec81419b1e9beb3471ecc515bfd34d11c85fa741 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import { Ball } from "../state";
import { VELOCITY_SCALING_FACTOR } from "./const";
export const applyBallVelocity = (ball: Ball): Ball => {
let newPos = ball.position.map(
(a, i) => a + ball.velocity[i] * VELOCITY_SCALING_FACTOR
) as [number, number];
return { ...ball, position: newPos };
};
export const applyBallBounce = (ball: Ball, normal: [number, number]): Ball => {
//calculate reflection
const newVelocity = ball.velocity.map(
(v, i) => v - v * Math.abs(normal[i]) * 2
) as [number, number];
//move the ball out of the collider
const newPos = ball.position.map((p, i) => p + normal[i]) as [number, number];
//this gives a little punch
let newBall = { velocity: newVelocity, position: newPos };
newBall = applyBallVelocity(newBall);
return newBall;
};
|