+5 votes

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?

in Engine by (703 points)

1 Answer

+5 votes
Best answer

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 ! http://docs.godotengine.org/en/latest/tutorials/2d/custom_drawing_in_2d.html

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

by (225 points)
edited by

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

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.