2

I'm trying to get a character to throw something in an arc at a target.

I know the vertex(x,y) and the target(x,y) and I want to get an arc from the origin(x,y) to the target with a max height of vertex.y

What I have is based off the vertex form of y = a(x-h)^2 + k

public static Vector3 parabola(Vector2 origin, Vector2 target, float height)
{
    float dist = target.x - origin.x;
    Vector2 vertex = new Vector2(origin.x + (dist / 2), origin.y + height);

    //a = (y-k) / (x-h)^2
    float a = (target.y - vertex.y) / ((target.x - vertex.x) * (target.x - vertex.x));

    //b = (-h + -h) * a
    float b = (-vertex.x + -vertex.x) * a;

    //c = (h * h) * a + k
    float c = (vertex.x * vertex.x) * a + vertex.y;

    return new Vector3(a, b, c);        
}

    x += Time.DeltaTime;
    float yPos = a * ((x - h) * (x - h)) + k;

This doesn't produce the correct arc. It's usually much too steep or much too shallow. Is my algebra wrong, or am I using the wrong approach?

Thanks

user3818733
  • 17
  • 1
  • 5
  • What do you mean by "I know the vertex(x,y)"? And "max height of vertex.y"? What are you actually wanting to work out? The variables you should be interested are starting position, target position, initial velocity, angle of trajectory and gravity value **IF** you want to send a projectile from one point to another. You will know the starting/end position and the gravity value, you can adjust the initial velocity then you just need to work out the angle. This is assuming a 2D nature to the game. Also, how does a `Vector3` describe a parabola? If you clarify these issues, I'll try to answrr. – anothershrubery Jul 10 '14 at 09:26
  • That is of course not taking into account any resistance. (From air, etc) – anothershrubery Jul 10 '14 at 09:28

1 Answers1

0

Here is a good solution: Wiki:Trajectory of a projectile.

enter image description here