How can I stretch a sprite to simulate an object aiming at the cursor position?

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

Hey everyone! Beginner to gamedev in general over here.
I chose a shooting gallery asset pack at random and decided to make a tiny game with it.
I want to stretch, squash, and pivot the rifle to the position of a position2D node that is further down the hierarchy (which follows the mouse whenever a MouseMoveEvent is triggered), does anyone have any useful / relevant links that I might use?

Is the rifle a sprite? Do you intend to rotate the sprite around a fixed point? If so, you can use the look_at() function (or some similar code) to move the sprite around. To squash and stretch the sprite, change the “size” property of the sprite’s image. Change the size with respect to how far/close the mouse is from the rifle. Use some ratio (such as the position of the mouse and the rifle image) to scale the sprite with the mouse.

Ertain | 2020-09-09 03:44

Thanks! I did end up changing the scale of the sprite in relation to the mouse position.

Lazarwolfe | 2020-09-09 18:55

:bust_in_silhouette: Reply From: beaverusiv

If you’re talking about a sprite like this one:

What I would do at a high level is to figure out the vector the gun points along and then use that with the vector you want it to be pointing to to figure out what transforms you need to apply to the sprite. It will take some tweaking and figuring out which transforms look best (stretching height vs stretching width, etc)

If it’s a simple top-down sprite then look_at() is something you should look up in the docs.

Thanks! You hit the nail on the head, I ended up subtracting the mouse position from the InputEventMouseMotion from the Sprite’s origin position, adding it to the horizontal scale of the Sprite --with some stops so that it doesn’t stretch too far either way, of course–.
For vertical motion I think I’m gonna go with a slight rotation in the general vertical position of the event, but I still got to implement it.

Lazarwolfe | 2020-09-09 18:28