What is the Godot way to define (and refer) custom rectangle areas?

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

I’m coming from Phaser + Tiled world, where, if I need some rectangle area in game world (like Player’s area, spawning area, and so on) I can just draw rectangle in Tiled and then get it coordinates from Phaser.js and use as I need. And I seem to stuck to do similar things in Godot.

For some of tasks I can use Area2D with rectangle inside and collision events. But it is not always enough.

How can I just define rectangle on screen, and get its coordinates? For Sprite object and for Node2D I cannot get bounding rectangle. I can use Area2D + Rectangle and refer to rectangle’s ‘extent’ property to get width/height, but that seems to be overhead for me - Area2D is used in collision detection.

What can I do in general? And what could be done in Godot for following scenarios?

  1. Camera limits. I have Sprite with background gradient which I scale to needed world size, and I’d like to set camera limits on that Sprite’s width/height.
  2. Hero movement limits. Half of world is not accessible for player, so any move to x > MIDDLE shall be denied. I can just setup constant MIDDLE in the code, but I’d like to draw allowed area as rectangle and refer to it coordinates.
  3. Spawn area. Mark some place of world (that could be just point, not rectangle) where new objects shall be created by code.

When you write “I can use Area2D + Rectangle and refer to rectangle’s ‘extent’ property to get width/height, but that seems to be overhead for me”, are you referring to a Rect2?

Ertain | 2019-01-03 19:26

Will polygon2d work for your issue?

sxkod | 2019-01-09 02:21