Reputation: 21
I am aware that functions such as look_at() and rotate() are available.
I am doing a little experiment on Godot where I'm attempting to rotate my character's arm towards the cursor, by transforming the mouse x and y position into a polar coordinate form, where then I can use the radian value to rotate the arm.
I've written this simple code that calculates the radian using the formula, by taking in the mouse position.
extends Sprite2D
func _process(delta: float) -> void:
var mpos = get_local_mouse_position()
var rad = sqrt(mpos.x**2 + mpos.y**2)
rotation = rad
While the arm does rotate, it does not rotate towards the cursor. Instead, it spins seemingly uncontrollably every time the cursor moves.
Can anyone point out where the code flaw is? Or is it simply not possible?
Note:
The rotation property is intended to take radians.
I tried graphing the polar coordinate, all is fine because the angle does not change no matter how far (how long the radian is) from the arm's pivot point.
Upvotes: 1
Views: 53
Reputation: 21
As it turns out, i made a silly mistake. I was supposed to use the formula to find the angle (theta) and not the radian itself.
var mpos = get_global_mouse_position()
rotation = atan2(mpos.y,mpos.x)
Works exactly like the look_at()
function.
Upvotes: 1