Is it possible to draw a circular arc?

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

In the CanvasItem I see there is a draw_circle(…), but is there a way to draw only a part of a circle, from one angle to another?

:bust_in_silhouette: Reply From: Julian Murgia

Ok, actually I have a code ready for you, but the tutorial now needs to be written - and it will, but I’ll need some time. Don’t worry, it will be done.
edit : It’s done ! Custom drawing in 2D — Godot Engine (latest) documentation in English

So, here are 2 functions for you to draw arcs. The first one draws only the circle, the second one draws a polygon. Parameters angles are in degrees, not in radians (on purpose, to fit with Godot’s way to use angles in GDScript - see Vector2.angle()) . Thus, an angle of 0 is a clock needle pointing at noon.


func draw_circle_arc( center, radius, angleFrom, angleTo, color ):
	var nbPoints = 32
	var pointsArc = Vector2Array()
	
	for i in range(nbPoints+1):
		var anglePoint = angleFrom + i*(angleTo-angleFrom)/nbPoints - 90
		var point = center + Vector2( cos(deg2rad(anglePoint)), sin(deg2rad(anglePoint)) )* radius
		pointsArc.push_back( point )
	
	for indexPoint in range(nbPoints):
		printt(indexPoint, pointsArc[indexPoint], pointsArc[indexPoint+1])
		draw_line(pointsArc[indexPoint], pointsArc[indexPoint+1], color)
	pass

func draw_circle_arc_poly( center, radius, angleFrom, angleTo, color ):
	var nbPoints = 32
	var pointsArc = Vector2Array()
	pointsArc.push_back(center)
	var colors = ColorArray([color])
	
	for i in range(nbPoints+1):
		var anglePoint = angleFrom + i*(angleTo-angleFrom)/nbPoints - 90
		pointsArc.push_back(center + Vector2( cos( deg2rad(anglePoint) ), sin( deg2rad(anglePoint) ) )* radius)
	draw_polygon(pointsArc, colors)
	pass

Can you correct this in the docs? It seems to be wrong even with the /latest/ url.

Rami Awar | 2020-04-28 13:02