#center an object to a point

1 messages · Page 1 of 1 (latest)

brave stratus
#

hey so I have these tetris pieces I generate on the fly but one negative aspect is that it's not centered. How would I center it? (each indivisual square is it's own scene instantiated then parented to a seperate node called "origin") I thought mabye I could get the bounding box of the origin but i can't figure that out either

code: https://bpa.st/QF7A

floral hull
#

theres an offset property in the sprite perhaps that could help

brave stratus
#

prolly not but I'll see

tropic yew
#

it looks like you generate the pieces one square at a time

#

you'd have to get the bounding box of all the squares after they're done generating

brave stratus
#

I think I've solved my problem

#

in a diff way

brave stratus
#

For anyone who wants to do smth like ts

#

I have an array of the locations of all pieces so depending on that you might have to adjust

#

basically I iterate over the array

#

and then find the outermost x and y coordinates on both sides

#

do sum math to figure out the size variable of the rect2D

#

and then wait

#

what about the position var of the rect2D? well I'll figure it out

#

hopefully 🙏🏼

brave stratus
#

position is the top left value

#

so min of the y array

#

and min of the x array

#

and then offset it by half it's size on x and y axis

#

wait you don't have to do that

#

the offset part

manic kite
#

You could also just use Rect2 and its many functions. I think the Rect2.merge() function really fits your needs. Then you can just store the Rect2 as a property of the whole tetris piece. @brave stratus

brave stratus