Rotate around a Vector2 in 2D

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

I have a Node2D “Asteroid” that I want to rotate around specific Vector2.
In my case, the Asteroid gets spawned randomly on the map. I want to rotate it around the center of my map, in my case Vector2(mapSize / 2, mapSize / 2)

How can I do this?

In Unity they had something like “RotateAround” but it seems this is not the case in Godot. I’m really bad in maths and don’t know how to work with sin and cos

what i usually do in cases like that is parent to a pivot(position2D) and rotate the pivot.

puzzlebeast | 2021-04-01 22:39

Have you considered making your map from -map_size / 2 to map_size / 2 instead of 0 to map_size? Then (0, 0) would be the center.

Aaron Franke | 2021-04-01 18:24

:bust_in_silhouette: Reply From: Roshi

Here is a very basic solution :

enter image description here

extends Node2D
var angle = 0
var rotation_speed = 1
var center
var radius

# Called when the node enters the scene tree for the first time.
func _ready():
    center = $Sprite.position
    radius = $Sprite2.position.distance_to(center)

func _process(delta):
    angle = lerp(angle, deg2rad(360), rotation_speed * delta)
    var coord = (Vector2(cos(angle), sin(angle)) * radius) + center
    $Sprite2.position = coord
    if angle > deg2rad(358):
	    angle = 0

You will have to find a way to have a linear speed and handle the looping

:bust_in_silhouette: Reply From: SteveSmith
func rotate_around_point(vec: Vector2, center: Vector2, angle:float) -> Vector2:
	var diff:Vector2 = vec - center
	diff = diff.rotated(angle)
	diff += center
	return diff