[SOLVED] Prevent vertical rotation using euler values

In a multiplayer application I’m syncing remote player entity angles using their local camera euler values. This part works as expected but it looks odd when the whole player model rotates vertically. Is there a straightforward method to make it rotate clockwise and anti-clockwise and remain standing upright?

Hi @Jimage,

How are you handling this in code?

If you keep both X and Z angles unchanged, and you update only the rotation around Y, this will handle this.

1 Like

By vertical rotation you mean rotating it around Y axis? Also, you can check this tutorial. The second part goes into orientation of the entity.

1 Like

Only applying Y is what I originally thought would work, but doing that only rotates 180 degrees back and forth rather than a full 360 turn.

The network code is adapted from the realtime multiplayer tutorial.

Network emit function is modified thus:

        var pos = this.player.getPosition();
        var eul = this.camera.getEulerAngles();
        Network.socket.emit('positionUpdate', {id: Network.id, x: pos.x, y: pos.y, z: pos.z, rx: eul.x, ry: eul.y, rz: eul.z});

Received data is being applied like this:

        this.players[data.id].entity.rigidbody.teleport(data.x, data.y+4, data.z);

Applying eulers directly to the teleport has the same result.

If I set both Y and Z it gives the extra 180 but upside down.

Found a solution via a previous post: [SOLVED] Getting Y rotation as 0 - 360 degrees (or -180 to 180)

I’m now sending this as the y rotation and getting the full 360 around the Y axis:

var fwd = this.camera.forward;
var y = Math.atan2(-fwd.x, -fwd.z) * pc.math.RAD_TO_DEG;

I see. The issue is probably because of the way euler angles work. Good you found a solution. Another way could probably be to use quaternions:

var q = new pc.Quat();
q.setFromEulerAngles(0, data.ry, 0);

Just tested it but it’s still giving me the back and forth 180 degree movement.

Thanks for verifying :slight_smile: I guess, the solution you found is the way to go then.