attempt to fix glitchy, not yet

This commit is contained in:
Evert Prants 2023-06-25 21:08:35 +03:00
parent 3084fefc4f
commit 16cffd0b25
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
6 changed files with 57 additions and 29 deletions

View File

@ -11,6 +11,8 @@ import {
import { GameEvents } from '../types/events';
import { GameplayComponent } from './gameplay';
const INVERSE_MAX_FPS = 0.016;
export class Game extends Engine {
public events = new EventEmitter<GameEvents>();
@ -29,10 +31,14 @@ export class Game extends Engine {
}
loop(now: number): void {
const delta = this.getDelta(now);
let delta = this.getDelta(now);
this.running && requestAnimationFrame((ts) => this.loop(ts));
this.update(delta);
this.render.render();
while (delta >= INVERSE_MAX_FPS) {
this.update(0.016);
delta -= INVERSE_MAX_FPS;
}
}
async loadLevel(path: string) {

View File

@ -8,11 +8,12 @@ import {
EventEmitter,
Renderer,
randomUUID,
SpawnEvent,
} from '@freeblox/engine';
import { Quaternion, Vector3 } from 'three';
import { ThirdPersonCamera } from './camera';
import { GameEvents, SpawnEvent } from '../types/events';
import { GameEvents } from '../types/events';
/**
* Gameplay manager.
@ -71,21 +72,9 @@ export class GameplayComponent extends EngineComponent {
this.look.copy(this.move).normalize();
this.character?.localToWorld(this.look);
this.character?.setVelocity(this.move);
const look = this.move.clone().normalize();
if (this.move.length()) {
this.character?.setLook(look);
}
let jump = false;
if (this.jump) {
jump = true;
this.jump = false;
this.character?.jump();
}
let jump = this.jump;
if (!this.move.equals(this.prevVelocity) || jump !== this.prevJump) {
this.events.emit('sendPlayer', {
playerId: this.uuid,
@ -95,12 +84,24 @@ export class GameplayComponent extends EngineComponent {
});
}
this.character?.setVelocity(this.move);
if (this.move.length()) {
this.character?.setLook(look);
}
if (this.jump) {
this.jump = false;
this.character?.jump();
}
this.prevVelocity.copy(this.move);
this.prevJump = jump;
}
override dispose(): void {
this.cleanUpEvents?.();
this.server.dispose();
}
public async loadCharacter(

View File

@ -43,6 +43,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.physicsEngine = physicsEngine;
this.initializePhysicsScene();
});
@ -65,12 +66,16 @@ export class PhysicsWorldComponent extends EngineComponent {
const object = this.trackedObjects.find(
(obj) => event.object === obj.uuid
) as PhysicsObject;
if (!object) return;
object.rigidBody?.setTranslation(event.position, false);
object.rigidBody?.setRotation(event.quaternion, false);
event.velocity && object.rigidBody?.setLinvel(event.velocity, false);
event.angularVelocity &&
object.rigidBody?.setAngvel(event.angularVelocity, false);
if (!object || !object.rigidBody) return;
if (object instanceof Humanoid) {
object.synchronize(event);
} else {
object.rigidBody?.setTranslation(event.position, false);
object.rigidBody?.setRotation(event.quaternion, false);
event.velocity && object.rigidBody?.setLinvel(event.velocity, false);
event.angularVelocity &&
object.rigidBody?.setAngvel(event.angularVelocity, false);
}
};
const characterMoveEvent = (event: PlayerEvent) => {

View File

@ -16,6 +16,7 @@ import { NameTag } from './nametag.object';
import { CanvasUtils } from '../canvas/utils';
import { PhysicsTicking } from '../physics';
import type Rapier from '@dimforge/rapier3d';
import { ServerTransformEvent } from '..';
export type HumanoidBodyPart =
| 'Head'
@ -210,6 +211,11 @@ export class Humanoid extends GameObject implements PhysicsTicking {
tick(dt: number): void {
if (!this.ready) return;
// Stick to ground
if (this.grounded) {
this._appliedGravity.y = 0;
}
// Apply rigidbody transforms to object from last process tick
if (this.rigidBody) {
this.parent!.position.copy(this.rigidBody.translation() as any);
@ -240,11 +246,6 @@ export class Humanoid extends GameObject implements PhysicsTicking {
this.parent?.lookAt(this._currentLookAt);
this.applyRotation(this.parent!.quaternion);
// Stick to ground
if (this.grounded) {
this._appliedGravity.y = 0;
}
}
setWalkAnimationState(index: number) {
@ -285,6 +286,15 @@ export class Humanoid extends GameObject implements PhysicsTicking {
this.detach();
}
synchronize(event: ServerTransformEvent) {
if (!this.rigidBody) return;
this.parent!.position.copy(this.rigidBody.translation() as any);
this.parent!.quaternion.copy(this.rigidBody.rotation() as any);
this.rigidBody?.setTranslation(event.position, false);
this.rigidBody?.setRotation(event.quaternion, false);
this.rigidBody?.setNextKinematicTranslation(event.velocity);
}
private createNameTag() {
if (this.nameTag) {
this.nameTag.dispose();

View File

@ -109,6 +109,12 @@ export class GameSocket implements Disposable {
console.log('player joined', playerId, playerName);
break;
}
case PacketType.PLAYER_QUIT: {
const playerId = incoming.read(String)!;
const playerName = incoming.read(String)!;
console.log('player quit', playerId, playerName);
break;
}
case PacketType.PLAYER_CHARACTER: {
const playerId = incoming.read(String)!;
const playerName = incoming.read(String)!;

View File

@ -73,8 +73,8 @@ export interface ServerTransformEvent {
object: string;
position: Vector3;
quaternion: Quaternion;
velocity?: Vector3;
angularVelocity?: Vector3;
velocity: Vector3;
angularVelocity: Vector3;
}
export interface PlayerEvent {