fix pathfinding nekimegyafalnak style
This commit is contained in:
+33
-17
@@ -49,24 +49,24 @@ export default class SATX {
|
||||
return 1 / Math.cos(Math.PI / numberOfVertices)
|
||||
}
|
||||
|
||||
static entityTunnel(from, to, radius = 0) {
|
||||
static entityTunnel(fromX, fromY, toX, toY, radius = 0) {
|
||||
if (radius <= 0) {
|
||||
return this.line(from, to)
|
||||
return this.line(fromX, fromY, toX, toY)
|
||||
}
|
||||
|
||||
const length = to.clone().sub(from)
|
||||
const halfWidth = length.clone().normalize().multiplyScalar(radius).rotateAround(new Vector2(), Math.PI / 2)
|
||||
const width = halfWidth.clone().multiplyScalar(2)
|
||||
const sides = new Float32Array(5)
|
||||
sides[0] = toX - fromX
|
||||
sides[1] = toY - fromY
|
||||
sides[4] = Math.hypot(sides[0], sides[1])
|
||||
sides[2] = (sides[1] / sides[4]) * -radius // optimization: negation and swapping rotates
|
||||
sides[3] = (sides[0] / sides[4]) * radius
|
||||
|
||||
const origin = from.clone().sub(halfWidth)
|
||||
const satPoints = [
|
||||
new SAT.Vector(...origin.toArray()),
|
||||
new SAT.Vector(...from.clone().sub(halfWidth).add(length).sub(origin).toArray()),
|
||||
new SAT.Vector(...from.clone().sub(halfWidth).add(length.clone().add(width)).sub(origin).toArray()),
|
||||
new SAT.Vector(...from.clone().sub(halfWidth).add(width).sub(origin).toArray()),
|
||||
]
|
||||
|
||||
return new SAT.Polygon(satPoints[0], [new SAT.Vector(), ...satPoints.slice(1)])
|
||||
return new SAT.Polygon(new SAT.Vector(fromX - sides[2], fromY - sides[3]), [
|
||||
new SAT.Vector(),
|
||||
new SAT.Vector(sides[0], sides[1]),
|
||||
new SAT.Vector(sides[0] + (2 * sides[2]), sides[1] + (2 * sides[3])),
|
||||
new SAT.Vector(2 * sides[2], 2 * sides[3]),
|
||||
])
|
||||
}
|
||||
|
||||
static fixCollisions(entityPosition, colliders, radius = 0) {
|
||||
@@ -83,12 +83,28 @@ export default class SATX {
|
||||
return position
|
||||
}
|
||||
|
||||
static line(from, to) {
|
||||
return new SAT.Polygon(new SAT.Vector(...from.toArray()), [new SAT.Vector(), new SAT.Vector(...to.clone().sub(from).toArray())])
|
||||
static line(fromX, fromY, toX, toY) {
|
||||
return new SAT.Polygon(new SAT.Vector(fromX, fromY), [new SAT.Vector(), new SAT.Vector(toX - fromX, toY - fromY)])
|
||||
}
|
||||
|
||||
static polygonToThreeVector2(polygon) {
|
||||
static satPolygonToVectors(polygon) {
|
||||
const position = new Vector2(polygon.pos.x, polygon.pos.y)
|
||||
return polygon.points.map((p) => new Vector2(p.x, p.y).add(position))
|
||||
}
|
||||
|
||||
static vectorToFloat32Array(vector) {
|
||||
const array = new Float32Array(2)
|
||||
array[0] = vector.x
|
||||
array[1] = vector.y
|
||||
|
||||
return array
|
||||
}
|
||||
|
||||
static float32ArrayToVector(array) {
|
||||
return new Vector2(array[0], array[1])
|
||||
}
|
||||
|
||||
static float32ArrayWithIndexToVector(array, index) {
|
||||
return new Vector2(array[index], array[index + 1])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user