0 votes

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.

in Engine by (14 points)

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

1 Answer

+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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.