Fix physics
This commit is contained in:
parent
37954e2ef1
commit
f9f6825776
51 changed files with 832 additions and 541 deletions
|
|
@ -2,25 +2,22 @@ import { vec2 } from 'gl-matrix';
|
|||
import {
|
||||
id,
|
||||
CharacterBase,
|
||||
StepCommand,
|
||||
settings,
|
||||
CommandExecutors,
|
||||
MoveActionCommand,
|
||||
serializesTo,
|
||||
clamp,
|
||||
last,
|
||||
Circle,
|
||||
} from 'shared';
|
||||
import { DynamicPhysical } from '../physics/conatiners/dynamic-physical';
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
import { CirclePhysical } from './circle-physical';
|
||||
import { Physical } from '../physics/physical';
|
||||
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { Spring } from './spring';
|
||||
|
||||
@serializesTo(CharacterBase)
|
||||
export class CharacterPhysical extends CharacterBase implements Physical {
|
||||
export class CharacterPhysical extends CharacterBase implements DynamicPhysical {
|
||||
public readonly canCollide = true;
|
||||
public readonly isInverted = false;
|
||||
public readonly canMove = true;
|
||||
|
||||
private jumpEnergyLeft = settings.defaultJumpEnergy;
|
||||
|
|
@ -32,17 +29,19 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
private movementActions: Array<MoveActionCommand> = [];
|
||||
private lastMovementAction: MoveActionCommand = new MoveActionCommand(vec2.create());
|
||||
|
||||
protected commandExecutors: CommandExecutors = {
|
||||
[StepCommand.type]: this.step.bind(this),
|
||||
[MoveActionCommand.type]: (c: MoveActionCommand) => this.movementActions.push(c),
|
||||
};
|
||||
public handleMovementAction(c: MoveActionCommand) {
|
||||
this.movementActions.push(c);
|
||||
}
|
||||
|
||||
private static readonly headOffset = vec2.fromValues(0, 40);
|
||||
private static readonly leftFootOffset = vec2.fromValues(-20, -35);
|
||||
private static readonly rightFootOffset = vec2.fromValues(20, -35);
|
||||
|
||||
constructor(private readonly container: PhysicalContainer) {
|
||||
super(id());
|
||||
constructor(
|
||||
public readonly colorIndex: number,
|
||||
private readonly container: PhysicalContainer,
|
||||
) {
|
||||
super(id(), colorIndex);
|
||||
this.head = new CirclePhysical(
|
||||
vec2.clone(CharacterPhysical.headOffset),
|
||||
50,
|
||||
|
|
@ -76,7 +75,7 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
return this._boundingBox;
|
||||
}
|
||||
|
||||
public get gameObject(): CharacterPhysical {
|
||||
public get gameObject(): this {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -84,6 +83,10 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
return this.head.center;
|
||||
}
|
||||
|
||||
public get velocity(): vec2 {
|
||||
return this.head.velocity;
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return (
|
||||
Math.min(
|
||||
|
|
@ -113,11 +116,11 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
return direction;
|
||||
}
|
||||
|
||||
public step(c: StepCommand) {
|
||||
const deltaTime = c.deltaTimeInMiliseconds / 1000;
|
||||
|
||||
public step(deltaTimeInMiliseconds: number) {
|
||||
const deltaTime = deltaTimeInMiliseconds / 1000;
|
||||
const direction = this.sumAndResetMovementActions();
|
||||
const isAirborne = this.leftFoot.isAirborne && this.rightFoot.isAirborne;
|
||||
const feetAirborne = this.leftFoot.isAirborne && this.rightFoot.isAirborne;
|
||||
const isAirborne = feetAirborne && this.head.isAirborne;
|
||||
this.jumpEnergyLeft += isAirborne ? -deltaTime : deltaTime;
|
||||
this.jumpEnergyLeft = clamp(this.jumpEnergyLeft, 0, settings.defaultJumpEnergy);
|
||||
|
||||
|
|
@ -129,52 +132,80 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
vec2.fromValues(xMax, yMax),
|
||||
);
|
||||
|
||||
const sumBody = vec2.add(vec2.create(), this.head.center, this.leftFoot.center);
|
||||
vec2.add(sumBody, sumBody, this.rightFoot.center);
|
||||
vec2.scale(sumBody, sumBody, 1 / 3);
|
||||
|
||||
const headPosition = vec2.add(vec2.create(), sumBody, CharacterPhysical.headOffset);
|
||||
|
||||
Spring.step(new Circle(headPosition, 0), this.head, 0, 30, deltaTime);
|
||||
|
||||
const footDistance = vec2.distance(
|
||||
CharacterPhysical.headOffset,
|
||||
CharacterPhysical.leftFootOffset,
|
||||
);
|
||||
|
||||
Spring.step(
|
||||
new Circle(this.head.center, this.head.radius),
|
||||
this.leftFoot,
|
||||
footDistance,
|
||||
25,
|
||||
deltaTime,
|
||||
);
|
||||
Spring.step(
|
||||
new Circle(this.head.center, this.head.radius),
|
||||
this.rightFoot,
|
||||
footDistance,
|
||||
25,
|
||||
deltaTime,
|
||||
);
|
||||
Spring.step(
|
||||
this.leftFoot,
|
||||
this.rightFoot,
|
||||
vec2.distance(CharacterPhysical.leftFootOffset, CharacterPhysical.rightFootOffset),
|
||||
100,
|
||||
300,
|
||||
deltaTime,
|
||||
);
|
||||
|
||||
this.head.applyForce(movementForce, deltaTime);
|
||||
this.leftFoot.applyForce(movementForce, deltaTime);
|
||||
this.rightFoot.applyForce(movementForce, deltaTime);
|
||||
this.applyForce(this.head, movementForce, deltaTime);
|
||||
this.applyForce(this.leftFoot, movementForce, deltaTime);
|
||||
this.applyForce(this.rightFoot, movementForce, deltaTime);
|
||||
|
||||
this.head.applyForce(settings.gravitationalForce, deltaTime);
|
||||
this.leftFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
this.rightFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
if (feetAirborne) {
|
||||
this.applyForce(this.head, settings.gravitationalForce, deltaTime);
|
||||
}
|
||||
this.applyForce(this.leftFoot, settings.gravitationalForce, deltaTime);
|
||||
this.applyForce(this.rightFoot, settings.gravitationalForce, deltaTime);
|
||||
|
||||
this.head.step(deltaTime);
|
||||
this.leftFoot.step(deltaTime);
|
||||
this.rightFoot.step(deltaTime);
|
||||
this.head.step2(deltaTime);
|
||||
this.leftFoot.step2(deltaTime);
|
||||
this.rightFoot.step2(deltaTime);
|
||||
|
||||
let sumBody = vec2.add(vec2.create(), this.head.center, this.leftFoot.center);
|
||||
vec2.add(sumBody, sumBody, this.rightFoot.center);
|
||||
vec2.scale(sumBody, sumBody, 1 / 3);
|
||||
|
||||
const headPosition = vec2.add(vec2.create(), sumBody, CharacterPhysical.headOffset);
|
||||
const headDelta = vec2.subtract(headPosition, headPosition, this.head.center);
|
||||
vec2.scale(headDelta, headDelta, 0.5);
|
||||
this.head.tryMove(headDelta);
|
||||
|
||||
sumBody = vec2.add(vec2.create(), this.head.center, this.leftFoot.center);
|
||||
vec2.add(sumBody, sumBody, this.rightFoot.center);
|
||||
vec2.scale(sumBody, sumBody, 1 / 3);
|
||||
|
||||
const leftFootPosition = vec2.add(
|
||||
vec2.create(),
|
||||
sumBody,
|
||||
CharacterPhysical.leftFootOffset,
|
||||
);
|
||||
const leftFootDelta = vec2.subtract(
|
||||
leftFootPosition,
|
||||
leftFootPosition,
|
||||
this.leftFoot.center,
|
||||
);
|
||||
vec2.scale(leftFootDelta, leftFootDelta, 1);
|
||||
this.leftFoot.tryMove(leftFootDelta);
|
||||
|
||||
const rightFootPosition = vec2.add(
|
||||
vec2.create(),
|
||||
sumBody,
|
||||
CharacterPhysical.rightFootOffset,
|
||||
);
|
||||
const rightFootDelta = vec2.subtract(
|
||||
rightFootPosition,
|
||||
rightFootPosition,
|
||||
this.rightFoot.center,
|
||||
);
|
||||
vec2.scale(rightFootDelta, rightFootDelta, 1);
|
||||
this.rightFoot.tryMove(rightFootDelta);
|
||||
}
|
||||
|
||||
public applyForce(circle: CirclePhysical, force: vec2, timeInSeconds: number) {
|
||||
vec2.add(
|
||||
circle.velocity,
|
||||
circle.velocity,
|
||||
vec2.scale(vec2.create(), force, timeInSeconds),
|
||||
);
|
||||
|
||||
vec2.set(
|
||||
circle.velocity,
|
||||
clamp(circle.velocity.x, -settings.maxVelocityX, settings.maxVelocityX),
|
||||
clamp(circle.velocity.y, -settings.maxVelocityY, settings.maxVelocityY),
|
||||
);
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { Circle, clamp, GameObject, serializesTo, settings } from 'shared';
|
||||
import { Circle, GameObject, serializesTo, settings } from 'shared';
|
||||
import { Physical } from '../physics/physical';
|
||||
|
||||
import { BoundingBox } from '../physics/bounding-boxes/bounding-box';
|
||||
import { BoundingBoxBase } from '../physics/bounding-boxes/bounding-box-base';
|
||||
import { moveCircle } from '../physics/move-circle';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { DynamicPhysical } from '../physics/conatiners/dynamic-physical';
|
||||
|
||||
@serializesTo(Circle)
|
||||
export class CirclePhysical implements Circle, Physical {
|
||||
readonly isInverted = false;
|
||||
export class CirclePhysical implements Circle, DynamicPhysical {
|
||||
readonly canCollide = true;
|
||||
readonly canMove = true;
|
||||
|
||||
private _isAirborne = true;
|
||||
private velocity = vec2.create();
|
||||
public velocity = vec2.create();
|
||||
|
||||
public get isAirborne(): boolean {
|
||||
return this._isAirborne;
|
||||
|
|
@ -27,6 +27,7 @@ export class CirclePhysical implements Circle, Physical {
|
|||
private _radius: number,
|
||||
public owner: GameObject,
|
||||
private readonly container: PhysicalContainer,
|
||||
private restitution = 0,
|
||||
) {
|
||||
this._boundingBox = new BoundingBox();
|
||||
this.recalculateBoundingBox();
|
||||
|
|
@ -74,19 +75,6 @@ export class CirclePhysical implements Circle, Physical {
|
|||
return other.distance(this.center) < -this.radius;
|
||||
}
|
||||
|
||||
public getPerimeterPoints(count: number): Array<vec2> {
|
||||
const result: Array<vec2> = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
result.push(
|
||||
vec2.fromValues(
|
||||
Math.cos((2 * Math.PI * i) / count) * this.radius + this.center.x,
|
||||
Math.sin((2 * Math.PI * i) / count) * this.radius + this.center.y,
|
||||
),
|
||||
);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private recalculateBoundingBox() {
|
||||
this._boundingBox.xMin = this.center.x - this._radius;
|
||||
this._boundingBox.xMax = this.center.x + this._radius;
|
||||
|
|
@ -100,48 +88,75 @@ export class CirclePhysical implements Circle, Physical {
|
|||
this.velocity,
|
||||
vec2.scale(vec2.create(), force, timeInSeconds),
|
||||
);
|
||||
|
||||
vec2.set(
|
||||
this.velocity,
|
||||
clamp(this.velocity.x, -settings.maxVelocityX, settings.maxVelocityX),
|
||||
clamp(this.velocity.y, -settings.maxVelocityY, settings.maxVelocityY),
|
||||
);
|
||||
}
|
||||
|
||||
public resetVelocity() {
|
||||
this.velocity = vec2.create();
|
||||
}
|
||||
public step(deltaTime: number) {}
|
||||
|
||||
public step(deltaTimeInSeconds: number): boolean {
|
||||
public step2(deltaTimeInSeconds: number): boolean {
|
||||
vec2.scale(
|
||||
this.velocity,
|
||||
this.velocity,
|
||||
Math.pow(settings.velocityAttenuation, deltaTimeInSeconds),
|
||||
);
|
||||
|
||||
const distance = vec2.scale(vec2.create(), this.velocity, deltaTimeInSeconds);
|
||||
const delta = vec2.scale(vec2.create(), this.velocity, deltaTimeInSeconds);
|
||||
|
||||
const distanceLength = vec2.length(distance);
|
||||
const stepCount = Math.ceil(distanceLength / settings.physicsMaxStep);
|
||||
vec2.scale(distance, distance, 1 / stepCount);
|
||||
const stepCount = Math.ceil(vec2.length(delta) / settings.physicsMaxStep);
|
||||
vec2.scale(delta, delta, 1 / stepCount);
|
||||
|
||||
let wasHit = false;
|
||||
|
||||
for (let i = 0; i < stepCount; i++) {
|
||||
const { tangent, hitSurface } = moveCircle(
|
||||
this,
|
||||
vec2.clone(distance),
|
||||
this.container.findIntersecting(this.boundingBox),
|
||||
const distance = vec2.scale(
|
||||
vec2.create(),
|
||||
this.velocity,
|
||||
deltaTimeInSeconds / stepCount,
|
||||
);
|
||||
this.radius += vec2.length(distance);
|
||||
const intersecting = this.container.findIntersecting(this.boundingBox);
|
||||
this.radius -= vec2.length(distance);
|
||||
|
||||
const { normal, hitSurface } = moveCircle(this, vec2.clone(distance), intersecting);
|
||||
|
||||
if (hitSurface) {
|
||||
vec2.scale(this.velocity, tangent!, vec2.dot(tangent!, this.velocity));
|
||||
if (
|
||||
vec2.length(this.velocity) <
|
||||
settings.frictionMinVelocity * deltaTimeInSeconds
|
||||
) {
|
||||
this.velocity = vec2.create();
|
||||
}
|
||||
vec2.subtract(
|
||||
this.velocity,
|
||||
this.velocity,
|
||||
vec2.scale(
|
||||
normal!,
|
||||
normal!,
|
||||
(1 + this.restitution) * vec2.dot(normal!, this.velocity),
|
||||
),
|
||||
);
|
||||
|
||||
wasHit = true;
|
||||
}
|
||||
}
|
||||
|
||||
this._isAirborne = !wasHit;
|
||||
return wasHit;
|
||||
}
|
||||
|
||||
public tryMove(delta: vec2) {
|
||||
const stepCount = Math.ceil(vec2.length(delta) / settings.physicsMaxStep);
|
||||
vec2.scale(delta, delta, 1 / stepCount);
|
||||
|
||||
let wasHit = false;
|
||||
|
||||
for (let i = 0; i < stepCount; i++) {
|
||||
this.radius += vec2.length(delta);
|
||||
const intersecting = this.container.findIntersecting(this.boundingBox);
|
||||
this.radius -= vec2.length(delta);
|
||||
|
||||
const { normal, hitSurface } = moveCircle(this, vec2.clone(delta), intersecting);
|
||||
|
||||
if (hitSurface) {
|
||||
vec2.subtract(
|
||||
delta,
|
||||
delta,
|
||||
vec2.scale(normal!, normal!, vec2.dot(normal!, delta)),
|
||||
);
|
||||
|
||||
wasHit = true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,11 @@ import { vec2, vec3 } from 'gl-matrix';
|
|||
import { LampBase, settings, id, serializesTo } from 'shared';
|
||||
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
|
||||
import { Physical } from '../physics/physical';
|
||||
import { StaticPhysical } from '../physics/containers/static-physical';
|
||||
|
||||
@serializesTo(LampBase)
|
||||
export class LampPhysical extends LampBase implements Physical {
|
||||
export class LampPhysical extends LampBase implements StaticPhysical {
|
||||
public readonly canCollide = false;
|
||||
public readonly isInverted = false;
|
||||
public readonly canMove = false;
|
||||
|
||||
constructor(center: vec2, color: vec3, lightness: number) {
|
||||
|
|
@ -30,12 +28,11 @@ export class LampPhysical extends LampBase implements Physical {
|
|||
return this._boundingBox;
|
||||
}
|
||||
|
||||
public get gameObject(): LampPhysical {
|
||||
public get gameObject(): this {
|
||||
return this;
|
||||
}
|
||||
|
||||
// todo
|
||||
public distance(_: vec2): number {
|
||||
return 0;
|
||||
public distance(target: vec2): number {
|
||||
return vec2.distance(this.center, target);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
67
backend/src/objects/projectile-physical.ts
Normal file
67
backend/src/objects/projectile-physical.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import {
|
||||
id,
|
||||
settings,
|
||||
serializesTo,
|
||||
ProjectileBase,
|
||||
GameObject,
|
||||
rotate90Deg,
|
||||
} from 'shared';
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
import { CirclePhysical } from './circle-physical';
|
||||
import { DynamicPhysical } from '../physics/conatiners/dynamic-physical';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
|
||||
@serializesTo(ProjectileBase)
|
||||
export class ProjectilePhysical extends ProjectileBase implements DynamicPhysical {
|
||||
public readonly canCollide = true;
|
||||
public readonly canMove = true;
|
||||
|
||||
public object: CirclePhysical;
|
||||
|
||||
constructor(
|
||||
center: vec2,
|
||||
radius: number,
|
||||
private velocity: vec2,
|
||||
readonly container: PhysicalContainer,
|
||||
) {
|
||||
super(id(), center, radius);
|
||||
this.object = new CirclePhysical(center, radius, this, container, 0.9);
|
||||
}
|
||||
|
||||
private _boundingBox?: ImmutableBoundingBox;
|
||||
|
||||
public get boundingBox(): ImmutableBoundingBox {
|
||||
if (!this._boundingBox) {
|
||||
this._boundingBox = (this.object as CirclePhysical).boundingBox;
|
||||
}
|
||||
|
||||
return this._boundingBox;
|
||||
}
|
||||
|
||||
public get gameObject(): this {
|
||||
return this;
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return this.object.distance(target);
|
||||
}
|
||||
|
||||
public onCollision(normal: vec2, other: GameObject) {
|
||||
const tangent = rotate90Deg(normal);
|
||||
vec2.scale(this.velocity, tangent, vec2.dot(tangent, this.velocity));
|
||||
}
|
||||
|
||||
public step(deltaTimeInMiliseconds: number) {
|
||||
const deltaTime = deltaTimeInMiliseconds / 1000;
|
||||
|
||||
vec2.add(
|
||||
this.velocity,
|
||||
this.velocity,
|
||||
vec2.scale(vec2.create(), settings.gravitationalForce, deltaTime),
|
||||
);
|
||||
|
||||
this.object.velocity = this.velocity;
|
||||
this.object.step2(deltaTime);
|
||||
}
|
||||
}
|
||||
78
backend/src/objects/stone-physical.ts
Normal file
78
backend/src/objects/stone-physical.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { clamp01, id, serializesTo, StoneBase } from 'shared';
|
||||
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
import { StaticPhysical } from '../physics/containers/static-physical';
|
||||
|
||||
@serializesTo(StoneBase)
|
||||
export class StonePhysical extends StoneBase implements StaticPhysical {
|
||||
public readonly canCollide = true;
|
||||
public readonly canMove = false;
|
||||
|
||||
private _boundingBox?: ImmutableBoundingBox;
|
||||
|
||||
constructor(vertices: Array<vec2>) {
|
||||
super(id(), vertices);
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
const startEnd = this.vertices[0];
|
||||
let vb = startEnd;
|
||||
|
||||
let d = vec2.squaredDistance(target, vb);
|
||||
let sign = 1;
|
||||
|
||||
for (let i = 1; i <= this.vertices.length; i++) {
|
||||
const va = vb;
|
||||
vb = i === this.vertices.length ? startEnd : this.vertices[i];
|
||||
const targetFromDelta = vec2.subtract(vec2.create(), target, va);
|
||||
const toFromDelta = vec2.subtract(vec2.create(), vb, va);
|
||||
const h = clamp01(
|
||||
vec2.dot(targetFromDelta, toFromDelta) / vec2.squaredLength(toFromDelta),
|
||||
);
|
||||
|
||||
const ds = vec2.fromValues(
|
||||
vec2.dist(targetFromDelta, vec2.scale(vec2.create(), toFromDelta, h)),
|
||||
toFromDelta.x * targetFromDelta.y - toFromDelta.y * targetFromDelta.x,
|
||||
);
|
||||
|
||||
if (
|
||||
(target.y >= va.y && target.y < vb.y && ds.y > 0) ||
|
||||
(target.y < va.y && target.y >= vb.y && ds.y <= 0)
|
||||
) {
|
||||
sign *= -1;
|
||||
}
|
||||
|
||||
d = Math.min(d, ds.x);
|
||||
}
|
||||
|
||||
return sign * d;
|
||||
}
|
||||
|
||||
public get boundingBox(): ImmutableBoundingBox {
|
||||
if (!this._boundingBox) {
|
||||
const { xMin, xMax, yMin, yMax } = this.vertices.reduce(
|
||||
(extremities, vertex) => ({
|
||||
xMin: Math.min(extremities.xMin, vertex.x),
|
||||
xMax: Math.max(extremities.xMax, vertex.x),
|
||||
yMin: Math.min(extremities.yMin, vertex.y),
|
||||
yMax: Math.max(extremities.yMax, vertex.y),
|
||||
}),
|
||||
{
|
||||
xMin: Infinity,
|
||||
xMax: -Infinity,
|
||||
yMin: Infinity,
|
||||
yMax: -Infinity,
|
||||
},
|
||||
);
|
||||
|
||||
this._boundingBox = new ImmutableBoundingBox(xMin, xMax, yMin, yMax);
|
||||
}
|
||||
|
||||
return this._boundingBox;
|
||||
}
|
||||
|
||||
public get gameObject(): this {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { clamp01, mix, TunnelBase, id, serializesTo } from 'shared';
|
||||
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
import { StaticPhysical } from '../physics/containers/static-physical-object';
|
||||
|
||||
@serializesTo(TunnelBase)
|
||||
export class TunnelPhysical extends TunnelBase implements StaticPhysical {
|
||||
public readonly canCollide = true;
|
||||
public readonly isInverted = true;
|
||||
public readonly canMove = false;
|
||||
|
||||
private _boundingBox?: ImmutableBoundingBox;
|
||||
|
||||
constructor(from: vec2, to: vec2, fromRadius: number, toRadius: number) {
|
||||
super(id(), from, to, fromRadius, toRadius);
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
const toFromDelta = vec2.subtract(vec2.create(), this.to, this.from);
|
||||
const targetFromDelta = vec2.subtract(vec2.create(), target, this.from);
|
||||
|
||||
const h = clamp01(
|
||||
vec2.dot(targetFromDelta, toFromDelta) / vec2.dot(toFromDelta, toFromDelta),
|
||||
);
|
||||
|
||||
return (
|
||||
vec2.distance(targetFromDelta, vec2.scale(vec2.create(), toFromDelta, h)) -
|
||||
mix(this.fromRadius, this.toRadius, h)
|
||||
);
|
||||
}
|
||||
|
||||
public get boundingBox(): ImmutableBoundingBox {
|
||||
if (!this._boundingBox) {
|
||||
const xMin = Math.min(this.from.x - this.fromRadius, this.to.x - this.toRadius);
|
||||
const yMin = Math.min(this.from.y - this.fromRadius, this.to.y - this.toRadius);
|
||||
const xMax = Math.max(this.from.x + this.fromRadius, this.to.x + this.toRadius);
|
||||
const yMax = Math.max(this.from.y + this.fromRadius, this.to.y + this.toRadius);
|
||||
this._boundingBox = new ImmutableBoundingBox(xMin, xMax, yMin, yMax);
|
||||
}
|
||||
|
||||
return this._boundingBox;
|
||||
}
|
||||
|
||||
public get gameObject(): TunnelPhysical {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue