So I come from a long background of Java, I only recently got into JS so pardon me if I appear somewhat naive on the topic.
I’ve been developing a first person game for the past few months using mainly Three and Socket.io, everything runs fine in the scene, player position / rotation updating and animating are all in a working state and for the most part efficient (as far as i know). When a player first joins the scene all our speeds for moving / jumping / animating are perfect, however once a new player joins everything becomes slightly slower (exponentially I think). once the extra player is removed it becomes faster and returns to the previous speed state. I’ve done what research is available and I think the problem lies in the delta timing for the first person controller.
Here is what that looks like
run () {
requestAnimationFrame(() => this.run());
time1 = performance.now();
deltaa = ( time1 - prevTime ) / 1000;
this.update();
this.render();
}
update () {
let time = time1;
let delta = deltaa;
this.camera.rotation.set(0,0,0);
this.camera.rotateY(lookY);
this.camera.rotateX(lookX);
let forward = new THREE.Vector3(Math.sin(lookY), 0, Math.cos(lookY));
let right = new THREE.Vector3(Math.cos(lookY), 0, Math.sin(lookY));
let velocity = new THREE.Vector3(0,0,0);
let offset = 50;
let offset1 = 40;
this.camera.position.x -= velocity.x;
this.camera.position.y -= velocity.y;
this.camera.position.z -= velocity.z;
if (mouselocked) {
if (moveForward) {
if (!grounded) {
velocity.x -= forward.x * offset * delta;
velocity.z -= forward.z * offset * delta;
} else {
velocity.x -= forward.x * offset1 * delta;
velocity.z -= forward.z * offset1 * delta;
console.log(delta);
}
this.emit('walking');
}
if (moveBackward) {
if (!grounded) {
velocity.x += forward.x * offset * delta;
velocity.z += forward.z * offset * delta;
} else {
velocity.x += forward.x * offset1 * delta;
velocity.z += forward.z * offset1 * delta;
}
this.emit('walking');
}
if (moveRight) {
if (!grounded) {
velocity.x += right.x * offset * delta;
velocity.z -= right.z * offset * delta;
} else {
velocity.x += right.x * offset1 * delta;
velocity.z -= right.z * offset1 * delta;
}
this.emit('walking');
}
if (moveLeft) {
if (!grounded) {
velocity.x -= right.x * offset * delta;
velocity.z += right.z * offset * delta;
} else {
velocity.x -= right.x * offset1 * delta;
velocity.z += right.z * offset1 * delta;
}
this.emit('walking');
}
}
if (!moveForward && !moveBackward && !moveLeft && !moveRight) {
walking = false;
}
if (!walking && !jumping) {
this.emit("idling");
}
this.camera.position.x += velocity.x;
this.camera.position.y += velocity.y;
this.camera.position.z += velocity.z;
if(grounded && jumping) {
verticalVelocity = 1.2;
grounded = false;
}
verticalVelocity -= 0.06 ;
this.camera.position.y += verticalVelocity * offset1 * delta;
if(this.camera.position.y < 13){
this.camera.position.y = 13;
verticalVelocity = 0;
grounded = true;
}
prevTime = time;
this.emit("update", this.camera.position, lookX, lookY);
}
render() {
this.renderer.render(this.scene, this.camera);
}
Apologies if that is not easy on the eyes, but basically I kind of Frankensteined the delta timing from a modified version of PointerLockControls. When a new player joins something affects this and causes something in here to be ran / calculated slower, with or without the timing variables included. so in that I do admit that they may be kind of arbitrary to me right now since they ultimately don’t change the issue but hopefully I’m headed down the right track
I hope this is enough information to help me determine what I might be doing wrong or could do more efficiently. If it’s necessary I can post visual aid or possibly a fiddle.