I’m replicating dynamic entities by guid values on my multiplayer game, it’s successfully syncing entity state for the most part considering I used realtime multiplayer example as a start reference.
This line causes the target entity to have a stutter/glitch like movement during update to server yet this doesn’t occur with player movements.
clientNetEntity.rigidbody.teleport(entityData.objPosX, entityData.objPosY, entityData.objPosZ, entityData.objRotX, entityData.objRotY, entityData.objRotZ);
network.js - Entity Replication Code
// Called every frame in Network.prototype.update()
Network.prototype.updateNetEntityPosition = function () {
if (this.initialized) {
var gameObjects = this.game.findByTag('netDynamic');
var gameObjLength = gameObjects.length;
for (var i = 0; i < gameObjLength; i++) {
var obj = gameObjects[i]; // Not passed to Server
var objName = obj.name; // Not passed to Server
var objGuid = obj._guid;
var objPos = obj.getPosition();
var objRot = obj.getRotation();
var objAngl = objRot.getEulerAngles();
//console.log(objName + ': ' + objGuid + '| pos: ' + objPos);
var clientNetEntity = this.game.findByGuid(objGuid); // Locate entity in scene
// Update per frame if entity's velocity changed
if (clientNetEntity.enabled && clientNetEntity.rigidbody.linearVelocity.lengthSq() > 0.01) {
// Prevents newly connected clients from updating entity positions for already connected clients (Dynamic objects fall due to mass and gravity)
if (this.shouldEmitUpdate) {
//console.log('Alert Network', objName, 'moved to', objPos);
Network.socket.emit('netEntityPositionUpdate', {objGuid: objGuid, objPosX: objPos.x, objPosY: objPos.y, objPosZ: objPos.z, objRotX: objAngl.x, objRotY: objAngl.y, objRotZ: objAngl.z}); // Network emits signal to moveNetEntity() next
}
}
}
}
};
Network.prototype.moveNetEntity = function (entityData) {
if (this.initialized) {
if (this.netEntities !== null && this.netEntities[entityData.objGuid]) {
var clientNetEntity = this.game.findByGuid(entityData.objGuid); // Locate entity in scene
var netEntityPos = new pc.Vec3(entityData.objPosX, entityData.objPosY, entityData.objPosZ);
if (clientNetEntity.enabled && clientNetEntity.getPosition() !== netEntityPos) {
//console.log('Network updated position:', clientNetEntity.name);
//clientNetEntity.setPosition(entityData.objPosX, entityData.objPosY, entityData.objPosZ, entityData.objRotX, entityData.objRotY, entityData.objRotZ);
clientNetEntity.rigidbody.teleport(entityData.objPosX, entityData.objPosY, entityData.objPosZ, entityData.objRotX, entityData.objRotY, entityData.objRotZ);
}
}
}
};
A temporary yet not suggested fix has been to use .setPosition on the dynamic entity as it does smoothly move the entity for all clients however sometimes it can be inaccurate/take time to update to proper position.
Below is the code that replicates player position state, it smoothly moves all clients with rigidbody.teleport yet on entities such code isn’t effective.
network.js - Player Replication Code
// update code called every frame
Network.prototype.update = function(dt) {
this.updatePosition();
if (this.netEntities !== null) {
this.updateNetEntityPosition();
}
};
Network.prototype.updatePosition = function () {
if (this.initialized) {
var pos = this.player.getPosition();
var rot = this.player.getRotation();
var angl = rot.getEulerAngles();
if (this.player.getPosition().y < -7) {
this.player.rigidbody.teleport(0, 5, 0, angl.x, angl.y, angl.z);
}
Network.socket.emit('positionUpdate', {id: Network.id, x: pos.x, y: pos.y, z: pos.z, rotX: angl.x, rotY: angl.y, rotZ: angl.z}); // Network emits signal to movePlayer() next
}
};
Network.prototype.movePlayer = function (data) {
if (this.initialized && this.players[data.id]) {
this.players[data.id].entity.rigidbody.teleport(data.x, data.y, data.z, data.rotX, data.rotY, data.rotZ);
//console.log('moveAndOrientate:', data.x, data.y, data.z, 'rotX:', data.rotX, 'rotY:', data.rotY, 'rotZ:', data.rotZ);
//console.log('Moving:', this.players[data.id].entity.name);
//console.log(this.players[data.id].entity.children[1]);
}
};
The entities in editor properties such as Linear/Angular Damping/Factors are matching so I don’t understand why this is occurring. I also prevent updates to the server unless the entity rigidbody velocity changes. Does anyone know why this is happening for tracked entities and not players?