1

I need to calculate the angles to through the ball in that direction for a given speed and the point where it should land after thrown.

The horizontal angle is easy(We know both start and step points).How to calculate the vertical angle of projection.There is gravy applying on object.

Time of travel will be usual as bowling time(time between ball release and occurring step) as per video.

Is there a way directly in unity3d?

Watch this video for 8 seconds for clear understating of this question.

djkpA
  • 1,224
  • 2
  • 27
  • 57

1 Answers1

1

According to the Wikipedia page Trajectory of a projectile, the "Angle of reach" (The angle you want to know) is calculated as follows:

θ = 1/2 * arcsin(gd/v²)

In this formula, g is the gravitational constant 9.81, d is the distance you want the projectile to travel, and v is the velocity at which the object is thrown.

Code to calculate this could look something like this:

float ThrowAngle(Vector3 destination, float velocity)
{
    const float g = 9.81f;
    float distance = Vector3.Distance(transform.position, destination);
    //assuming you want degrees, otherwise just drop the Rad2Deg.
    return Mathf.Rad2Deg * (0.5f * Asin((g*distance)/Mathf.Pow(velocity, 2f)));
}

This will give you the angle assuming no air resistance etc. exist in your game. If your destination and your "throwing point" are not at the same height, you may want to set both to y=0 first, otherwise, errors may occur.

EDIT:

Considering that your launch point is higher up than the destination, this formula from the same page should work:

θ = arctan(v² (+/-) √(v^4-g(gx² + 2yv²))/gx)

Here, x is the range, or distance, and y is the altitude (relative to the launch point).

Code:

float ThrowAngle(Vector3 start, Vector3 destination, float v)
    {
        const float g = 9.81f;
        float xzd = Mathf.Sqrt(Mathf.Pow(destination.x - start.x, 2) + Mathf.Pow(destination.z - start.z, 2));
        float yd = destination.y - start.y;
        //assuming you want degrees, otherwise just drop the Rad2Deg. Split into two lines for better readability.
        float sqrt = (Mathf.Pow(v,4) - g * (g*Mathf.Pow(xzd,2) + 2*yd*Mathf.Pow(v,2))/g*xzd);
        //you could also implement a solution which uses both values in some way, but I left that out for simplicity.
        return Mathf.Atan(Mathf.Pow(v, 2) + sqrt);
    }
Light Drake
  • 121
  • 3
  • 9
  • No both can't be at same height . Projection starts at some height and destination point is on ground as shown in video same as bowling(ball movement). – djkpA Sep 18 '16 at 16:43
  • That article has a part for a throw that doesn't start at y = 0 and a landing point with y = 0. (You would treat the lower point as y = 0, I guess, if actually none of them is at y = 0.) Just look at the graphics on the right to find the correct formula. – Gunnar B. Sep 18 '16 at 16:51
  • Currently writing the answer. Please give me a moment. – Light Drake Sep 18 '16 at 16:53