Skip to content
Snippets Groups Projects
Commit 34a7f2f8 authored by gawen.ackerman's avatar gawen.ackerman :robot:
Browse files

REMAKE : UDPSender

parent a6e1c0f8
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
...@@ -136,7 +136,7 @@ Transform: ...@@ -136,7 +136,7 @@ Transform:
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: 0.5, y: 0.5, z: 0.5, w: 0.5} m_LocalRotation: {x: 0.5, y: 0.5, z: 0.5, w: 0.5}
m_LocalPosition: {x: 0.131, y: 0.7, z: 4.963} m_LocalPosition: {x: 0.131, y: 0.7, z: 4.963}
m_LocalScale: {x: 3.21, y: -0.2247, z: 3.21} m_LocalScale: {x: 3.21, y: 0.2247, z: 3.21}
m_ConstrainProportionsScale: 1 m_ConstrainProportionsScale: 1
m_Children: [] m_Children: []
m_Father: {fileID: 7748414627489068023} m_Father: {fileID: 7748414627489068023}
......
...@@ -145,15 +145,21 @@ public class PlayerController : MonoBehaviour ...@@ -145,15 +145,21 @@ public class PlayerController : MonoBehaviour
float theta_x = newPosition[3]; float theta_x = newPosition[3];
float theta_y = newPosition[4] * rotationSensibility; float theta_y = newPosition[4] * rotationSensibility;
float theta_z = newPosition[5]; float theta_z = newPosition[5];
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"PositionManager x {x}, y {y}, z {z}"); stringBuilder.AppendLine($"PositionManager : [x {x}], [y {y}], [z {z}] - [theta_x {theta_x}], [theta_y {theta_y}], [theta_z {theta_z}]");
stringBuilder.AppendLine($"Transform x {this.transform.position.x}, y {this.transform.position.y}, z {this.transform.position.z}"); stringBuilder.AppendLine($"Transform P : [x {this.transform.position.x}], [y {this.transform.position.y}], [z {this.transform.position.z}]");
Vector3 movement = new Vector3(x, 0, z); Vector3 movement = new Vector3(x, 0, z);
this.transform.position += movement * Time.deltaTime * movementSensibility; this.transform.position += movement * Time.deltaTime * movementSensibility;
stringBuilder.AppendLine($"Updated Transform x {this.transform.position.x}, y {this.transform.position.y}, z {this.transform.position.z}"); stringBuilder.AppendLine($"Updated Transform P : [x {this.transform.position.x}], [y {this.transform.position.y}], [z {this.transform.position.z}]");
print(stringBuilder.ToString()); stringBuilder.AppendLine($"Transform R : [x {this.transform.rotation.x}], [y {this.transform.rotation.y}], [z {this.transform.rotation.z}]");
Quaternion newRotation = Quaternion.Euler(theta_x, theta_y, theta_z); Quaternion newRotation = Quaternion.Euler(theta_x, theta_y, theta_z);
this.transform.rotation = newRotation;
stringBuilder.AppendLine($"Updated Transform R : [x {this.transform.rotation.x} ], [y {this.transform.rotation.y} ], [z {this.transform.rotation.z}]");
print(stringBuilder.ToString());
} }
else else
{ {
......
import socket import socket
import time
import tkinter as tk import tkinter as tk
# UDP Configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5000
refreshRate = 0.1
# Window Configuration
WINDOW_SIZE = 400 WINDOW_SIZE = 400
CENTER_X = WINDOW_SIZE // 2 CENTER_X = WINDOW_SIZE // 2
CENTER_Y = WINDOW_SIZE // 2 CENTER_Y = WINDOW_SIZE // 2
# Create a UDP socket UDP_IP = "127.0.0.1"
UDP_PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
REFRESH_RATE = 100
mouse_x = 0
mouse_y = 0
y_theta = 0
is_tracking = False
def aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta): def aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta):
return f"{x},{y},{z},{x_theta},{y_theta},{z_theta}" return f"{x},{y},{z},{x_theta},{y_theta},{z_theta}"
def send_mouse_position(event): def update_position():
# Calculate relative position from the center of the window global mouse_x, mouse_y, y_theta, is_tracking
x = event.x - CENTER_X
y = event.y - CENTER_Y
# Dummy values for z and rotation angles x = mouse_x - CENTER_X
y = mouse_y - CENTER_Y
z = 0 z = 0
x_theta = 0 x_theta = 0
y_theta = 0
z_theta = 0 z_theta = 0
if (is_tracking != True):
x = 0
y = 0
z = 0
data = aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta) data = aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta)
sock.sendto(data.encode(), (UDP_IP, UDP_PORT)) sock.sendto(data.encode(), (UDP_IP, UDP_PORT))
print("Sent:", data) print("Sent:", data)
root.after(REFRESH_RATE, update_position)
def track_mouse(event):
global mouse_x, mouse_y
mouse_x = event.x
mouse_y = event.y
def adjust_rotation(event):
global y_theta
if event.delta > 0:
y_theta += 5
else:
y_theta -= 5
print(f"Rotation Angle (Y): {y_theta}°")
def draw_center_and_arrows(canvas):
CIRCLE_RADIUS = 20
canvas.create_oval(CENTER_X - CIRCLE_RADIUS, CENTER_Y - CIRCLE_RADIUS,
CENTER_X + CIRCLE_RADIUS, CENTER_Y + CIRCLE_RADIUS, fill="black")
ARROW_MARGIN = 30
ARROW_LENGTH = 150
canvas.create_line(CENTER_X, CENTER_Y - ARROW_MARGIN, CENTER_X, CENTER_Y - ARROW_LENGTH, arrow=tk.LAST, width=2)
canvas.create_line(CENTER_X + ARROW_MARGIN, CENTER_Y, CENTER_X + ARROW_LENGTH, CENTER_Y, arrow=tk.LAST, width=2)
canvas.create_line(CENTER_X, CENTER_Y + ARROW_MARGIN, CENTER_X, CENTER_Y + ARROW_LENGTH, arrow=tk.LAST, width=2)
canvas.create_line(CENTER_X - ARROW_MARGIN, CENTER_Y, CENTER_X - ARROW_LENGTH, CENTER_Y, arrow=tk.LAST, width=2)
def disable_tracking(event):
global mouse_x, mouse_y, y_theta, is_tracking
is_tracking = False
mouse_x = 0
mouse_y = 0
y_theta = 0
def enable_tracking(event):
global is_tracking
is_tracking = True
def main(): def main():
global root
root = tk.Tk() root = tk.Tk()
root.title("Mouse Tracker") root.title("Mouse Tracker")
root.geometry(f"{WINDOW_SIZE}x{WINDOW_SIZE}") root.geometry(f"{WINDOW_SIZE}x{WINDOW_SIZE}")
...@@ -42,8 +87,14 @@ def main(): ...@@ -42,8 +87,14 @@ def main():
canvas = tk.Canvas(root, width=WINDOW_SIZE, height=WINDOW_SIZE, bg="white") canvas = tk.Canvas(root, width=WINDOW_SIZE, height=WINDOW_SIZE, bg="white")
canvas.pack() canvas.pack()
# Bind mouse movement event draw_center_and_arrows(canvas)
root.bind('<Motion>', send_mouse_position)
root.bind('<Motion>', track_mouse)
root.bind("<MouseWheel>", adjust_rotation)
root.bind("<Enter>", enable_tracking)
root.bind("<Leave>", disable_tracking)
update_position()
root.mainloop() root.mainloop()
......
import socket
import tkinter as tk
# Window Configuration
WINDOW_SIZE = 400
CENTER_X = WINDOW_SIZE // 2
CENTER_Y = WINDOW_SIZE // 2
# UDP Configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Rotation angle
y_theta = 0
def aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta):
return f"{x},{y},{z},{x_theta},{y_theta},{z_theta}"
def send_mouse_position(event):
global y_theta
x = event.x - CENTER_X
y = event.y - CENTER_Y
z = 0
x_theta = 0
z_theta = 0
data = aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta)
sock.sendto(data.encode(), (UDP_IP, UDP_PORT))
print("Sent:", data)
def adjust_rotation(event):
global y_theta
if event.delta > 0:
y_theta += 5
else:
y_theta -= 5
print(f"Rotation Angle (Y): {y_theta}°")
def draw_center_and_arrows(canvas):
CIRCLE_RADIUS = 20
# Draw central circle
canvas.create_oval(CENTER_X - CIRCLE_RADIUS, CENTER_Y - CIRCLE_RADIUS, CENTER_X + CIRCLE_RADIUS, CENTER_Y + CIRCLE_RADIUS, fill="black")
# Draw arrows
ARROW_MARGIN = 30
ARROW_LENGTH = 150
canvas.create_line(CENTER_X, CENTER_Y - ARROW_MARGIN, CENTER_X, CENTER_Y - ARROW_LENGTH, arrow=tk.LAST, width=2) # Up
canvas.create_line(CENTER_X + ARROW_MARGIN, CENTER_Y, CENTER_X + ARROW_LENGTH, CENTER_Y, arrow=tk.LAST, width=2) # Right
canvas.create_line(CENTER_X, CENTER_Y + ARROW_MARGIN, CENTER_X, CENTER_Y + ARROW_LENGTH, arrow=tk.LAST, width=2) # Down
canvas.create_line(CENTER_X - ARROW_MARGIN, CENTER_Y, CENTER_X - ARROW_LENGTH, CENTER_Y, arrow=tk.LAST, width=2) # Left
def main():
root = tk.Tk()
root.title("Mouse Tracker")
root.geometry(f"{WINDOW_SIZE}x{WINDOW_SIZE}")
root.resizable(False, False)
canvas = tk.Canvas(root, width=WINDOW_SIZE, height=WINDOW_SIZE, bg="white")
canvas.pack()
draw_center_and_arrows(canvas)
# Bind mouse movement event
root.bind('<Motion>', send_mouse_position)
# Bind mouse wheel event
root.bind("<MouseWheel>", adjust_rotation)
root.mainloop()
if __name__ == "__main__":
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment