diff --git a/packages/client/src/game/core/camera.ts b/packages/client/src/game/core/camera.ts index 3ff0f56..525649c 100644 --- a/packages/client/src/game/core/camera.ts +++ b/packages/client/src/game/core/camera.ts @@ -149,8 +149,10 @@ export class ThirdPersonCamera { // https://www.youtube.com/watch?v=UuNPHOJ_V5o const factor = 1.0 - Math.pow(0.001, dt); - this.currentPosition.lerp(offset, factor); - this.currentLookAt.lerp(lookAt, factor); + // this.currentPosition.lerp(offset, factor); + // this.currentLookAt.lerp(lookAt, factor); + this.currentPosition.copy(offset); + this.currentLookAt.copy(lookAt); this.camera.position.copy(this.currentPosition); this.camera.lookAt(this.currentLookAt); diff --git a/packages/client/src/game/core/game.ts b/packages/client/src/game/core/game.ts index 17ae115..8f13569 100644 --- a/packages/client/src/game/core/game.ts +++ b/packages/client/src/game/core/game.ts @@ -24,9 +24,9 @@ export class Game extends Engine { this.use(ViewportComponent); this.use(EnvironmentComponent); this.use(LevelComponent); - this.use(GameplayComponent); this.use(MouseComponent); this.use(PhysicsWorldComponent); + this.use(GameplayComponent); this.getComponent(ViewportComponent).setSizeFromViewport(); this.start(); diff --git a/packages/client/src/game/core/gameplay.ts b/packages/client/src/game/core/gameplay.ts index 5cc0c4f..d5afe99 100644 --- a/packages/client/src/game/core/gameplay.ts +++ b/packages/client/src/game/core/gameplay.ts @@ -55,11 +55,11 @@ export class GameplayComponent extends EngineComponent { this.cleanUpEvents = this.bindEvents(); this.server.track(this.world); this.server.connect('ws://localhost:8256', this.uuid, 'testing'); + // this.loadCharacter('test', undefined, undefined, this.uuid); } override update(delta: number): void { this.controls?.update(delta); - this.move.set(0, 0, 0); this.look.setFromMatrixColumn(this.renderer.camera.matrix, 0); this.look.multiplyScalar(this.movement.forward + -this.movement.backward); @@ -83,7 +83,6 @@ export class GameplayComponent extends EngineComponent { jump, }); } - this.character?.setVelocity(this.move); if (this.move.length()) { @@ -119,7 +118,7 @@ export class GameplayComponent extends EngineComponent { this.world.add(char); - if (rot) char.quaternion.copy(rot); + if (rot) char.applyQuaternion(rot); this.events.emit('sceneJoin', char); diff --git a/packages/engine/src/components/physicsworld.ts b/packages/engine/src/components/physicsworld.ts index c9d30d8..9f364b5 100644 --- a/packages/engine/src/components/physicsworld.ts +++ b/packages/engine/src/components/physicsworld.ts @@ -27,6 +27,7 @@ export class PhysicsWorldComponent extends EngineComponent { update(delta: number): void { if (!this.physicsWorld) return; // FIXME: physics is tied to the FPS + this.physicsWorld.timestep = delta; this.physicsWorld?.step(); for (const object of this.trackedObjects) object.tickPhysics(delta); } @@ -44,7 +45,7 @@ export class PhysicsWorldComponent extends EngineComponent { const gravity = new Vector3(0, this.world.gravity, 0); const world = new physicsEngine.World(gravity); this.physicsWorld = world; - this.physicsWorld.timestep = 0.016; + this.physicsWorld.timestep = 1 / 60; this.physicsEngine = physicsEngine; this.initializePhysicsScene(); }); diff --git a/packages/engine/src/gameobjects/humanoid.object.ts b/packages/engine/src/gameobjects/humanoid.object.ts index d7c0031..efac3fb 100644 --- a/packages/engine/src/gameobjects/humanoid.object.ts +++ b/packages/engine/src/gameobjects/humanoid.object.ts @@ -3,6 +3,7 @@ import { AnimationClip, AnimationMixer, LoopOnce, + Matrix4, Quaternion, Skeleton, SkinnedMesh, @@ -27,6 +28,7 @@ export class Humanoid extends GameObject implements PhysicsTicking { private _health = 100; private _maxHealth = 100; private _velocity = new Vector3(0, 0, 0); + private _serverSet = false; private _serverVelocity = new Vector3(0, 0, 0); private _serverPosition = new Vector3(0, 0, 0); private _serverRotation = new Quaternion(); @@ -214,10 +216,16 @@ export class Humanoid extends GameObject implements PhysicsTicking { this._appliedGravity.y = 0; } + if (this._serverSet) { + this.rigidBody!.setRotation(this._serverRotation, false); + this.rigidBody!.setTranslation(this._serverPosition, false); + this._serverSet = false; + } + // Apply rigidbody transforms to object from last process tick if (this.rigidBody) { this.parent!.position.copy(this.rigidBody.translation() as any); - this.parent!.quaternion.copy(this.rigidBody.rotation() as any); + this.parent?.quaternion.copy(this.rigidBody.rotation() as any); } // Run animation @@ -238,12 +246,17 @@ export class Humanoid extends GameObject implements PhysicsTicking { this._velocity.clone().add(this._appliedGravity).multiplyScalar(dt) ); - // Apply look vector - this._currentLookAt.copy(this.parent!.position); - this._currentLookAt.add(this._lookAt); - this.parent?.lookAt(this._currentLookAt); - - this.applyRotation(this.parent!.quaternion); + // Apply look direction to the physics engine + const sink = new Vector3(); + const rotQuat = new Quaternion(); + new Matrix4() + .lookAt( + this.parent!.position, + this.parent!.position.clone().sub(this._lookAt), + new Vector3(0, 1, 0) + ) + .decompose(sink, rotQuat, sink); + this.rigidBody?.setRotation(rotQuat, false); } setWalkAnimationState(index: number) { @@ -286,9 +299,10 @@ export class Humanoid extends GameObject implements PhysicsTicking { synchronize(event: ServerTransformEvent) { if (!this.rigidBody) return; - this._serverPosition.copy(event.position); this._serverRotation.copy(event.quaternion); + this._serverPosition.copy(event.position); this._serverVelocity.copy(event.velocity); + this._serverSet = true; } private createNameTag() { @@ -313,7 +327,7 @@ export class Humanoid extends GameObject implements PhysicsTicking { const grounded = this.characterControllerRef.computedGrounded(); vec3.copy(computed as Vector3); vec3.add(this.parent.position); - vec3.lerp(this._serverPosition, 0.05); + // vec3.lerp(this._serverPosition, 0.05); this.rigidBody?.setNextKinematicTranslation(vec3); // After the collider movement calculation is done, we can read the @@ -327,11 +341,6 @@ export class Humanoid extends GameObject implements PhysicsTicking { this._grounded = grounded; } - private applyRotation(quat: Quaternion) { - quat.slerp(this._serverRotation, 0.05); - this.rigidBody?.setRotation(quat, false); - } - dispose(): void { this.nameTag?.dispose(); diff --git a/packages/engine/src/utils/character.ts b/packages/engine/src/utils/character.ts index d86eb58..fe7cda9 100644 --- a/packages/engine/src/utils/character.ts +++ b/packages/engine/src/utils/character.ts @@ -11,8 +11,11 @@ import { Group } from '../gameobjects/group.object'; import { MeshPart } from '../gameobjects/mesh.object'; import { Humanoid } from '../gameobjects/humanoid.object'; import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js'; -import { CharacterBodyPart, CharacterSpecification } from '../types/character'; -import { CharacterTextureType } from '..'; +import { + CharacterBodyPart, + CharacterSpecification, + CharacterTextureType, +} from '../types/character'; const CHARACTER_MAX_VERSION = 1; const CHARACTER_MIN_VERSION = 1;