Skip to content

Commit 8d4c972

Browse files
authored
Merge pull request godotengine#6455 from dalexeev/draw-line-coords
Explain `draw_line` and `draw_rect` coordinates
2 parents d97d2c2 + 519b8ac commit 8d4c972

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

tutorials/2d/custom_drawing_in_2d.rst

+66-1
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,74 @@ call ``queue_redraw()`` from the ``_process()`` callback, like this:
139139
}
140140
}
141141

142+
Coordinates
143+
-----------
144+
145+
The drawing API uses the CanvasItem's coordinate system, not necessarily pixel
146+
coordinates. Which means it uses the coordinate space created after applying
147+
the CanvasItem's transform. Additionally, you can apply a custom transform on
148+
top of it by using
149+
:ref:`draw_set_transform<class_CanvasItem_method_draw_set_transform>` or
150+
:ref:`draw_set_transform_matrix<class_CanvasItem_method_draw_set_transform_matrix>`.
151+
152+
When using ``draw_line``, you should consider the width of the line.
153+
When using a width that is an odd size, the position should be shifted
154+
by ``0.5`` to keep the line centered as shown below.
155+
156+
.. image:: img/draw_line.png
157+
158+
.. tabs::
159+
.. code-tab:: gdscript GDScript
160+
161+
extends Node2D
162+
163+
func _draw():
164+
draw_line(Vector2(1.5, 1), Vector2(1.5, 4), Color.GREEN, 1.0)
165+
draw_line(Vector2(4, 1), Vector2(4, 4), Color.GREEN, 2.0)
166+
draw_line(Vector2(7.5, 1), Vector2(7.5, 4), Color.GREEN, 3.0)
167+
168+
.. code-tab:: csharp
169+
170+
public class CustomNode2D : Node2D
171+
{
172+
public override void _Draw()
173+
{
174+
DrawLine(new Vector2(1.5, 1), new Vector2(1.5, 4), Colors.Green, 1.0)
175+
DrawLine(new Vector2(4, 1), new Vector2(4, 4), Colors.Green, 2.0)
176+
DrawLine(new Vector2(7.5, 1), new Vector2(7.5, 4), Colors.Green, 3.0)
177+
}
178+
}
179+
180+
The same applies to the ``draw_rect`` method with ``filled = false``.
181+
182+
.. image:: img/draw_rect.png
183+
184+
.. tabs::
185+
.. code-tab:: gdscript GDScript
186+
187+
extends Node2D
188+
189+
func _draw():
190+
draw_rect(Rect2(1, 1, 3, 3), Color.GREEN)
191+
draw_rect(Rect2(5.5, 1.5, 2, 2), Color.GREEN, false, 1.0)
192+
draw_rect(Rect2(9, 1, 5, 5), Color.GREEN)
193+
draw_rect(Rect2(16, 2, 3, 3), Color.GREEN, false, 2.0)
194+
195+
.. code-tab:: csharp
196+
197+
public class CustomNode2D : Node2D
198+
{
199+
public override void _Draw()
200+
{
201+
DrawRect(new Rect2(1, 1, 3, 3), Colors.Green)
202+
DrawRect(new Rect2(5.5, 1.5, 2, 2), Colors.Green, false, 1.0)
203+
DrawRect(new Rect2(9, 1, 5, 5), Colors.Green)
204+
DrawRect(new Rect2(16, 2, 3, 3), Colors.Green, false, 2.0)
205+
}
206+
}
142207

143208
An example: drawing circular arcs
144-
----------------------------------
209+
---------------------------------
145210

146211
We will now use the custom drawing functionality of the Godot Engine to draw
147212
something that Godot doesn't provide functions for. As an example, Godot provides

tutorials/2d/img/draw_line.png

974 Bytes
Loading

tutorials/2d/img/draw_rect.png

1.56 KB
Loading

0 commit comments

Comments
 (0)