Finding Horizontal/Vertical Distance Between Two Points in Scratch 3.0

1 reply · 3K views · last reply
Kizza23
Messages
1
Reaction score
0
I am making a Scratch 3.0 game. The shooter sprite is holding a gun slightly off-centre (see images), and I need the bullet to go to the end of the barrel of the gun before traveling forward (as so it would appear the bullet it leaving the gun). The issue is, to do this, I need to find the X and Y distance of the end of the barrel from the centre of the shooter sprite (Scratch doesn't let you use diagonal distances 😡). The X and Y distances change as the sprite rotates.

I know if the shooter sprite is facing 90º (right angle), the X distance to the gun is 105 pixels, and the Y distance is 45 pixels.

The computer always knows what direction the shooter sprite is facing.

What about the distances for all the other angles the shooter sprite is facing? Do I need a special formula?

View attachment 8942
View attachment 8943
 
Attachments
  • Diagram of a shooter sprite with red construction lines: an upward arrow from the sprite centre, a 90º right-angle marker, a triangle to the gun barrel end labelled 66.48º and 23.12º, and distances marked 105px horizontal and 45px vertical.
    Background (2).png
    12.2 KB · Views: 187
  • Diagram of a dark top-down shooter sprite with a red 45 degree reference line, a red angle marked 23 degrees 12 minutes, another marked 21 degrees 48 minutes, and distances labelled 106.07px horizontal and 42.43px vertical.
    Background (1).png
    11.8 KB · Views: 171
Looks like a right triangle is formed in each case you've shown ... couldn't the distance from your set origin to the muzzle (the hypotenuse) be found using Pythagoras?