Rotate without using euler angles in multiplayer game makes the entity disappear

cant seem to figure out why but when another player moves/rotates they just disappear.

var Network = pc.createScript('network');

function setCookie(cname, cvalue, exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i <ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}
// static variables
Network.id = null;
Network.socket = null;
var loggedin=getCookie("prevlogin");
// initialize code called once per entity
Network.prototype.initialize = function() {
    this.player = this.app.root.findByName('Player');
    this.playercam = this.app.root.findByName('PCamera');
    this.other = this.app.root.findByName('Other');

    var socket = io.connect('https://AIO-Dev-Server.grantrocks.repl.co'); // Glitch hosted server
    Network.socket = socket;
    
    socket.emit ('initialize');
    var self = this;
    socket.on ('playerData', function (data) {
        self.initializePlayers (data);
    });

    socket.on ('playerJoined', function (data) {
        self.addPlayer(data);
    });
    socket.on('message',function(data){
        if(self.app.root.findByName("Game").enabled==false){
            self.invalidsession(data);
        }else{
            alert(data);
        }
    });
    socket.on ('playerMoved', function (data) {
        self.movePlayer(data);
    });
    socket.on('validsession',function(data){
        self.startgame();
    });
    socket.on('invalidsession',function(data){
        self.invalidsession();
    });
    socket.on ('killPlayer', function (data) {
        self.removePlayer(data);
    });
};

Network.prototype.initializePlayers = function (data) {
    this.players = data.players;
    Network.id = data.id;

    for(var id in this.players){
        if(id != Network.id){
            this.players[id].entity = this.createPlayerEntity(this.players[id]);
        }
    }
    

    this.initialized = true;
    console.log('initialized');
};

Network.prototype.addPlayer = function (data) {
    this.players[data.id] = data;
    this.players[data.id].entity = this.createPlayerEntity(data);
};

Network.prototype.movePlayer = function (data) {
    if(loggedin=="yes"){
    if (this.initialized && !this.players[data.id].deleted) {
        this.players[data.id].entity.setLocalRotation(data.rx, data.ry, data.rz);
        this.players[data.id].entity.rigidbody.teleport(data.x, data.y, data.z);
    }
    }
};

Network.prototype.removePlayer = function (data) {
    if (this.players[data].entity) {
        this.players[data].entity.destroy ();
        this.players[data].deleted = true;
    }
};

Network.prototype.createPlayerEntity = function (data) {
    var newPlayer = this.other.clone();
    newPlayer.enabled = true;

    this.other.getParent().addChild(newPlayer);

    if (data)
        newPlayer.rigidbody.teleport(data.x, data.y, data.z);
    return newPlayer;
};

// update code called every frame
Network.prototype.update = function(dt) {
    if(loggedin=="yes"){
    this.updatePosition();
    }
};

Network.prototype.updatePosition = function () {
    if(loggedin=="yes"){
    if (this.initialized) {    
        var pos = this.player.getPosition();
        var rot= this.playercam.getLocalRotation();
        console.log(rot);
        Network.socket.emit('positionUpdate', {id: Network.id, x: pos.x, y: pos.y, z: pos.z,rx: 0, ry: rot.y, rz: 0});
    }
    }
};
Network.prototype.startgame=function(){
        setCookie("prevlogin","yes",1);
        this.app.root.findByName("2D Screen").enabled=false;
        this.app.root.findByName("Game").enabled=true;
};
Network.prototype.invalidsession=function(data){
    this.app.root.findByName("loginbtntext").element.text="Login";
    if(data){
       this.app.root.findByName("errormsglogin").element.text=data; 
    }
};

https://playcanvas.com/editor/scene/1517481

Hi @Granted! Not sure why the entity disappears, but you can’t use setLocalRotation on a dynamic rigidbody. You can try to remove line 90 and replace line 91 with the rule below.

this.players[data.id].entity.rigidbody.teleport(data.x, data.y, data.z, data.rx, data.ry, data.rz);

yes i tried doing that but i only want the y rotation. If i use that snippet then once the player rotates 180 it snaps back to zero not doing a ful rotation only a half one. Also the rigidbody is not dynamic its static.

You can’t rotate a static rigidbody anyway. Then at least it need to be a kinematic rigidbody.

is there a different way i can rotate the player using a rigid body?

What type of rigidbody?

its fine now. i figured out another way to get the rotation to work. thanks for the help

Can you share it here please, so it can help other too?

Im not rotating the main other player. Im now rotating a entity inside of the other player and what i wanted to move and look around now moves inside of the rotation entity. Still dont know why the other player would dissaper though

1 Like

Your other player still has a static rigidbody.

A static rigidbody is not supposed to move or rotate.

1 Like

ok

I think if an entity disappears, you are setting a position or rotation that’s undefined or something like that.

1 Like

yeah maybe

getLocalRotation returns a quaternion which has four components, X, Y, Z, W

In both your network message send and where you use setLocalRotation, you are missing the W component.

dang it thats shy it wasnt working

i could try to help you add me

Thanks for the offer man but i got it working