What does .rotated() do?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ronit5rg

I was seeing a lot of people use .rotated such as in the code below,

mob.rotation = direction
mob.set_Linear_velocity(Vector2(100, 200)).rotated(direction)

So, here, direction is a var containing an angle to which the node should be directed.
But, mob.rotation, already does the task.

So, what did .rotated() help with here?
Also, in general, I wanted to know the use of .rotated().

Thank You

In generell “rotated” rotates an Vector by the amount it’s given as a parameter, so for example, if you do

Vector2(1,0).rotated(PI)

you will get the Vector2(-1,0) since you’ve rotated the Vector by 180 degree and for that get the opposite direction from the old one. However in your specific example the rotated function doesn’t do anything, since rotated returns the new rotated Vector and not modifying the one in which it got called and since it doesn’t get assign to anything it will doesn’t do anything except costing the CPU some cycles :stuck_out_tongue: So I guess you’ve actually misplaced the brackets and you mean:

mob.setLinearvelocity(Vector2(100, 200).rotated(direction))

Which will rotate the Velocity by the direction amount itself (I guess this is done simple to make the linear velocity always relative to the current rotation it’s facing). Also setting the rotation property only changes the angle of the object (best visible when you do it on a sprite) and doesn’t have any other influence except changing the angle of the children objects too.

Hope this helps

Nophlock | 2019-08-25 15:20