Godot 4, using raycast2d to draw a circle out of curve2d points

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

Im making a magic game in godot 4 and am using a path2d and pathfollow2d to make spell paths. for one of these spell trajectories, I need the spell to orbit around the player, so I have this code

extends CharacterBody2D
@onready var spellpath = $Spell_path
@onready var spellraycast = $Spell_path/RayCast2D

func _ready():
	var orbit = Curve2D.new()
	for x in range(0, 360):

I’m not sure what the question is here?

jgodfrey | 2023-03-06 04:05

Sorry, I forgot to ask the question :(, it doesn’t work for some reason. The ray just shows up pointing at the bottom left and no points are created. I don’t know what I’m doing wrong or if this method just doesn’t work

AI_notFound | 2023-03-06 12:46

:bust_in_silhouette: Reply From: Wakatta

You’re using a ray cast to do what now?
That’s pretty much overkill

Math is the solution here

extends CharacterBody2D
@onready var spellpath = $Spell_path
@onready var spellraycast = $Spell_path/RayCast2D

# adds points to curve2D in a circle
# - center: the center point of the circle
# - radius: controls the size of the circle
# - points: the amount of points to add to the curve2D
func set_spell_path(curve: Curve2D, center: Vector2, radius: float, points: int):
	for i in range(points +1):
		# Calculate the angle for this point on the circle
		var angle = (i / float(points)) * TAU
		# Calculate the x and y coordinates of the point on the circle
		var x = center.x + radius * cos(angle)
		var y = center.y + radius * sin(angle)
		# Add the point to the curve
		curve.add_point(Vector2(x, y))

func _ready():
        set_spell_path(spellpath.curve, global_position, 100, 20)

This is one of those functions that you will need continually and you should add it to your toolkit, removing the curve and returning an Array of vector2’s also maybe adding another param to create ellipticals

Godot4 has a Geometry2D class that also does the above but as said before this is good to know