Circular Movement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pasuva
:warning: Old Version Published before Godot 3 was released.

Hi everyone!! :slight_smile:

I need to create a script to make the move of an enemy. The enemy is a seagull. The seagull flies with circular and continuous motion. I need to use move function because the seagull needs to detect collisions. I dont know if in godot I can use something similar to euler function like Unity 5 in this case.

Ideas??

Thanks for any help :wink:

:bust_in_silhouette: Reply From: Kamil Lewan

First you need to declare circular movement. Like:
EDITED:

var speed = 1
var CircularMovement = Curve2D.new()
var r = 100
var pos = Vector2(200,200)
CircularMovement.add_point(pos+Vector2(r,0),Vector2(0,-r))
CircularMovement.add_point(pos+Vector2(0,r),Vector2(r,0))
CircularMovement.add_point(pos+Vector2(-r,0),Vector2(0,r))
CircularMovement.add_point(pos+Vector2(0,-r),Vector2(-r,0))
CircularMovement.add_point(pos+Vector2(r,0),Vector2(0,-r))
CircularMovement.set_bake_interval(1)

Then you should save somewhere position in baked_points, eg.:

var bppos = 0

If you can use move_to() instead of move(), everything will be easier. If so, use:

func _fixed_process(delta):
if bppos + delta*speed >= CircularMovement.get_baked_points().size():
    bppos += delta*speed - CircularMovement.get_baked_points().size()
else:
    KinematicBody2D.move_pos(CircularMovement.get_baked_points()[round(bppos)])
    bppos += delta*speed

SHOULD work, but not best solution.

This is bad due to ignoring the delta value of the fixed process function.

timoschwarzer | 2017-07-12 23:47

Yup, that’s true. It should be something like this:

KinematicBody2D.move_pos(CircularMovement.get_baked_points()[round(bppos)])
bppos += delta*1

And o/c condition: if bppos is at the end of curve; then pbpos = 0
Thanks for caution :slight_smile:

Kamil Lewan | 2017-07-13 10:39

Thanks for editing! Now this should work find on any computer / framerate.

timoschwarzer | 2017-07-13 10:56

Many thanks I will try it :slight_smile:

pasuva | 2017-07-17 07:47

Hey, thanks! That was really helpful :slight_smile:

Do you know how can I change the points to get an elliptical orbit instead of a circular one?

hiulit | 2019-04-18 14:13

:bust_in_silhouette: Reply From: Zero

I know this has been answered, but I found a simpler solution while searching around that I think might be useful for someone.

var RotateSpeed = 5
var Radius = 100
var _centre
var _angle = 0

func _ready():
	set_process(true)
    _centre = self.get_pos()

func _process(delta): 
       _angle += RotateSpeed * delta;
 
		var offset = Vector2(sin(_angle), cos(_angle)) * Radius;
		var pos = _centre + offset
		self.set_pos(pos) 

And just replace the last line “self.set_pos(pos)” for move(pos) if you are using a KinematicBody2D.