Hi guys im new to godot and i want to challenge myself by making a game where the character moves using a handtracking python program i made. I connected the two together using sockets and i tested to see if i get the coordinates in the console by making a print and i do. Now i tried thinking of a way to move this character but i cant quite understand how to move it by using these coordinates. If u guys can give out any tips or points out stuff i did wrong please let me know as i want to improve more at godot.
Here is the python handtracking program
import cv2
import mediapipe as mp
import socket
mp_drawing = mp.solutions.drawing_utils
mphands = mp.solutions.hands
cap = cv2.VideoCapture(0)
hand = mphands.Hands()
# Socket setup
UDP_IP = "127.0.0.1" # IP address of the Godot machine
UDP_PORT = 5005 # Port number for communication
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
data, image = cap.read()
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
results = hand.process(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# Extract the coordinates of the tip of the pointer finger landmark (index 8)
cx, cy, cz = int(hand_landmarks.landmark[8].x * image.shape[1]), int(
hand_landmarks.landmark[8].y * image.shape[0]),
int(hand_landmarks.landmark[8].z * image.shape[1])
# Send the fingertip coordinates to the client (GDScript)
message = f"{cx},{cz}".encode()
sock.sendto(message, (UDP_IP, UDP_PORT))
# Draw a circle at the fingertip
cv2.circle(image, (cx, cy), 5, (0, 255, 0), -1)
cv2.imshow('Handtracker', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
And here is my gdscript
extends RigidBody
var target_position = Vector3.ZERO
var move_speed = 1000 # Adjust the movement speed as needed
var max_speed = 10 # Maximum speed limit
var server := UDPServer.new()
var peers = []
func _ready():
server.listen(5005)
move_character()
func _physics_process(delta):
server.poll() # Important!
if server.is_connection_available():
var peer : PacketPeerUDP = server.take_connection()
var pkt = peer.get_packet()
var data = pkt.get_string_from_utf8() # Get received data and remove leading/trailing whitespaces
var coordinates = data.split(",") # Split the data by comma to get individual coordinates
if coordinates.size() >= 2: # Ensure we have at least two coordinates (x and z)
var x = coordinates[0].to_float() # Convert the first coordinate to float
var z = coordinates[1].to_float() # Convert the second coordinate to float
print("Received coordinates - X:", x, " Z:", z) # Print the received coordinates
update_target_position(x, z)
func update_target_position(new_x, new_z):
target_position.x = new_x
target_position.z = new_z
func move_character():
var current_position = translation
# Calculate movement direction
var movement_direction = (target_position - current_position).normalized()
# Calculate movement velocity
var movement_velocity = movement_direction * move_speed
# Apply movement velocity
linear_velocity.x = movement_velocity.x
linear_velocity.z = movement_velocity.z
# Limit maximum speed
if linear_velocity.length() > max_speed:
linear_velocity = linear_velocity.normalized() * max_speed