Trouble with clock arms movement in a clock puzzle

Godot Version

godot4.3

hello everyone, im making a 2d puzzle game and i have a clock puzzle where the player places the clock hands and moves it with the mouse wheel to place it at the correct time.
i used chatgbt to help cus i have no idea how to work this in now the code works the clock hands do rotate but they dont rotate as normal clock hands would the hour hands follows the minute hand too closely and i think its not registering the correct time probably but for now i want to fix the clock hands movement.

this is my code for the rotation function
and ive attached a video to show the issue better

func _input(event):
	if clock_hands_placed:
		if event is InputEventMouseButton:
			if event.button_index == MOUSE_BUTTON_WHEEL_UP:
				rotate_hands(rotation_speed)
			elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
				rotate_hands(-rotation_speed)
			
func rotate_hands(rotation_delta):
	minute_hand.rotation += rotation_delta
	minute_hand.rotation = fmod(minute_hand.rotation, TAU)
	var fractional_hour = (minute_hand.rotation/ TAU) * 12.0
	var hour_angle = (fractional_hour + (current_minute / 60.0)) * (TAU / 12)
	hour_hand.rotation = hour_angle
	current_minute = int((minute_hand.rotation / TAU) * 60.0)
	current_hour = int(fmod(fractional_hour, 12))
	check_time_solution()

Whats your scenetree setup? Is one hand the child of another?
You should be able to apply a 12th of the minute-hand rotation to the hour-hand:

func rotate_hands(rotation_delta):
	minute_hand.rotation += rotation_delta
	minute_hand.rotation = fmod(minute_hand.rotation, TAU)
	var fractional_hour = (minute_hand.rotation/ TAU) * 12.0
	var hour_angle = (fractional_hour + (current_minute / 60.0)) * (TAU / 12)
	hour_hand.rotation += rotation_delta / 12
	hour_hand.rotation = fmod(hour_hand.rotation, TAU)
	current_minute = int((minute_hand.rotation / TAU) * 60.0)
	current_hour = int(fmod(fractional_hour, 12))
	check_time_solution()

I have a node2d with an area2d and the clock arms and clock image as children but the problem might have been my writing, i tried the code you provided before posting here and it made the hour hand move simultaneously with the minute hand not independently. i changed the code yesterday to something else and managed to get it working but was presented with a new problem anyway i tried writing the code u provided again and it gave me the same problem it did before then i tried coping it and pasting it directly and surprise it worked! i most have been writing something wrong all along. thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.