PHYSICS/physics.ts
2024-12-12 22:07:59 +03:00

75 lines
1.8 KiB
TypeScript

import IParams from "./IParams.ts";
import IPoint from "./IPoint.ts";
function getV0(params: IParams) {
return Math.sqrt(2 * params.g * params.h);
}
function getCollisionPoints(params: IParams): IPoint[] {
const interpolationPoints = interpolation(params);
const points: IPoint[] = [];
for (let i = 0; i < interpolationPoints.length - 1; i++) {
if (interpolationPoints[i].angle - interpolationPoints[i+1].angle < 0) {
points.push(interpolationPoints[i]);
}
}
return [{x: 0, y: 0, angle: 0, velocity: 0, time: 0}, ...points];
}
function interpolation(params: IParams): IPoint[] {
const T = (2 * getV0(params)) / params.g;
const points: IPoint[] = [];
let lastX = 0;
let i = 0;
while (lastX*Math.cos(params.alpha) < params.l) {
const t = i / 100;
const vx = getV0(params)*Math.sin(params.alpha) + params.g*Math.sin(params.alpha)*t;
const vy = getV0(params)*Math.cos(params.alpha) - params.g*Math.cos(params.alpha)*(t % T);
lastX = getV0(params) * Math.sin(params.alpha) * t + (params.g * Math.sin(params.alpha) * t ** 2) / 2;
points.push({
x: lastX,
y: getV0(params) * Math.cos(params.alpha) * (t % T) - (params.g * Math.cos(params.alpha) * (t % T) ** 2) / 2,
time: t,
velocity: Math.sqrt(vx*vx + vy*vy),
angle: Math.atan2(vy, vx)
});
i++;
}
return points;
}
function firstPoint(params: IParams): IPoint[] {
const points: IPoint[] = [
{
x: 0,
y: 0,
time: 0,
angle: -Math.PI / 2,
velocity: 0
}
];
let t = 0.01;
while(points[points.length - 1].y >= -params.h) {
points.push({
x: 0,
y: -params.g*t**2,
time: t,
angle: -Math.PI / 2,
velocity: -params.g*t
})
t += 0.01;
}
return points.slice(0, points.length-1);
}
export { getCollisionPoints, interpolation, firstPoint };