I’m trying to create a 2d wheel where you spin it with your mouse to give it a velocity that it keeps. My current thought is using a rigid body where the change in rotations would become it’s angular velocity. The issue is, when calculating the rotation using atan2, there’s the seam where 2PI becomes 0 which messes with the calculation causing it to not spin at all.
Here’s the code:
extends RigidBody2D
var is_held : bool = false
var pre_click_rotation : float
@onready var previous_mouse_position : Vector2 = get_global_mouse_position()
func _process(delta: float) -> void:
if is_held:
var mouse_rotation = atan2(get_global_mouse_position().y, get_global_mouse_position().x) + (PI/2)
var previous_rotation = atan2(previous_mouse_position.y, previous_mouse_position.x) + (PI/2)
rotation += mouse_rotation - previous_rotation
previous_mouse_position = get_global_mouse_position()
func _on_button_button_down() -> void:
is_held = true
pre_click_rotation = rotation
func _on_button_button_up() -> void:
is_held = false
var mouse_rotation = atan2(get_global_mouse_position().y, get_global_mouse_position().x) + (PI/2)
var previous_rotation = atan2(previous_mouse_position.y, previous_mouse_position.x) + (PI/2)
angular_velocity += (mouse_rotation - previous_rotation) * 2