Arrow shooting in 2d game

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

Hello, I’ve been trying to wrap my head around a proper implementation of arrow shooting. In order for you to better understand a situation, I’m going to present to you a setup I’ve conjured up for my game. Well, there’s two characters, namely an archer mob and a player. Apparently, archer mob plays a role of arrow shooter. Once player enters dangerous zone, archer gets ready to shoot some arrows. Pretty simple, isn’t it? The only problem I’ve encountered during my coding adventures was a physics related to projectile motion of an arrow. Here’s what I managed to construct so far

func _process(delta):
if launched:
  # Notice: angle chosen is -pi/4 (i find it well suited for initial conditions)
  xt = launchSpeed * cos(angle) * delta
  yt = (launchSpeed * sin(angle) * delta) - (1/2 * consts.g * delta * delta)
  position += Vector2(xt, yt)	
  angle = atan2(yt, xt)
  rotation += deg2rad(1)

Should I update an angle the way I did in the code? Is there a better way to do it?
Thanks in advance

I have a video that might help you. It’s from GameEndeavor, and in it he shows how he shoots physics-based cannonballs. You might be to tweak the code to be able to suit your needs:

Not sure if this is going to help, but hopefully it does.

TheJokingJack | 2021-04-12 20:15

Thanks! I’ll definitely watch it.

threadedstream | 2021-04-15 05:46

:bust_in_silhouette: Reply From: Kanor

I think it should. Towerfall would make a good reference for this sort of thing.

As for how, it’s hard to say without trying it out myself, but here’s what you’ve got should work. One thing to do for sure would be to set the pivot point of the arrow to the tip, so that the feather of the arrow can rotate and trail behind the sharp end.