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; };