How to draw 2D things without depending on the node's transform?

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

I need to draw debug lines and circles in _draw() to verify if an enemy AI script is working.
However, I want those drawings to be independent from the node’s transform.

I tried to call debug_set_transform() but it doesn’t resets the current transform, instead it adds another one on top.

I tried to create another node to use as a global debug drawer but I get this error:

ERROR: Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.
   At: scene\2d\canvas_item.cpp:706
:bust_in_silhouette: Reply From: genete

From the enemy script send a signal that is caught by a node that is globally positioned. Then draw normally on that node.
Anyway, drawing over the enemy node (even with its own transformation) is not a bad idea because this way you know that the actual script of that enemy node is working and not other.

I would prefer not to rely on another node because it’s too awkward to setup for a debug system. Also, I tried to use a different node for drawing so instead I write something like:

func _draw():
	var debug_node = get_debug_node()
	debug_node.draw_line(...)

But as I said, it creates an error and doesn’t draws anything for some reason…

I prefer drawing directly from the concerned node, but it’s a spinning RigidBody and it’s awful to draw aligned things from it :s

Zylann | 2016-05-23 22:02

:bust_in_silhouette: Reply From: Ignacio Armenteros

You have a function on CanvasItem to set the base trasform of all the following draw commands:

void draw_set_transform ( Vector2 pos, float rot, Vector2 scale )

You can reset it to pos=Vector2(), rot=0, scale=Vector2(1,1) and will be drawing in global coordinates.

I tried this already, but it doesn’t works. It adds the transformation, not replace it.

Zylann | 2016-05-24 07:30

Then set the inverse trasform of your node with the get_global_transform().inverse() and using the pos,rot,scale of that matrix.

Ignacio Armenteros | 2016-05-24 07:48

:bust_in_silhouette: Reply From: Zylann

I created an issue in which we discussed on the ways of doing it for a custom particle system Cannot draw in global space from a child node · Issue #5428 · godotengine/godot · GitHub