How to view a Node2D's height and width

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey

I see the member variables have a global_scale, but I want to know where the left/right/top/bottom of my Node is. Can we use scale to find width/height?

Node2D’s don’t have a height or a width. They’re just points in 2D space.

literalcitrus | 2018-02-01 04:33

Yeah, bad question. I meant the Sprite child’s size

jarlowrey | 2018-02-08 00:14

:bust_in_silhouette: Reply From: jarlowrey
get_node("Sprite").texture.get_size()
:bust_in_silhouette: Reply From: arthurZ

I read in the kidsCanCode videos comments a way to find the extents of a collisionShape2d too, that I think sometimes is better than find the size of a sprite and divide by two.

get_node("CollisionShape2d").get_shape().get_extents()

Just keep in mind that starting with Godot 3.0 you cannot use get_shape() function. You have to use shape_owner_get_shape(int owner_id, int shape_id).

Paar | 2018-02-08 17:45

Came to this question from search. The answers did not help, but eventually I figured myself how to do it in Godot 3 for Area2D.

One need to access the shape of the collison object. Each shape type has different set of parameters, the bounding box can be calculated differently from each.

For example, the simplest is RectangleShape2D:

$MyCollisionShape2D.shape.extents

This gives you the extent of the rectangle. The width and height of this shape is twice the half extents.

Other shapes will require more complex calculations.

Artium Nihamkin | 2018-09-29 20:33