1
\$\begingroup\$

i'm trying to throw an arrow in my game, but i'm having a hard time trying to realize how to make a good parabola.

What I need: The more you hold "enter" stronger the arrow goes. The arrow angle will be always the same, 45 degrees.

This is what I have already have:

    private float velocityHeld = 1f;
    protected override void Update(GameTime gameTime)
    {
 private void GetKeyboardEvent()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Enter) && !released)
            {
                timeHeld += velocityHeld;
                holding = true;
            }
            else
            {
                if (holding)
                {
                    released = true;
                    holding = false;
                    lastTimeHeld = timeHeld;
                }
            }
        }

        if (released && timeHeld > 0)
        {
        float alpha = MathHelper.ToRadians(45f);
        double vy = timeHeld * Math.Sin(alpha);
        double vx = timeHeld * Math.Cos(alpha);

        ShadowPosition.Y -= (int)vy;
        ShadowPosition.X += (int)vx;
        timeHeld -= velocityHeld;
        }
        else
        {
            released = false;

        }
       }

My question is, what do I need to do to make the arrow to go bottom as it loses velocity (timeHeld) to make a perfect parabola?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You're already calculating the launch velocity; after launch, the X velocity of the object remains constant over time, and the Y velocity has constant downward gravitational acceleration applied to it over time. Then you simply apply the velocity as usual in a physics engine.

For more information, if you search for "parabolic trajectory", "ballistic trajectory" or "projectile trajectory" you'll find all kinds of articles and demos on the subject. For example, see Projectile motion on Wikipedia.

The standard treatment ignores air resistance, but you can incorporate that as well if you like; it only requires adding a drag force proportional to the negative of the object's current velocity. You can find this discussed many times on the Web as well.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .