Fix inv sqrt NaN issue
This commit is contained in:
+20
-28
@@ -8,17 +8,11 @@ export function createVector(x: number = 0, y: number = 0): Vector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function copyVector(vector: Vector): Vector {
|
export function copyVector(vector: Vector): Vector {
|
||||||
return {
|
return { x: vector.x, y: vector.y };
|
||||||
x: vector.x,
|
|
||||||
y: vector.y,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fromPolar(angle: number, magnitude: number): Vector {
|
export function fromPolar(angle: number, mag: number): Vector {
|
||||||
return {
|
return { x: Math.cos(angle) * mag, y: Math.sin(angle) * mag };
|
||||||
x: Math.cos(angle) * magnitude,
|
|
||||||
y: Math.sin(angle) * magnitude,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAngle(vector: Vector): number {
|
export function getAngle(vector: Vector): number {
|
||||||
@@ -36,33 +30,23 @@ export function getDistance(a: Vector, b: Vector): number {
|
|||||||
return Math.sqrt(dx * dx + dy * dy);
|
return Math.sqrt(dx * dx + dy * dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDistanceSquared(a: Vector, b: Vector): number {
|
export function getDistanceSqrt(a: Vector, b: Vector): number {
|
||||||
const dx = a.x - b.x;
|
const dx = a.x - b.x;
|
||||||
const dy = a.y - b.y;
|
const dy = a.y - b.y;
|
||||||
|
|
||||||
return dx * dx + dy * dy;
|
return dx * dx + dy * dy;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSquared(vector: Vector): number {
|
export function getSqrt(vector: Vector): number {
|
||||||
return vector.x * vector.x + vector.y * vector.y;
|
return vector.x * vector.x + vector.y * vector.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setUnit(unit: Vector, a: Vector, b: Vector): void {
|
export function setUnit(unit: Vector, a: Vector, b: Vector): void {
|
||||||
const dx = a.x - b.x;
|
invScale(unit, a.x - b.x, a.y - b.y);
|
||||||
const dy = a.y - b.y;
|
|
||||||
const inv = 1.0 / Math.sqrt(dx * dx + dy * dy);
|
|
||||||
|
|
||||||
unit.x = dx * inv;
|
|
||||||
unit.y = dy * inv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setUnitNormal(unit: Vector, a: Vector, b: Vector): void {
|
export function setUnitNormal(unit: Vector, a: Vector, b: Vector): void {
|
||||||
const nx = a.y - b.y;
|
invScale(unit, a.y - b.y, b.x - a.x);
|
||||||
const ny = b.x - a.x;
|
|
||||||
const inv = 1.0 / Math.sqrt(nx * nx + ny * ny);
|
|
||||||
|
|
||||||
unit.x = nx * inv;
|
|
||||||
unit.y = ny * inv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDotProduct(a: Vector, b: Vector): number {
|
export function getDotProduct(a: Vector, b: Vector): number {
|
||||||
@@ -79,17 +63,25 @@ export function scale(vector: Vector, scalar: number): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function normalize(vector: Vector): void {
|
export function normalize(vector: Vector): void {
|
||||||
const inv = 1.0 / Math.sqrt(vector.x * vector.x + vector.y * vector.y);
|
invScale(vector, vector.x, vector.y);
|
||||||
vector.x *= inv;
|
}
|
||||||
vector.y *= inv;
|
|
||||||
|
export function invScale(vector: Vector, x: number, y: number): void {
|
||||||
|
if (x === 0 && y === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const inv = 1.0 / Math.sqrt(x * x + y * y);
|
||||||
|
vector.x = x * inv;
|
||||||
|
vector.y = y * inv;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isZero(vector: Vector): boolean {
|
export function isZero(vector: Vector): boolean {
|
||||||
return vector.x === 0 && vector.y === 0;
|
return vector.x === 0 && vector.y === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isBelowThreshold(vector: Vector, threshold: number): boolean {
|
export function isBelowAbs(vector: Vector, abs: number): boolean {
|
||||||
return Math.abs(vector.x) < threshold && Math.abs(vector.y) < threshold;
|
return Math.abs(vector.x) < abs && Math.abs(vector.y) < abs;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function equals(a: Vector, b: Vector): boolean {
|
export function equals(a: Vector, b: Vector): boolean {
|
||||||
|
|||||||
@@ -13,7 +13,12 @@ import {
|
|||||||
moveAndCollide,
|
moveAndCollide,
|
||||||
Shape,
|
Shape,
|
||||||
} from "#/engine/system/physics.ts";
|
} from "#/engine/system/physics.ts";
|
||||||
import { createVector, type Vector } from "#/engine/Vector.ts";
|
import {
|
||||||
|
createVector,
|
||||||
|
normalize,
|
||||||
|
scale,
|
||||||
|
type Vector,
|
||||||
|
} from "#/engine/Vector.ts";
|
||||||
import charIdle from "#/game/assets/char-idle.png";
|
import charIdle from "#/game/assets/char-idle.png";
|
||||||
|
|
||||||
const speed = 0.05;
|
const speed = 0.05;
|
||||||
@@ -36,10 +41,12 @@ process((id, delta) => {
|
|||||||
position.y = pointer.position.y;
|
position.y = pointer.position.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
velocity.x -= keyboardInput.arrowLeft ? speed : 0;
|
velocity.x -= keyboardInput.arrowLeft ? 1 : 0;
|
||||||
velocity.x += keyboardInput.arrowRight ? speed : 0;
|
velocity.x += keyboardInput.arrowRight ? 1 : 0;
|
||||||
velocity.y -= keyboardInput.arrowUp ? speed : 0;
|
velocity.y -= keyboardInput.arrowUp ? 1 : 0;
|
||||||
velocity.y += keyboardInput.arrowDown ? speed : 0;
|
velocity.y += keyboardInput.arrowDown ? 1 : 0;
|
||||||
|
normalize(velocity);
|
||||||
|
scale(velocity, speed);
|
||||||
|
|
||||||
position.x = Math.round(velocity.x * delta + position.x);
|
position.x = Math.round(velocity.x * delta + position.x);
|
||||||
position.y = Math.round(velocity.y * delta + position.y);
|
position.y = Math.round(velocity.y * delta + position.y);
|
||||||
|
|||||||
Reference in New Issue
Block a user