Improve movement

This commit is contained in:
schmelczerandras 2020-10-07 21:05:24 +02:00
parent e01400058d
commit 9e35538b79
13 changed files with 164 additions and 125 deletions

View file

@ -7,10 +7,9 @@ import {
CommandExecutors,
MoveActionCommand,
serializesTo,
clamp,
} from 'shared';
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';
@ -21,6 +20,8 @@ export class CharacterPhysical extends CharacterBase implements Physical {
public readonly isInverted = false;
public readonly canMove = true;
private jumpEnergyLeft = settings.defaultJumpEnergy;
public head: CirclePhysical;
public leftFoot: CirclePhysical;
public rightFoot: CirclePhysical;
@ -37,27 +38,25 @@ export class CharacterPhysical extends CharacterBase implements Physical {
private static readonly rightFootOffset = vec2.fromValues(20, -10);
constructor(private readonly container: PhysicalContainer) {
super(
id(),
new CirclePhysical(vec2.clone(CharacterPhysical.headOffset), 50, null, container),
new CirclePhysical(
vec2.clone(CharacterPhysical.leftFootOffset),
20,
null,
container,
),
new CirclePhysical(
vec2.clone(CharacterPhysical.rightFootOffset),
20,
null,
container,
),
super(id(), undefined, undefined, undefined);
this.head = new CirclePhysical(
vec2.clone(CharacterPhysical.headOffset),
50,
this,
container,
);
this.leftFoot = new CirclePhysical(
vec2.clone(CharacterPhysical.leftFootOffset),
20,
this,
container,
);
this.rightFoot = new CirclePhysical(
vec2.clone(CharacterPhysical.rightFootOffset),
20,
this,
container,
);
this.head.owner = this;
this.leftFoot.owner = this;
this.rightFoot.owner = this;
container.addObject(this.head);
container.addObject(this.leftFoot);
container.addObject(this.rightFoot);
@ -77,9 +76,14 @@ export class CharacterPhysical extends CharacterBase implements Physical {
return this;
}
// todo
public distance(_: vec2): number {
return 0;
public distance(target: vec2): number {
return (
Math.min(
this.head.distance(target),
this.leftFoot.distance(target),
this.rightFoot.distance(target),
) - 20
);
}
private sumAndResetMovementActions(): vec2 {
@ -102,15 +106,32 @@ export class CharacterPhysical extends CharacterBase implements Physical {
public step(c: StepCommand) {
const deltaTime = c.deltaTimeInMiliseconds;
this.head.applyForce(settings.gravitationalForce, deltaTime);
this.leftFoot.applyForce(settings.gravitationalForce, deltaTime);
this.rightFoot.applyForce(settings.gravitationalForce, deltaTime);
const movementForce = this.sumAndResetMovementActions();
if (!this.leftFoot.isAirborne || !this.rightFoot.isAirborne) {
movementForce.y = 0;
const isAirborne = this.leftFoot.isAirborne && this.rightFoot.isAirborne;
if (isAirborne) {
this.jumpEnergyLeft -= deltaTime;
} else {
this.jumpEnergyLeft = settings.defaultJumpEnergy;
}
const xMax = deltaTime * settings.maxAccelerationX;
const yMax = this.jumpEnergyLeft > 0 ? deltaTime * settings.maxAccelerationY : 0;
vec2.set(
movementForce,
clamp(movementForce.x, -xMax, xMax),
clamp(movementForce.y, -yMax, yMax),
);
this.head.applyForce(movementForce, deltaTime);
this.head.applyForce(settings.gravitationalForce, deltaTime);
this.leftFoot.applyForce(movementForce, deltaTime);
this.rightFoot.applyForce(movementForce, deltaTime);
const bodyCenter = vec2.sub(
const bodyCenter = vec2.subtract(
vec2.create(),
this.head.center,
CharacterPhysical.headOffset,
@ -127,38 +148,35 @@ export class CharacterPhysical extends CharacterBase implements Physical {
CharacterPhysical.rightFootOffset,
);
const leftFootDelta = vec2.sub(vec2.create(), this.leftFoot.center, leftFootPositon);
const rightFootDelta = vec2.sub(
const leftFootDelta = vec2.subtract(
vec2.create(),
this.leftFoot.center,
leftFootPositon,
);
const rightFootDelta = vec2.subtract(
vec2.create(),
this.rightFoot.center,
rightFootPositon,
);
vec2.scale(leftFootDelta, leftFootDelta, 0.0006);
vec2.scale(rightFootDelta, rightFootDelta, 0.0006);
if (vec2.squaredLength(leftFootDelta) > 0) {
vec2.scale(leftFootDelta, leftFootDelta, 0.001);
this.head.applyForce(leftFootDelta, deltaTime);
vec2.scale(leftFootDelta, leftFootDelta, -4);
this.leftFoot.applyForce(leftFootDelta, deltaTime);
}
this.head.applyForce(leftFootDelta, deltaTime);
this.head.applyForce(rightFootDelta, deltaTime);
vec2.scale(leftFootDelta, leftFootDelta, -0.5);
vec2.scale(rightFootDelta, rightFootDelta, -0.5);
this.leftFoot.applyForce(movementForce, deltaTime);
this.rightFoot.applyForce(movementForce, deltaTime);
this.leftFoot.applyForce(leftFootDelta, deltaTime);
this.rightFoot.applyForce(rightFootDelta, deltaTime);
this.leftFoot.applyForce(settings.gravitationalForce, deltaTime);
this.rightFoot.applyForce(settings.gravitationalForce, deltaTime);
if (vec2.squaredLength(rightFootDelta) > 0) {
vec2.scale(rightFootDelta, rightFootDelta, 0.001);
this.head.applyForce(rightFootDelta, deltaTime);
vec2.scale(rightFootDelta, rightFootDelta, -4);
this.rightFoot.applyForce(rightFootDelta, deltaTime);
}
this.head.step(deltaTime);
this.leftFoot.step(deltaTime);
this.rightFoot.step(deltaTime);
/*this.flashlight.center = vec2.add(
vec2.create(),
this.head.center,
vec2.fromValues(0, 0)
);*/
}
public destroy() {

View file

@ -1,13 +1,5 @@
import { vec2 } from 'gl-matrix';
import {
Circle,
clamp,
CommandExecutors,
GameObject,
serializesTo,
settings,
StepCommand,
} from 'shared';
import { Circle, clamp, GameObject, serializesTo, settings } from 'shared';
import { Physical } from '../physics/physical';
import { BoundingBox } from '../physics/bounding-boxes/bounding-box';
@ -22,17 +14,12 @@ export class CirclePhysical implements Circle, Physical {
readonly canMove = true;
private _isAirborne = true;
private velocity = vec2.create();
public get isAirborne(): boolean {
return this._isAirborne;
}
protected commandExecutors: CommandExecutors = {
[StepCommand.type]: this.step.bind(this),
};
private _boundingBox: BoundingBox;
constructor(
@ -117,7 +104,7 @@ export class CirclePhysical implements Circle, Physical {
vec2.set(
this.velocity,
clamp(this.velocity.x, -settings.maxVelocityX, settings.maxVelocityX),
clamp(this.velocity.y, -settings.maxVelocityY, settings.maxVelocityY),
this.velocity.y,
);
}
@ -140,7 +127,7 @@ export class CirclePhysical implements Circle, Physical {
let wasHit = false;
for (let i = 0; i < stepCount; i++) {
const { tangent, hitSurface } = moveCircle(
const { normal, tangent, hitSurface } = moveCircle(
this,
distance,
this.container.findIntersecting(this.boundingBox),
@ -149,10 +136,9 @@ export class CirclePhysical implements Circle, Physical {
vec2.scale(this.velocity, tangent!, vec2.dot(tangent!, this.velocity));
wasHit = true;
}
this._isAirborne = hitSurface;
}
this._isAirborne = !wasHit;
return wasHit;
}