0
$\begingroup$

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 travelling 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?

IMAGE - Player facing 90º with all known dimensions HERE

IMAGE - Player facing 45º with all known dimensions HERE

$\endgroup$
0
$\begingroup$

Sounds like the length of the gun is $$ L = \sqrt{105^2+45^2} = 15 \sqrt{58} \approx 114.2366 \text{ pixels} $$ and now if you are pointing to an angle $a$, your distances are $$ X = L\cos a \quad \text{and} \quad Y = L\sin a. $$

$\endgroup$
  • $\begingroup$ I got the first bit, though I'm still trying to wrap my head around the second part. Does that mean if he is facing 45º, the offset is approximately X:81px, Y:81px? (This link may help: scratch.mit.edu/projects/304320659) $\endgroup$ – Kieran Ryan Apr 24 at 4:32
  • $\begingroup$ I've added my calculations for 45º facing in the original post above. $\endgroup$ – Kieran Ryan Apr 24 at 4:39
  • $\begingroup$ Okay, I've tried your formula (I understand how it works now), though it cannot calculate X or Y at 90º. I.e: 114.2366 x cos(90) = 0 (this obviously isn't the case) $\endgroup$ – Kieran Ryan Apr 24 at 5:00
  • $\begingroup$ @KieranRyan at $90^\circ$ there should be no horizontal distance, just pure vertical, no? $\endgroup$ – gt6989b Apr 24 at 17:35
  • $\begingroup$ Looking at the first image, you can see a horizontal offset of 105 pixels, as the character is holding the gun out from his centre. $\endgroup$ – Kieran Ryan Apr 24 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.