How to rotate a node from its center point

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

I am making a card game, and I would like to make the ability to make cards rotate 90 or 180 degrees. My card scenes are use an Area2D for a root node. Using the “rotate_degrees” property rotates the card, but does so using the cards top-left corner as a pivot which looks unnatural for a card game.

Is there a way to rotate my card scene using its center as a pivot point, or will I have to mess with chainging its position as it’s rotating to achieve approximately the same effect?

:bust_in_silhouette: Reply From: jgodfrey

I assume the visible part of the card is a Sprite? By default, the children of your top-level Area2D (probably a Sprite and maybe a CollisionShape2D) will be centered on the parent Area2D. If they’re not, you can certainly move the child nodes so they are centered.

With that done, rotating the Area2D node should rotate all of the children. And, if they’re centered on the Area2D node, it should look like you want/need.

Actually, the children are Control-class nodes. But I see your point; if I have the position of the Control node child at -size/2, its center would be at the Area2D’s Vector2(0,0), and therefore would appear to rotate from the center.

For now I went with this approach which seems more straightforward, but if I run into any bugs, I’ll try your way instead. Thanks

db0 | 2020-11-12 01:17

:bust_in_silhouette: Reply From: db0

As a pretty-good working workaround, I have used an child Control node with a modified rect_pivot_offset and just rotate only the internal control node.

However the card’s CollisionShape2D doesn’t rotate with it, which has the potential to create some wonkyness when trying to detect collisions, but it should work for 99% of the interactions.

Still, if someone has a solution on how to rotate from the center, using the Area2D node (and therefore rotate also the CollisionShare2D area, I’d like to learn it :slight_smile: