attempt to fix glitchy, not yet
This commit is contained in:
parent
20acbbecaa
commit
e2ec33f97c
@ -119,6 +119,11 @@ export class Humanoid extends GameObject implements PhysicsTicking {
|
|||||||
tick(dt: number): void {
|
tick(dt: number): void {
|
||||||
if (!this.ready) return;
|
if (!this.ready) return;
|
||||||
|
|
||||||
|
// Stick to ground
|
||||||
|
if (this.grounded) {
|
||||||
|
this._appliedGravity.y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Apply rigidbody transforms to object from last process tick
|
// Apply rigidbody transforms to object from last process tick
|
||||||
if (this.rigidBody) {
|
if (this.rigidBody) {
|
||||||
this.parent!.position.copy(this.rigidBody.translation() as any);
|
this.parent!.position.copy(this.rigidBody.translation() as any);
|
||||||
@ -146,11 +151,6 @@ export class Humanoid extends GameObject implements PhysicsTicking {
|
|||||||
this.parent?.lookAt(this._currentLookAt);
|
this.parent?.lookAt(this._currentLookAt);
|
||||||
|
|
||||||
this.applyRotation(this.parent!.quaternion);
|
this.applyRotation(this.parent!.quaternion);
|
||||||
|
|
||||||
// Stick to ground
|
|
||||||
if (this.grounded) {
|
|
||||||
this._appliedGravity.y = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
|
@ -15,27 +15,33 @@ export class PhysicsService {
|
|||||||
|
|
||||||
private running = false;
|
private running = false;
|
||||||
private sceneInitialized = false;
|
private sceneInitialized = false;
|
||||||
private physicsTicker: ReturnType<typeof setTimeout>;
|
private physicsTicker: ReturnType<typeof setInterval>;
|
||||||
private trackedObjects: PhysicsTicking[] = [];
|
private trackedObjects: PhysicsTicking[] = [];
|
||||||
|
private tickCallback?: () => void;
|
||||||
|
|
||||||
loop() {
|
loop() {
|
||||||
if (!this.running) return;
|
if (!this.running) return;
|
||||||
this.physicsTicker = setTimeout(() => this.loop(), 25);
|
this.physicsTicker = setInterval(() => {
|
||||||
this.physicsWorld.step();
|
this.physicsWorld.step();
|
||||||
for (const object of this.trackedObjects) object.tick(0.025); // TODO: DT
|
for (const object of this.trackedObjects) object.tick(0.016);
|
||||||
|
this.tickCallback?.();
|
||||||
|
}, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(world: World) {
|
async start(world: World, cb?: () => void) {
|
||||||
this.world = world;
|
this.world = world;
|
||||||
await RAPIER.init();
|
await RAPIER.init();
|
||||||
this.physicsWorld = new RAPIER.World(new Vector3(0, this.world.gravity, 0));
|
this.physicsWorld = new RAPIER.World(new Vector3(0, this.world.gravity, 0));
|
||||||
|
this.physicsWorld.timestep = 0.016;
|
||||||
this.running = true;
|
this.running = true;
|
||||||
|
this.tickCallback = cb;
|
||||||
this.initializePhysicsScene();
|
this.initializePhysicsScene();
|
||||||
this.loop();
|
this.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
this.running = false;
|
this.running = false;
|
||||||
|
clearInterval(this.physicsTicker);
|
||||||
}
|
}
|
||||||
|
|
||||||
getObjectPackets() {
|
getObjectPackets() {
|
||||||
@ -48,7 +54,10 @@ export class PhysicsService {
|
|||||||
.write(object.uuid, String)
|
.write(object.uuid, String)
|
||||||
.write(body.translation(), 'vec3')
|
.write(body.translation(), 'vec3')
|
||||||
.write(body.rotation(), 'quat')
|
.write(body.rotation(), 'quat')
|
||||||
.write(body.linvel(), 'vec3')
|
.write(
|
||||||
|
object instanceof Humanoid ? body.nextTranslation() : body.linvel(),
|
||||||
|
'vec3',
|
||||||
|
)
|
||||||
.write(body.angvel(), 'vec3')
|
.write(body.angvel(), 'vec3')
|
||||||
.toBuffer(),
|
.toBuffer(),
|
||||||
);
|
);
|
||||||
|
@ -19,7 +19,7 @@ export class WorldService implements OnModuleInit {
|
|||||||
private logger = new Logger(WorldService.name);
|
private logger = new Logger(WorldService.name);
|
||||||
private world = new World();
|
private world = new World();
|
||||||
private environment = new Environment();
|
private environment = new Environment();
|
||||||
private broadcastWorldStateTicker!: ReturnType<typeof setInterval>;
|
private broadcastTick = 0;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly players: PlayerStoreService,
|
private readonly players: PlayerStoreService,
|
||||||
@ -33,8 +33,14 @@ export class WorldService implements OnModuleInit {
|
|||||||
this.loadWorld()
|
this.loadWorld()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.logger.log('World file loaded');
|
this.logger.log('World file loaded');
|
||||||
this.physics.start(this.world);
|
this.physics.start(this.world, () => {
|
||||||
this.startUpdateTick();
|
if (this.broadcastTick >= 0.25) {
|
||||||
|
this.broadcastTick = 0;
|
||||||
|
this.broadcastWorldState();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.broadcastTick += 0.016;
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
this.logger.error('Failed to load world:', err.stack);
|
this.logger.error('Failed to load world:', err.stack);
|
||||||
@ -207,11 +213,4 @@ export class WorldService implements OnModuleInit {
|
|||||||
this.players.broadcastExcept(data, this.players.getUninitializedIds()),
|
this.players.broadcastExcept(data, this.players.getUninitializedIds()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private startUpdateTick() {
|
|
||||||
this.broadcastWorldStateTicker = setInterval(
|
|
||||||
() => this.broadcastWorldState(),
|
|
||||||
5000,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user