What's the use of Rect2.end

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

I want to know how far away an object is from the borders of the area. I thought I could use Rect2.position and Rect2.end and comparing the highest values for the bottom and right borders and the lowest numbers for the top and the left borders.

Rect2.end adds the size of the area to the position but it doesnt multiply the size for the units amount so I don’t undestand what it’s use is for. if I add size*8 (the unit amount) to the position then I get the proper opposite corner as I espected.

What is Rect2.end used for?
And is there an easier way to check the distance of an object compared to a border?
I’m using a tilemap and want to know how many tiles in one direction are left to hit the border.

:bust_in_silhouette: Reply From: estebanmolca

If the rectangle starts at position (0,0) of the parent, and we assign 100 width and 100 height, end will mark the end of the rectangle with respect to the parent position, in this case it will be (100,100). If we now move the rectangle to position (10,10), the rectangle will still have 100 width and 100 height but now end will mark (110,110). Note that the size can go negative, that is, the rectangle can be expanded to the right (width +), left (width-), down (height +), or up (height-).You can draw a rectangle in _draw () to see how they behave.

but it doesnt multiply the size for the units amount if I add size * 8

I do not understand this. Where does the 8 come from? Is the size of your tiles? If you know the position of the object and the edge you can use the Vector2.DistanceTo function to find out the distance in pixels and then divide that distance by the width of the tile.

So it does work as I suspected but as you explained you have to use pixels for the size instead of units.
I was watching a video and the author used the ruler tool, explained that the units are tied to your grid size and then he used the amount of units he got from the ruler tool for the size of the rect. I’ll have to watch it again, but using pixels works just fine thanks.

IHate | 2021-03-08 15:15