Custom drawing in 2D

Introduction

Godot has nodes to draw sprites, polygons, particles, text, and many other common game development needs. However, if you need something specific not covered with the standard nodes you can make any 2D node (for example, Control or Node2D-based) draw on screen using custom commands.

Custom drawing in a 2D node is really useful. Here are some use cases:

  • Drawing shapes or logic that existing nodes can’t do, such as an image with trails or a special animated polygon.

  • Drawing a large number of simple objects, such as a grid or a board for a 2d game. Custom drawing avoids the overhead of using a large number of nodes, possibly lowering memory usage and improving performance.

  • Making a custom UI control. There are plenty of controls available, but when you have unusual needs, you will likely need a custom control.

Drawing

Add a script to any CanvasItem derived node, like Control or Node2D. Then override the _draw() function.

GDScriptC#

  1. extends Node2D
  2. func _draw():
  3. pass # Your draw commands here.
  1. using Godot;
  2. public partial class MyNode2D : Node2D
  3. {
  4. public override void _Draw()
  5. {
  6. // Your draw commands here.
  7. }
  8. }

Draw commands are described in the CanvasItem class reference. There are plenty of them and we will see some of them in the examples below.

Updating

The _draw function is only called once, and then the draw commands are cached and remembered, so further calls are unnecessary.

If re-drawing is required because a variable or something else changed, call CanvasItem.queue_redraw in that same node and a new _draw() call will happen.

Here is a little more complex example, where we have a texture variable that can be modified at any time, and using a setter, it forces a redraw of the texture when modified:

GDScriptC#

  1. extends Node2D
  2. @export var texture : Texture2D:
  3. set(value):
  4. texture = value
  5. queue_redraw()
  6. func _draw():
  7. draw_texture(texture, Vector2())
  1. using Godot;
  2. public partial class MyNode2D : Node2D
  3. {
  4. private Texture2D _texture;
  5. [Export]
  6. public Texture2D Texture
  7. {
  8. get
  9. {
  10. return _texture;
  11. }
  12. set
  13. {
  14. _texture = value;
  15. QueueRedraw();
  16. }
  17. }
  18. public override void _Draw()
  19. {
  20. DrawTexture(_texture, new Vector2());
  21. }
  22. }

To see it in action, you can set the texture to be the Godot icon on the editor by dragging and dropping the default icon.svg from the FileSystem tab to the Texture property on the Inspector tab. When changing the Texture property value while the previous script is running, the texture will also change automatically.

In some cases, we may need to redraw every frame. For this, call queue_redraw from the _process method, like this:

GDScriptC#

  1. extends Node2D
  2. func _draw():
  3. pass # Your draw commands here.
  4. func _process(_delta):
  5. queue_redraw()
  1. using Godot;
  2. public partial class MyNode2D : Node2D
  3. {
  4. public override void _Draw()
  5. {
  6. // Your draw commands here.
  7. }
  8. public override void _Process(double delta)
  9. {
  10. QueueRedraw();
  11. }
  12. }

Coordinates and line width alignment

The drawing API uses the CanvasItem’s coordinate system, not necessarily pixel coordinates. This means _draw() uses the coordinate space created after applying the CanvasItem’s transform. Additionally, you can apply a custom transform on top of it by using draw_set_transform or draw_set_transform_matrix.

When using draw_line, you should consider the width of the line. When using a width that is an odd size, the position of the start and end points should be shifted by 0.5 to keep the line centered, as shown below.

../../_images/draw_line.png

GDScriptC#

  1. func _draw():
  2. draw_line(Vector2(1.5, 1.0), Vector2(1.5, 4.0), Color.GREEN, 1.0)
  3. draw_line(Vector2(4.0, 1.0), Vector2(4.0, 4.0), Color.GREEN, 2.0)
  4. draw_line(Vector2(7.5, 1.0), Vector2(7.5, 4.0), Color.GREEN, 3.0)
  1. public override void _Draw()
  2. {
  3. DrawLine(new Vector2(1.5f, 1.0f), new Vector2(1.5f, 4.0f), Colors.Green, 1.0f);
  4. DrawLine(new Vector2(4.0f, 1.0f), new Vector2(4.0f, 4.0f), Colors.Green, 2.0f);
  5. DrawLine(new Vector2(7.5f, 1.0f), new Vector2(7.5f, 4.0f), Colors.Green, 3.0f);
  6. }

The same applies to the draw_rect method with filled = false.

../../_images/draw_rect.png

GDScriptC#

  1. func _draw():
  2. draw_rect(Rect2(1.0, 1.0, 3.0, 3.0), Color.GREEN)
  3. draw_rect(Rect2(5.5, 1.5, 2.0, 2.0), Color.GREEN, false, 1.0)
  4. draw_rect(Rect2(9.0, 1.0, 5.0, 5.0), Color.GREEN)
  5. draw_rect(Rect2(16.0, 2.0, 3.0, 3.0), Color.GREEN, false, 2.0)
  1. public override void _Draw()
  2. {
  3. DrawRect(new Rect2(1.0f, 1.0f, 3.0f, 3.0f), Colors.Green);
  4. DrawRect(new Rect2(5.5f, 1.5f, 2.0f, 2.0f), Colors.Green, false, 1.0f);
  5. DrawRect(new Rect2(9.0f, 1.0f, 5.0f, 5.0f), Colors.Green);
  6. DrawRect(new Rect2(16.0f, 2.0f, 3.0f, 3.0f), Colors.Green, false, 2.0f);
  7. }

Antialiased drawing

Godot offers method parameters in draw_line to enable antialiasing, but not all custom drawing methods offer this antialiased parameter.

For custom drawing methods that don’t provide an antialiased parameter, you can enable 2D MSAA instead, which affects rendering in the entire viewport. This provides high-quality antialiasing, but a higher performance cost and only on specific elements. See 2D antialiasing for more information.

Here is a comparison of a line of minimal width (width=-1) drawn with antialiased=false, antialiased=true, and antialiased=false with 2D MSAA 2x, 4x, and 8x enabled.

../../_images/draw_antialiasing_options.webp

Tools

Drawing your own nodes might also be desired while running them in the editor. This can be used as a preview or visualization of some feature or behavior.

To do this, you can use the tool annotation on both GDScript and C#. See the example below and Running code in the editor for more information.

Example 1: drawing a custom shape

We will now use the custom drawing functionality of the Godot Engine to draw something that Godot doesn’t provide functions for. We will recreate the Godot logo but with code- only using drawing functions.

You will have to code a function to perform this and draw it yourself.

Note

The following instructions use a fixed set of coordinates that could be too small for high resolution screens (larger than 1080p). If that is your case, and the drawing is too small consider increasing your window scale in the project setting Display > Window > Stretch > Scale to adjust the project to a higher resolution (a 2 or 4 scale tends to work well).

Drawing a custom polygon shape

While there is a dedicated node to draw custom polygons ( Polygon2D), we will use in this case exclusively lower level drawing functions to combine them on the same node and be able to create more complex shapes later on.

First, we will define a set of points -or X and Y coordinates- that will form the base of our shape:

GDScriptC#

  1. extends Node2D
  2. var coords_head : Array = [
  3. [ 22.952, 83.271 ], [ 28.385, 98.623 ],
  4. [ 53.168, 107.647 ], [ 72.998, 107.647 ],
  5. [ 99.546, 98.623 ], [ 105.048, 83.271 ],
  6. [ 105.029, 55.237 ], [ 110.740, 47.082 ],
  7. [ 102.364, 36.104 ], [ 94.050, 40.940 ],
  8. [ 85.189, 34.445 ], [ 85.963, 24.194 ],
  9. [ 73.507, 19.930 ], [ 68.883, 28.936 ],
  10. [ 59.118, 28.936 ], [ 54.494, 19.930 ],
  11. [ 42.039, 24.194 ], [ 42.814, 34.445 ],
  12. [ 33.951, 40.940 ], [ 25.637, 36.104 ],
  13. [ 17.262, 47.082 ], [ 22.973, 55.237 ]
  14. ]
  1. using Godot;
  2. public partial class MyNode2D : Node2D
  3. {
  4. private float[,] _coordsHead =
  5. {
  6. { 22.952f, 83.271f }, { 28.385f, 98.623f },
  7. { 53.168f, 107.647f }, { 72.998f, 107.647f },
  8. { 99.546f, 98.623f }, { 105.048f, 83.271f },
  9. { 105.029f, 55.237f }, { 110.740f, 47.082f },
  10. { 102.364f, 36.104f }, { 94.050f, 40.940f },
  11. { 85.189f, 34.445f }, { 85.963f, 24.194f },
  12. { 73.507f, 19.930f }, { 68.883f, 28.936f },
  13. { 59.118f, 28.936f }, { 54.494f, 19.930f },
  14. { 42.039f, 24.194f }, { 42.814f, 34.445f },
  15. { 33.951f, 40.940f }, { 25.637f, 36.104f },
  16. { 17.262f, 47.082f }, { 22.973f, 55.237f }
  17. };
  18. }

This format, while compact, is not the one that Godot understands to draw a polygon. In a different scenario we could have to load these coordinates from a file or calculate the positions while the application is running, so some transformation may be needed.

To transform these coordinates into the right format, we will create a new method float_array_to_Vector2Array(). Then we will override the _ready() function, which Godot will call only once -at the start of the execution- to load those coordinates into a variable:

GDScriptC#

  1. var head : PackedVector2Array
  2. func float_array_to_Vector2Array(coords : Array) -> PackedVector2Array:
  3. # Convert the array of floats into a PackedVector2Array.
  4. var array : PackedVector2Array = []
  5. for coord in coords:
  6. array.append(Vector2(coord[0], coord[1]))
  7. return array
  8. func _ready():
  9. head = float_array_to_Vector2Array(coords_head);
  1. private Vector2[] _head;
  2. private Vector2[] FloatArrayToVector2Array(float[,] coords)
  3. {
  4. // Convert the array of floats into an array of Vector2.
  5. int size = coords.GetUpperBound(0);
  6. Vector2[] array = new Vector2[size + 1];
  7. for (int i = 0; i <= size; i++)
  8. {
  9. array[i] = new Vector2(coords[i, 0], coords[i, 1]);
  10. }
  11. return array;
  12. }
  13. public override void _Ready()
  14. {
  15. _head = FloatArrayToVector2Array(_coordsHead);
  16. }

To finally draw our first shape, we will use the method draw_polygon and pass the points (as an array of Vector2 coordinates) and its color, like this:

GDScriptC#

  1. func _draw():
  2. # We are going to paint with this color.
  3. var godot_blue : Color = Color("478cbf")
  4. # We pass the PackedVector2Array to draw the shape.
  5. draw_polygon(head, [ godot_blue ])
  1. public override void _Draw()
  2. {
  3. // We are going to paint with this color.
  4. Color godotBlue = new Color("478cbf");
  5. // We pass the array of Vector2 to draw the shape.
  6. DrawPolygon(_head, new Color[]{ godotBlue });
  7. }

When running it you should see something like this:

../../_images/draw_godot_logo_polygon.webp

Note the lower part of the logo looks segmented- this is because a low amount of points were used to define that part. To simulate a smooth curve, we could add more points to our array, or maybe use a mathematical function to interpolate a curve and create a smooth shape from code (see example 2).

Polygons will always connect its last defined point to its first one in order to have a closed shape.

Drawing connected lines

Drawing a sequence of connected lines that don’t close down to form a polygon is very similar to the previous method. We will use a connected set of lines to draw Godot’s logo mouth.

First, we will define the list of coordinates that form the mouth shape, like this:

GDScriptC#

  1. var coords_mouth = [
  2. [ 22.817, 81.100 ], [ 38.522, 82.740 ],
  3. [ 39.001, 90.887 ], [ 54.465, 92.204 ],
  4. [ 55.641, 84.260 ], [ 72.418, 84.177 ],
  5. [ 73.629, 92.158 ], [ 88.895, 90.923 ],
  6. [ 89.556, 82.673 ], [ 105.005, 81.100 ]
  7. ]
  1. private float[,] _coordsMouth =
  2. {
  3. { 22.817f, 81.100f }, { 38.522f, 82.740f },
  4. { 39.001f, 90.887f }, { 54.465f, 92.204f },
  5. { 55.641f, 84.260f }, { 72.418f, 84.177f },
  6. { 73.629f, 92.158f }, { 88.895f, 90.923f },
  7. { 89.556f, 82.673f }, { 105.005f, 81.100f }
  8. };

We will load these coordinates into a variable and define an additional variable with the configurable line thickness:

GDScriptC#

  1. var mouth : PackedVector2Array
  2. var _mouth_width : float = 4.4
  3. func _ready():
  4. head = float_array_to_Vector2Array(coords_head);
  5. mouth = float_array_to_Vector2Array(coords_mouth);
  1. private Vector2[] _mouth;
  2. private float _mouthWidth = 4.4f;
  3. public override void _Ready()
  4. {
  5. _head = FloatArrayToVector2Array(_coordsHead);
  6. _mouth = FloatArrayToVector2Array(_coordsMouth);
  7. }

And finally we will use the method draw_polyline to actually draw the line, like this:

GDScriptC#

  1. func _draw():
  2. # We will use white to draw the line.
  3. var white : Color = Color.WHITE
  4. var godot_blue : Color = Color("478cbf")
  5. draw_polygon(head, [ godot_blue ])
  6. # We draw the while line on top of the previous shape.
  7. draw_polyline(mouth, white, _mouth_width)
  1. public override void _Draw()
  2. {
  3. // We will use white to draw the line.
  4. Color white = Colors.White;
  5. Color godotBlue = new Color("478cbf");
  6. DrawPolygon(_head, new Color[]{ godotBlue });
  7. // We draw the while line on top of the previous shape.
  8. DrawPolyline(_mouth, white, _mouthWidth);
  9. }

You should get the following output:

../../_images/draw_godot_logo_polyline.webp

Unlike draw_polygon(), polylines can only have a single unique color for all its points (the second argument). This method has 2 additional arguments: the width of the line (which is as small as possible by default) and enabling or disabling the antialiasing (it is disabled by default).

The order of the _draw calls is important- like with the Node positions on the tree hierarchy, the different shapes will be drawn from top to bottom, resulting in the latest shapes hiding earlier ones if they overlap. In this case we want the mouth drawn over the head, so we put it afterwards.

Notice how we can define colors in different ways, either with a hexadecimal code or a predefined color name. Check the class Color for other constants and ways to define Colors.

Drawing circles

To create the eyes, we are going to add 4 additional calls to draw the eye shapes, in different sizes, colors and positions.

To draw a circle, you position it based on its center using the draw_circle method. The first parameter is a Vector2 with the coordinates of its center, the second is its radius, and the third is its color:

GDScriptC#

  1. func _draw():
  2. var white : Color = Color.WHITE
  3. var godot_blue : Color = Color("478cbf")
  4. var grey : Color = Color("414042")
  5. draw_polygon(head, [ godot_blue ])
  6. draw_polyline(mouth, white, _mouth_width)
  7. # Four circles for the 2 eyes: 2 white, 2 grey.
  8. draw_circle(Vector2(42.479, 65.4825), 9.3905, white)
  9. draw_circle(Vector2(85.524, 65.4825), 9.3905, white)
  10. draw_circle(Vector2(43.423, 65.92), 6.246, grey)
  11. draw_circle(Vector2(84.626, 66.008), 6.246, grey)
  1. public override void _Draw()
  2. {
  3. Color white = Colors.White;
  4. Color godotBlue = new Color("478cbf");
  5. Color grey = new Color("414042");
  6. DrawPolygon(_head, new Color[]{ godotBlue });
  7. DrawPolyline(_mouth, white, _mouthWidth);
  8. // Four circles for the 2 eyes: 2 white, 2 grey.
  9. DrawCircle(new Vector2(42.479f, 65.4825f), 9.3905f, white);
  10. DrawCircle(new Vector2(85.524f, 65.4825f), 9.3905f, white);
  11. DrawCircle(new Vector2(43.423f, 65.92f), 6.246f, grey);
  12. DrawCircle(new Vector2(84.626f, 66.008f), 6.246f, grey);
  13. }

When executing it, you should have something like this:

../../_images/draw_godot_logo_circle.webp

For partial, unfilled arcs (portions of a circle shape between certain arbitrary angles), you can use the method draw_arc.

Drawing lines

To draw the final shape (the nose) we will use a line to approximate it.

draw_line can be used to draw a single segment by providing its start and end coordinates as arguments, like this:

GDScriptC#

  1. func _draw():
  2. var white : Color = Color.WHITE
  3. var godot_blue : Color = Color("478cbf")
  4. var grey : Color = Color("414042")
  5. draw_polygon(head, [ godot_blue ])
  6. draw_polyline(mouth, white, _mouth_width)
  7. draw_circle(Vector2(42.479, 65.4825), 9.3905, white)
  8. draw_circle(Vector2(85.524, 65.4825), 9.3905, white)
  9. draw_circle(Vector2(43.423, 65.92), 6.246, grey)
  10. draw_circle(Vector2(84.626, 66.008), 6.246, grey)
  11. # Draw a short but thick white vertical line for the nose.
  12. draw_line(Vector2(64.273, 60.564), Vector2(64.273, 74.349), white, 5.8)
  1. public override void _Draw()
  2. {
  3. Color white = Colors.White;
  4. Color godotBlue = new Color("478cbf");
  5. Color grey = new Color("414042");
  6. DrawPolygon(_head, new Color[]{ godotBlue });
  7. DrawPolyline(_mouth, white, _mouthWidth);
  8. DrawCircle(new Vector2(42.479f, 65.4825f), 9.3905f, white);
  9. DrawCircle(new Vector2(85.524f, 65.4825f), 9.3905f, white);
  10. DrawCircle(new Vector2(43.423f, 65.92f), 6.246f, grey);
  11. DrawCircle(new Vector2(84.626f, 66.008f), 6.246f, grey);
  12. // Draw a short but thick white vertical line for the nose.
  13. DrawLine(new Vector2(64.273f, 60.564f), new Vector2(64.273f, 74.349f),
  14. white, 5.8f);
  15. }

You should now be able to see the following shape on screen:

../../_images/draw_godot_logo_line.webp

Note that if multiple unconnected lines are going to be drawn at the same time, you may get additional performance by drawing all of them in a single call, using the draw_multiline method.

Drawing text

While using the Label Node is the most common way to add text to your application, the low level _draw function includes functionality to add text to your custom Node drawing. We will use it to add the name “GODOT” under the robot head.

We will use the draw_string method to do it, like this:

GDScriptC#

  1. var default_font : Font = ThemeDB.fallback_font;
  2. func _draw():
  3. var white : Color = Color.WHITE
  4. var godot_blue : Color = Color("478cbf")
  5. var grey : Color = Color("414042")
  6. draw_polygon(head, [ godot_blue ])
  7. draw_polyline(mouth, white, _mouth_width)
  8. draw_circle(Vector2(42.479, 65.4825), 9.3905, white)
  9. draw_circle(Vector2(85.524, 65.4825), 9.3905, white)
  10. draw_circle(Vector2(43.423, 65.92), 6.246, grey)
  11. draw_circle(Vector2(84.626, 66.008), 6.246, grey)
  12. draw_line(Vector2(64.273, 60.564), Vector2(64.273, 74.349), white, 5.8)
  13. # Draw GODOT text below the logo with the default font, size 22.
  14. draw_string(default_font, Vector2(20, 130), "GODOT",
  15. HORIZONTAL_ALIGNMENT_CENTER, 90, 22)
  1. private Font _defaultFont = ThemeDB.FallbackFont;
  2. public override void _Draw()
  3. {
  4. Color white = Colors.White;
  5. Color godotBlue = new Color("478cbf");
  6. Color grey = new Color("414042");
  7. DrawPolygon(_head, new Color[]{ godotBlue });
  8. DrawPolyline(_mouth, white, _mouthWidth);
  9. DrawCircle(new Vector2(42.479f, 65.4825f), 9.3905f, white);
  10. DrawCircle(new Vector2(85.524f, 65.4825f), 9.3905f, white);
  11. DrawCircle(new Vector2(43.423f, 65.92f), 6.246f, grey);
  12. DrawCircle(new Vector2(84.626f, 66.008f), 6.246f, grey);
  13. DrawLine(new Vector2(64.273f, 60.564f), new Vector2(64.273f, 74.349f),
  14. white, 5.8f);
  15. // Draw GODOT text below the logo with the default font, size 22.
  16. DrawString(_defaultFont, new Vector2(20f, 130f), "GODOT",
  17. HorizontalAlignment.Center, 90, 22);
  18. }

Here we first load into the defaultFont variable the configured default theme font (a custom one can be set instead) and then we pass the following parameters: font, position, text, horizontal alignment, width, and font size.

You should see the following on your screen:

../../_images/draw_godot_logo_text.webp

Additional parameters as well as other methods related to text and characters can be found on the CanvasItem class reference.

Show the drawing while editing

While the code so far is able to draw the logo on a running window, it will not show up on the 2D view on the editor. In certain cases you would also like to show your custom Node2D or control on the editor, to position and scale it appropriately, like most other nodes do.

To show the logo directly on the editor (without running it), you can use the @tool annotation to request the custom drawing of the node to also appear while editing, like this:

GDScriptC#

  1. @tool
  2. extends Node2D
  1. using Godot;
  2. [Tool]
  3. public partial class MyNode2D : Node2D

You will need to save your scene, rebuild your project (for C# only) and reload the current scene manually at the menu option Scene > Reload Saved Scene to refresh the current node in the 2D view the first time you add or remove the @tool annotation.

Animation

If we wanted to make the custom shape change at runtime, we could modify the methods called or its arguments at execution time, or apply a transform.

For example, if we want the custom shape we just designed to rotate, we could add the following variable and code to the _ready and _process methods:

GDScriptC#

  1. extends Node2D
  2. @export var rotation_speed : float = 1 # In radians per second.
  3. func _ready():
  4. rotation = 0
  5. ...
  6. func _process(delta: float):
  7. rotation -= rotation_speed * delta
  1. [Export]
  2. public float RotationSpeed { get; set; } = 1.0f; // In radians per second.
  3. public override void _Ready()
  4. {
  5. Rotation = 0;
  6. ...
  7. }
  8. public override void _Process(double delta)
  9. {
  10. Rotation -= RotationSpeed * (float)delta;
  11. }

The problem with the above code is that because we have created the points approximately on a rectangle starting from the upper left corner, the (0, 0) coordinate and extending to the right and down, we see that the rotation is done using the top left corner as pivot. A position transform change on the node won’t help us here, as the rotation transform is applied first.

While we could rewrite all of the points’ coordinates to be centered around (0, 0), including negative coordinates, that would be a lot of work.

One possible way to work around this is to use the lower level draw_set_transform method to fix this issue, translating all points in the CanvasItem’s own space, and then moving it back to its original place with a regular node transform, either in the editor or in code, like this:

GDScriptC#

  1. func _ready():
  2. rotation = 0
  3. position = Vector2(60, 60)
  4. ...
  5. func _draw():
  6. draw_set_transform(Vector2(-60, -60))
  7. ...
  1. public override void _Ready()
  2. {
  3. Rotation = 0;
  4. Position = new Vector2(60, 60);
  5. ...
  6. }
  7. public override void _Draw()
  8. {
  9. DrawSetTransform(new Vector2(-60.0f, -60.0f));
  10. ...
  11. }

This is the result, rotating around a pivot now on (60, 60):

../../_images/draw_godot_rotation.webp

If what we wanted to animate was a property inside the _draw() call, we must remember to call queue_redraw() to force a refresh, as otherwise it would not be updated on screen.

For example, this is how we can make the robot appear to open and close its mouth, by changing the width of its mouth line follow a sinusoidal (sin) curve:

GDScriptC#

  1. var _mouth_width : float = 4.4
  2. var _max_width : float = 7
  3. var _time : float = 0
  4. func _process(delta : float):
  5. _time += delta
  6. _mouth_width = abs(sin(_time) * _max_width)
  7. queue_redraw()
  8. func _draw():
  9. ...
  10. draw_polyline(mouth, white, _mouth_width)
  11. ...
  1. private float _mouthWidth = 4.4f;
  2. private float _maxWidth = 7f;
  3. private float _time = 0f;
  4. public override void _Process(double delta)
  5. {
  6. _time += (float)delta;
  7. _mouthWidth = Mathf.Abs(Mathf.Sin(_time) * _maxWidth);
  8. QueueRedraw();
  9. }
  10. public override void _Draw()
  11. {
  12. ...
  13. DrawPolyline(_mouth, white, _mouthWidth);
  14. ...
  15. }

It will look somewhat like this when run:

../../_images/draw_godot_mouth_animation.webp

Please note that _mouth_width is a user defined property like any other and it or any other used as a drawing argument can be animated using more standard and high level methods such as a Tween or an AnimationPlayer Node. The only difference is that a queue_redraw() call is needed to apply those changes so they get shown on screen.

Example 2: drawing a dynamic line

The previous example was useful to learn how to draw and modify nodes with custom shapes and animations. This could have some advantages, such as using exact coordinates and vectors for drawing, rather than bitmaps -which means they will scale well when transformed on screen. In some cases, similar results could be achieved composing higher level functionality with nodes such as sprites or AnimatedSprites loading SVG resources (which are also images defined with vectors) and the AnimationPlayer node.

In other cases that will not be possible because we will not know what the resulting graphical representation will be before running the code. Here we will see how to draw a dynamic line whose coordinates are not known beforehand, and are affected by the user’s input.

Drawing a straight line between 2 points

Let’s assume we want to draw a straight line between 2 points, the first one will be fixed on the upper left corner (0, 0) and the second will be defined by the cursor position on screen.

We could draw a dynamic line between those 2 points like this:

GDScriptC#

  1. extends Node2D
  2. var point1 : Vector2 = Vector2(0, 0)
  3. var width : int = 10
  4. var color : Color = Color.GREEN
  5. var _point2 : Vector2
  6. func _process(_delta):
  7. var mouse_position = get_viewport().get_mouse_position()
  8. if mouse_position != _point2:
  9. _point2 = mouse_position
  10. queue_redraw()
  11. func _draw():
  12. draw_line(point1, _point2, color, width)
  1. using Godot;
  2. using System;
  3. public partial class MyNode2DLine : Node2D
  4. {
  5. public Vector2 Point1 { get; set; } = new Vector2(0f, 0f);
  6. public int Width { get; set; } = 10;
  7. public Color Color { get; set; } = Colors.Green;
  8. private Vector2 _point2;
  9. public override void _Process(double delta)
  10. {
  11. Vector2 mousePosition = GetViewport().GetMousePosition();
  12. if (mousePosition != _point2)
  13. {
  14. _point2 = mousePosition;
  15. QueueRedraw();
  16. }
  17. }
  18. public override void _Draw()
  19. {
  20. DrawLine(Point1, _point2, Color, Width);
  21. }
  22. }

In this example we obtain the position of the mouse in the default viewport every frame with the method get_mouse_position. If the position has changed since the last draw request (a small optimization to avoid redrawing on every frame)- we will schedule a redraw. Our _draw() method only has one line: requesting the drawing of a green line of width 10 pixels between the top left corner and that obtained position.

The width, color, and position of the starting point can be configured with with the corresponding properties.

It should look like this when run:

../../_images/draw_line_between_2_points.webp

Drawing an arc between 2 points

The above example works, but we may want to join those 2 points with a different shape or function, other than a straight line.

Let’s try now creating an arc (a portion of a circumference) between both points.

Exporting the line starting point, segments, width, color, and antialiasing will allow us to modify those properties very easily directly from the editor inspector panel:

GDScriptC#

  1. extends Node2D
  2. @export var point1 : Vector2 = Vector2(0, 0)
  3. @export_range(1, 1000) var segments : int = 100
  4. @export var width : int = 10
  5. @export var color : Color = Color.GREEN
  6. @export var antialiasing : bool = false
  7. var _point2 : Vector2
  1. using Godot;
  2. using System;
  3. public partial class MyNode2DLine : Node2D
  4. {
  5. [Export]
  6. public Vector2 Point1 { get; set; } = new Vector2(0f, 0f);
  7. [Export]
  8. public float Length { get; set; } = 350f;
  9. [Export(PropertyHint.Range, "1,1000,")]
  10. public int Segments { get; set; } = 100;
  11. [Export]
  12. public int Width { get; set; } = 10;
  13. [Export]
  14. public Color Color { get; set; } = Colors.Green;
  15. [Export]
  16. public bool AntiAliasing { get; set; } = false;
  17. private Vector2 _point2;
  18. }

../../_images/draw_dynamic_exported_properties.webp

To draw the arc, we can use the method draw_arc. There are many arcs that pass through 2 points, so we will chose for this example the semicircle that has its center in the middle point between the 2 initial points.

Calculating this arc will be more complex than in the case of the line:

GDScriptC#

  1. func _draw():
  2. # Calculate the arc parameters.
  3. var center : Vector2 = Vector2((_point2.x - point1.x) / 2,
  4. (_point2.y - point1.y) / 2)
  5. var radius : float = point1.distance_to(_point2) / 2
  6. var start_angle : float = (_point2 - point1).angle()
  7. var end_angle : float = (point1 - _point2).angle()
  8. if end_angle < 0: # end_angle is likely negative, normalize it.
  9. end_angle += TAU
  10. # Finally, draw the arc.
  11. draw_arc(center, radius, start_angle, end_angle, segments, color,
  12. width, antialiasing)
  1. public override void _Draw()
  2. {
  3. // Calculate the arc parameters.
  4. Vector2 center = new Vector2((_point2.X - Point1.X) / 2.0f,
  5. (_point2.Y - Point1.Y) / 2.0f);
  6. float radius = Point1.DistanceTo(_point2) / 2.0f;
  7. float startAngle = (_point2 - Point1).Angle();
  8. float endAngle = (Point1 - _point2).Angle();
  9. if (endAngle < 0.0f) // endAngle is likely negative, normalize it.
  10. {
  11. endAngle += Mathf.Tau;
  12. }
  13. // Finally, draw the arc.
  14. DrawArc(center, radius, startAngle, endAngle, Segments, Color,
  15. Width, AntiAliasing);
  16. }

The center of the semicircle will be the middle point between both points. The radius will be half the distance between both points. The start and end angles will be the angles of the vector from point1 to point2 and vice-versa. Note we had to normalize the end_angle in positive values because if end_angle is less than start_angle, the arc will be drawn counter-clockwise, which we don’t want in this case (the arc would be upside-down).

The result should be something like this, with the arc going down and between the points:

../../_images/draw_arc_between_2_points.webp

Feel free to play with the parameters in the inspector to obtain different results: change the color, the width, the antialiasing, and increase the number of segments to increase the curve smoothness, at the cost of extra performance.


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next