This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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

Godot version 3.2.3
in Engine by (45 points)

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.

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

2 Answers

0 votes

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

by (81 points)
+1 vote
func rotate_around_point(vec: Vector2, center: Vector2, angle:float) -> Vector2:
    var diff:Vector2 = vec - center
    diff = diff.rotated(angle)
    diff += center
    return diff
by (1,055 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.