more physics stuff
This commit is contained in:
parent
9c980bc725
commit
837be262a4
|
@ -118,8 +118,6 @@ export class PhysicsWorldComponent extends EngineComponent {
|
|||
const proxy = new HumanoidPhysicsProxy(
|
||||
humanoid,
|
||||
this.characterPhysics,
|
||||
this.characterRay,
|
||||
this.physicsWorld,
|
||||
collider,
|
||||
rigidBody
|
||||
);
|
||||
|
@ -134,7 +132,7 @@ export class PhysicsWorldComponent extends EngineComponent {
|
|||
|
||||
this.characterRay = new this.physicsEngine.Ray(
|
||||
{ x: 0, y: 0, z: 0 },
|
||||
{ x: 0, y: -0.5, z: 0 }
|
||||
{ x: 0, y: -0.1, z: 0 }
|
||||
);
|
||||
this.characterPhysics = this.physicsWorld.createCharacterController(0.01);
|
||||
this.characterPhysics.setApplyImpulsesToDynamicBodies(true);
|
||||
|
|
|
@ -2,6 +2,7 @@ import {
|
|||
AnimationAction,
|
||||
AnimationClip,
|
||||
AnimationMixer,
|
||||
LoopOnce,
|
||||
Object3D,
|
||||
Skeleton,
|
||||
SkinnedMesh,
|
||||
|
@ -39,7 +40,6 @@ export class Humanoid extends GameObject implements Ticking, Disposable {
|
|||
private _lookAt = new Vector3(0, 0, 1);
|
||||
private _currentLookAt = new Vector3(0, 0, 1);
|
||||
private _animState = 0;
|
||||
private _jumpEnergy = 0;
|
||||
public static bodyPartNames = [
|
||||
'Head',
|
||||
'Torso',
|
||||
|
@ -71,7 +71,8 @@ export class Humanoid extends GameObject implements Ticking, Disposable {
|
|||
public characterHeight = 5.5;
|
||||
public characterHalfHeight = this.characterHeight / 2;
|
||||
|
||||
public jumpPower = 18;
|
||||
private gravity = -12;
|
||||
private shouldJump = false;
|
||||
|
||||
private mixer!: AnimationMixer;
|
||||
private idleAction!: AnimationAction;
|
||||
|
@ -81,6 +82,9 @@ export class Humanoid extends GameObject implements Ticking, Disposable {
|
|||
private nameTag?: NameTag;
|
||||
private physics?: HumanoidPhysicsProxy;
|
||||
|
||||
@EditorProperty({ type: Boolean })
|
||||
public jumpPower = 6;
|
||||
|
||||
@EditorProperty({ type: Number })
|
||||
get health() {
|
||||
return this._health;
|
||||
|
@ -111,9 +115,6 @@ export class Humanoid extends GameObject implements Ticking, Disposable {
|
|||
return this._grounded;
|
||||
}
|
||||
set grounded(value: boolean) {
|
||||
if (value && this._jumpEnergy < this.jumpPower / 2) {
|
||||
this._jumpEnergy = 0;
|
||||
}
|
||||
this._grounded = value;
|
||||
}
|
||||
|
||||
|
@ -149,6 +150,7 @@ export class Humanoid extends GameObject implements Ticking, Disposable {
|
|||
this.idleAction = this.mixer.clipAction(idleClip);
|
||||
this.walkAction = this.mixer.clipAction(walkClip);
|
||||
this.jumpAction = this.mixer.clipAction(jumpClip);
|
||||
this.jumpAction.setLoop(LoopOnce, 0);
|
||||
this.idleAction.play();
|
||||
|
||||
this.createNameTag();
|
||||
|
@ -165,25 +167,22 @@ export class Humanoid extends GameObject implements Ticking, Disposable {
|
|||
}
|
||||
|
||||
jump() {
|
||||
if (!this.grounded) return;
|
||||
this.grounded = false;
|
||||
this._jumpEnergy = this.jumpPower;
|
||||
if (!this.grounded || this.shouldJump) return;
|
||||
this.shouldJump = true;
|
||||
}
|
||||
|
||||
tick(dt: number): void {
|
||||
if (!this.ready) return;
|
||||
this.mixer.update(dt);
|
||||
|
||||
if (!this.grounded) {
|
||||
this._appliedGravity.y = -9.81;
|
||||
} else {
|
||||
this._appliedGravity.y = 0;
|
||||
}
|
||||
if (this._jumpEnergy > 0) {
|
||||
this._appliedGravity.y += this._jumpEnergy;
|
||||
this._jumpEnergy -= 10 * dt;
|
||||
if (this.shouldJump) {
|
||||
this._appliedGravity.y = Math.sqrt(-2 * this.gravity * this.jumpPower);
|
||||
this.grounded = false;
|
||||
this.shouldJump = false;
|
||||
}
|
||||
|
||||
this._appliedGravity.y += this.gravity * dt;
|
||||
|
||||
if (this.physics)
|
||||
this.physics.applyMovement(
|
||||
this.parent!.position,
|
||||
|
@ -198,6 +197,10 @@ export class Humanoid extends GameObject implements Ticking, Disposable {
|
|||
if (this.physics) {
|
||||
this.physics.applyRotation(this.parent!.quaternion);
|
||||
}
|
||||
|
||||
if (this.grounded) {
|
||||
this._appliedGravity.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
setWalkAnimationState(index: number) {
|
||||
|
|
|
@ -28,8 +28,6 @@ export class HumanoidPhysicsProxy extends PhysicsObjectAssociation {
|
|||
constructor(
|
||||
private humanoid: Humanoid,
|
||||
private controller: Rapier.KinematicCharacterController,
|
||||
private ray: Rapier.Ray,
|
||||
private world: Rapier.World,
|
||||
collider: Rapier.Collider,
|
||||
body: Rapier.RigidBody
|
||||
) {
|
||||
|
@ -40,8 +38,8 @@ export class HumanoidPhysicsProxy extends PhysicsObjectAssociation {
|
|||
const vec3 = position.clone();
|
||||
this.controller.computeColliderMovement(this.collider!, velocity);
|
||||
const computed = this.controller.computedMovement();
|
||||
const grounded = this.controller.computedGrounded();
|
||||
vec3.copy(computed as Vector3);
|
||||
// console.log(computed, position);
|
||||
this.body?.setNextKinematicTranslation(vec3.add(position));
|
||||
// After the collider movement calculation is done, we can read the
|
||||
// collision events.
|
||||
|
@ -53,18 +51,7 @@ export class HumanoidPhysicsProxy extends PhysicsObjectAssociation {
|
|||
// this.collider?.setTranslation(colliderHalf);
|
||||
// this.humanoid.parent!.position.copy(computed as Vector3).sub(halfVec);
|
||||
|
||||
vec3.copy(this.humanoid.parent!.position);
|
||||
this.ray.origin = vec3;
|
||||
this.ray.origin.y -= 0.01;
|
||||
const hit = this.world.castRay(this.ray, 0.5, false);
|
||||
this.humanoid.grounded = false;
|
||||
if (hit) {
|
||||
const point = this.ray.pointAt(hit.toi);
|
||||
const diff = vec3.y - (point.y + 0.15);
|
||||
if (diff < 0) {
|
||||
this.humanoid.grounded = true;
|
||||
}
|
||||
}
|
||||
this.humanoid.grounded = grounded;
|
||||
}
|
||||
|
||||
applyRotation(quat: Quaternion) {
|
||||
|
|
|
@ -80,7 +80,7 @@ export const instanceCharacterObject = async (name: string) => {
|
|||
controller.position.set(0, 4.75, 0);
|
||||
controller.archivable = false;
|
||||
baseObject.add(controller);
|
||||
baseObject.position.set(0, 0.1, 0);
|
||||
baseObject.position.set(0, 0.5, 0);
|
||||
|
||||
return baseObject;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue