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)
``````

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

in Engine

Same question, same comment. Here's the same video: https://www.youtube.com/watch?v=_kA1fbBH4ug

+1 vote

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.

by (149 points)
edited by

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