From script is there a way to set a Sprite's pivot point (offset) without it moving position?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By avencherus
:warning: Old Version Published before Godot 3 was released.

Been playing around with this, and it seems like there isn’t any built-in way to set the pivot point location relative to the Sprite. It’s new position also becomes the pivot point, and the texture will jump.

It’s been a bit of an issue when Sprites are rotated or in a tree of Sprites that also have rotations.

Could you provide an example?
I have an vague idea of what you are having a problem with, but the room for misunderstanding is too great at the moment.

Tybobobo | 2016-11-08 18:17

If you have a centered Sprite at location (0,0) it will sit centered in the scene’s origin. If in script, in my case using tool mode, if you use either of the following:

var s = get_node("my_sprite")
s.set_offset(Vector2(0,32))

or using the edit_set_pivot() from the node methods, you will see that the pivot/offset is at the origin now, however the sprite image will have moved down 32 pixels.

It’s not that it’s moving down exactly, it’s just that when you use the V key to set pivots, it will adjust a new relative position of the sprite so it preserves where your Sprite visually appeared.

avencherus | 2016-11-08 18:23

Okay, I understand.

I have experienced that using the “tool” property on the top of some scripts can screw things up inside it. Have you tried to run it without tool?

An alternative to using set_offset, is to create another node (could name it pivot_node), and put the my_sprite node inside it. That way, the sprite will be relative to the pivot_node and you can just set the normal position of the sprite to be 0,32 away from it.

Would that work for you?

Tybobobo | 2016-11-08 18:27

The tool is working fine. The offset is even working as designed.

I’m going to use these for bones.

I may try that out if it becomes too difficult to calculate a more direct solution by calculating normals based on the rotations. I’ll have to see if that even works.

If not, yeah I guess it’s probably not too terrible to fill some Position2Ds in between the Sprites.

avencherus | 2016-11-08 18:34

:bust_in_silhouette: Reply From: bWWd

Dude, You are aware that you can change position, scale, even spritesheet in animation player , right ? So you can adjust offset in animation player by moving sprite and keying it .
I have offset different per animation, so i key my animationsheets and position on first frame of every animation, cause i swap spritesheets when changing anims, its really great