Rotations Matrix

This commit is contained in:
brausjonas
2022-05-13 17:16:12 +02:00
parent a36cf65f6d
commit 04e06652f5
5 changed files with 121 additions and 7 deletions
@@ -148,6 +148,25 @@ public class Client
Send(send); Send(send);
} }
public void SendRotationUpdate(Quaternion rotation)
{
string x = rotation.x.ToString("n4");
string y = rotation.y.ToString("n4");
string z = rotation.z.ToString("n4");
string w = rotation.w.ToString("n4");
string sendRotation = x + " " + y + " " + z + " " + w;
byte[] sendRotationASCII = Encoding.ASCII.GetBytes(sendRotation);
byte[] send = new byte[sendRotationASCII.Length + 1];
send[0] = 8;
for (int i = 1; i < send.Length; i++)
{
send[i] = sendRotationASCII[i - 1];
}
Send(send);
}
private void Receive() private void Receive()
{ {
@@ -192,7 +211,27 @@ public class Client
{ {
world.RemovePlayer(data[1]); world.RemovePlayer(data[1]);
} }
if (data[0] == 8)
{
byte[] rotationASCII = new byte[data.Length - 2];
for (int i = 0; i < data.Length - 2; i++)
{
rotationASCII[i] = data[i + 2];
}
byte id = data[1];
string[] rotationString = Encoding.ASCII.GetString(rotationASCII).Split(' ');
float x = float.Parse(rotationString[0]);
float y = float.Parse(rotationString[1]);
float z = float.Parse(rotationString[2]);
float w = float.Parse(rotationString[3]);
world.UpdatePlayerRotation(new Quaternion(x, y, z, w), id);
}
} }
} }
public void EditBlock(Vector3 positionInWorld, byte blockID) public void EditBlock(Vector3 positionInWorld, byte blockID)
+23 -2
View File
@@ -25,6 +25,7 @@ public class Player : MonoBehaviour
private float gravity = -20f; private float gravity = -20f;
private Camera camera = null; private Camera camera = null;
private Vector3 lastPosition = new Vector3(); private Vector3 lastPosition = new Vector3();
private Quaternion lastRotation = new Quaternion();
//network //network
private Client client = null; private Client client = null;
@@ -56,13 +57,21 @@ public class Player : MonoBehaviour
transform.Translate(velocity, Space.World); transform.Translate(velocity, Space.World);
Vector3 position = transform.position; Vector3 position = transform.position;
Quaternion rotation = transform.rotation;
if (lastPosition.x != position.x || lastPosition.y != position.y || lastPosition.z != position.z) if (lastPosition.x != position.x || lastPosition.y != position.y || lastPosition.z != position.z)
{ {
client.SendPositionUpdate(transform.position); client.SendPositionUpdate(transform.position);
} }
if (rotation.w != lastRotation.w || rotation.x != lastRotation.x || rotation.y != lastRotation.y ||
rotation.z != lastRotation.z)
{
client.SendRotationUpdate(transform.rotation);
}
lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z); lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
lastRotation = transform.rotation;
} }
private void Update() private void Update()
@@ -233,9 +242,9 @@ public class Player : MonoBehaviour
} }
} }
public class PlayerUpdateData public class PlayerPositionUpdateData
{ {
public PlayerUpdateData(byte id, Vector3 position, bool destroy) public PlayerPositionUpdateData(byte id, Vector3 position, bool destroy)
{ {
this.id = id; this.id = id;
this.position = position; this.position = position;
@@ -246,5 +255,17 @@ public class Player : MonoBehaviour
public Vector3 position; public Vector3 position;
public bool destroy; public bool destroy;
} }
public class PlayerRotationsUpdateData
{
public byte id;
public Quaternion rotation;
public PlayerRotationsUpdateData(byte id, Quaternion rotation)
{
this.id = id;
this.rotation = rotation;
}
}
} }
+23 -5
View File
@@ -12,7 +12,10 @@ public class World : MonoBehaviour
private string name; private string name;
private Dictionary<byte, GameObject> otherPlayers = new Dictionary<byte, GameObject>(); private Dictionary<byte, GameObject> otherPlayers = new Dictionary<byte, GameObject>();
private Queue<byte> playersToCreate = new Queue<byte>(); private Queue<byte> playersToCreate = new Queue<byte>();
private Queue<Player.PlayerUpdateData> playersToUpdate = new Queue<Player.PlayerUpdateData>(); private Queue<Player.PlayerPositionUpdateData> playersPositionsToUpdate = new Queue<Player.PlayerPositionUpdateData>();
private Queue<Player.PlayerRotationsUpdateData> playersRotationsToUpdate =
new Queue<Player.PlayerRotationsUpdateData>();
//chunks //chunks
private Chunk[,] chunks; private Chunk[,] chunks;
@@ -36,7 +39,7 @@ public class World : MonoBehaviour
public void RemovePlayer(byte id) public void RemovePlayer(byte id)
{ {
playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, Vector3.zero, true)); playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, Vector3.zero, true));
} }
private void Update() private void Update()
@@ -58,9 +61,9 @@ public class World : MonoBehaviour
otherPlayers.Add(id, player); otherPlayers.Add(id, player);
} }
if (playersToUpdate.Count > 0) if (playersPositionsToUpdate.Count > 0)
{ {
Player.PlayerUpdateData data = playersToUpdate.Dequeue(); Player.PlayerPositionUpdateData data = playersPositionsToUpdate.Dequeue();
otherPlayers.TryGetValue(data.id, out GameObject g); otherPlayers.TryGetValue(data.id, out GameObject g);
if (g != null) if (g != null)
{ {
@@ -72,11 +75,26 @@ public class World : MonoBehaviour
} }
} }
} }
if (playersRotationsToUpdate.Count > 0)
{
Player.PlayerRotationsUpdateData data = playersRotationsToUpdate.Dequeue();
otherPlayers.TryGetValue(data.id, out GameObject g);
if (g != null)
{
g.transform.rotation = data.rotation;
}
}
} }
public void UpdatePlayerPosition(Vector3 position, byte id) public void UpdatePlayerPosition(Vector3 position, byte id)
{ {
playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, position, false)); playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, position, false));
}
public void UpdatePlayerRotation(Quaternion rotation, byte id)
{
playersRotationsToUpdate.Enqueue(new Player.PlayerRotationsUpdateData(id, rotation));
} }
public void AddPlayer(byte id) public void AddPlayer(byte id)
+36
View File
@@ -45,6 +45,7 @@ public class Server
//[0] = 5: create new player in client //[0] = 5: create new player in client
//[0] = 6: player ready //[0] = 6: player ready
//[0] = 7: player position update //[0] = 7: player position update
//[0] = 8: player rotation update
private void Receive() private void Receive()
{ {
@@ -179,6 +180,41 @@ public class Server
} }
} }
} }
if (data[0] == 8)
{
byte[] rotationASCII = new byte[data.Length - 1];
for (int i = 0; i < data.Length - 1; i++)
{
rotationASCII[i] = data[i + 1];
}
string[] rotationString = Encoding.ASCII.GetString(rotationASCII).Split(' ');
string x = rotationString[0];
string y = rotationString[1];
string z = rotationString[2];
string w = rotationString[3];
byte id = (byte)GetPlayerID(endPoint);
string sendRotation = x + " " + y + " " + z + " " + w;
byte[] sendRotationASCII = Encoding.ASCII.GetBytes(sendRotation);
byte[] send = new byte[sendRotationASCII.Length + 2];
send[0] = 8;
send[1] = id;
for (int i = 2; i < send.Length; i++)
{
send[i] = sendRotationASCII[i - 2];
}
foreach (IPEndPoint e in players)
{
if (id != GetPlayerID(e))
{
Send(send, e);
}
}
}
} }
catch (Exception e) catch (Exception e)
{ {
Binary file not shown.