• 2D
  • What is the coordinate system for a CanvasItem/Node2D

I've been trying to get my head around CanvasItems, as they may end up being what I need to use for my game. As a first try, I created a node that extends a Node2D and overrode _draw to draw a square. The examples I found showed hard-coded bounds. But I wanted to draw the "whatever" into the bounds as positioned in the main scene. So I looked to find how to get the bounds that had been specified in the parent scene.

I found nothing. It looks like there used to be such a function that no longer exists.

I discovered that the transform position seems to correspond to the center of the object. Similarly, resizing seems to affect the scale. But a transform has to operate on something. So I did some experiments with unity scale, etc to try to work out what was going on, and it seems that the bounds for Node2D maps (-32,-32) - (32,32) into the scene bounds. This seems like nice programmer-ish numbers. But it's empirical, and I may be misunderstanding.

I have looked all over and haven't yet found anything that actually documents this. I guess my question is: is it true that a Node2D coordinate system operates as I have empirically worked out, extending from (-32,-32) - (32,32) as far as the main scene bounds go?

Is there any documentation for this? I would love to read about this more in detail, if possible.

Also, do I simply have the wrong idea about what the rectangle sized and positioned in the scene for a sub-scene is supposed to signify (if it's not where the item will draw to)?

Thanks!

a month later

From the docs, a CanvasItem is:

<blockquote> Base class of anything 2D. Canvas items are laid out in a tree and children inherit and extend the transform of their parent. CanvasItem is extended by Control, for anything GUI related, and by Node2D for anything 2D engine related.

Any CanvasItem can draw. </blockquote>

So, you can draw on a CanvasItem or anything 'derived' from it.

A Node2D is

<blockquote> A 2D game object, with a position, rotation and scale. </blockquote>

So it basically represents a 2D point or position. You can attach (i.e. AddChild), e.g. a Sprite to it, so the Sprite then will be positioned at the x,y point of it's parent Node2D. The Sprite itself has a certain size, not the Node2D. The Node2D also has a scale value that will alter the size of it's child objects like the Sprite.

I'm not sure about your questions about bounds, etc. but it does get pretty complicated with position, scale, offsets, etc.

There's also local vs global position. Global position is basically relative to the root node of a scene, and local is from a node's parent.

Did you work out your questions in the end?