Physics for arrow shooting

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

Hi! I’ve been trying to wrap my head around proper implementation of arrow shooting. So, I have an archer mob and player. In this particular setup, archer is supposed to shoot player once player enters archer’s watching zone. I want the arrow to fly according to laws of parabolic motion, accounting for a player position as well – so that it’s flying right toward a player.
Here’s what I’ve written so far

  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)

An initial angle chosen is -pi/4.
What should I do next? Thanks in advance.

Same question, same comment. Here’s the same video:

TheJokingJack | 2021-04-12 20:16

:bust_in_silhouette: Reply From: avnih

The speed components shouldn’t depend on the momentary angle, but only on the initial angle.

  dx = launchSpeed * cos(initial_angle) * delta
  dy = (launchSpeed * sin(initial_angle) - (consts.g * t)) * delta
  var d = Vector2(dx, dy)   
  position += d
  rotation = d.angle()
  t += delta

But since the dy component has an error factor that accumulates, I would advise to calculate position anew on every update:

  t += delta
  position = initial_position + Vector2(launchSpeed * cos(initial_angle) * t, launchSpeed * sin(initial_angle) * t - 0.5 * consts.g * t * t)

Also note that y’s sign is inverted, as “down” means increasing y.

Thank you very much! I’ll take it into account.

threadedstream | 2021-04-15 05:45