# Question Calculate launch velocity of projectile to reach a target incorporating wind

Discussion in 'Physics' started by peadar-buckeejit, Feb 27, 2024.

Joined:
Aug 15, 2022
Posts:
3
I have the below code, from a plugin, which returns a velocity to apply to a ball given the parameters. This works to ensure the ball will reach the specified target. However, I want to modify this to pass in a `Vector2 windAcceleration` so the calculation should take this into account. This `windAcceleration` gets applied in the `FixedUpdate()` as
``rigidbody.AddForce(windAcceleration, ForceMode.Acceleration);``

Is there an easy way to do this? I have tried numerous different things with no satisfying outcome.

Code (CSharp):
1. public static Vector3 VelocityByAngle(Vector3 start, Vector3 end, float elevationAngle)
2. {
3.     var b = Mathf.Tan(Mathf.Deg2Rad * elevationAngle);
4.
5.     var vec = end - start;
6.     var n = vec.y;
7.     vec.y = 0;
8.     var m = vec.magnitude;
9.
10.     var a = (n / m - b) / m;
11.     var vx = Mathf.Sqrt(Physics.gravity.y / (2 * a));
12.     var vy = b * vx;
13.     var direction = vec / m;
14.     return new Vector3(direction.x * vx, vy, direction.z * vx);
15. }

Joined:
Aug 15, 2022
Posts:
3
My latest attempt is below, I can see that it creates an offset initial velocity but the acceleration force still takes it far away from the intended target.

Code (CSharp):
1. public static Vector3 VelocityByAngleWithWind(Vector3 start, Vector3 end, float elevationAngle,
2.             Vector3 windAcceleration)
3.         {
4.             // Assume windAcceleration has been converted to a Vector3 where necessary, affecting horizontal movement.
5.
6.             // Initial calculations without wind
7.             var b = Mathf.Tan(Mathf.Deg2Rad * elevationAngle);
8.             var vec = end - start;
9.             var n = vec.y;
10.             vec.y = 0;
11.             var m = vec.magnitude;
12.
14.             // Calculate time to reach the target without wind in the horizontal plane
15.             var a = (n / m - b) / m;
16.             var vxWithoutWind = Mathf.Sqrt(Physics.gravity.y / (2 * a));
17.             var vy = b * vxWithoutWind;
18.             var flightTimeWithoutWind = 2 * vy / Mathf.Abs(Physics.gravity.y);
19.
20.             // Estimate horizontal displacement due to wind
21.             var windEffect = windAcceleration * flightTimeWithoutWind * flightTimeWithoutWind / 2;
22.
23.             // Adjust the initial horizontal distance to include wind effect
25.                 m + windEffect.magnitude * Mathf.Sign(Vector3.Dot(windEffect, vec.normalized));
26.
27.             // Recalculate initial velocity with adjusted distance
29.             a = (n / m - b) / m;
30.             var vxWithWind = Mathf.Sqrt(Physics.gravity.y / (2 * a));
31.
32.             // Ensure the direction accounts for wind
33.             var direction = (end - start + windEffect).normalized;
34.
35.             // Calculate the new initial velocity, incorporating wind adjustment
36.             return new Vector3(direction.x * vxWithWind, vy, direction.z * vxWithWind);
37.         }

Last edited: Feb 28, 2024
3. ### zulo3d

Joined:
Feb 18, 2023
Posts:
1,056
Code (CSharp):
1.     // Returns the force needed to jump to a goal while countering any wind
2.     Vector3 JumpForce(Vector3 start, Vector3 goal, Vector3 currentVelocity, Vector3 wind)
3.     {
4.         float h=Vector3.Distance(start, goal)*0.05f; // Get the jump height. greater the distance the higher we need to jump
5.         h+=Mathf.Abs(Vector3.Dot((goal-start).normalized,Vector3.up)*(13f/Physics.gravity.y)); // a little more height is needed when jumping onto things up above.
6.         Vector3 force=(goal-start)/h-currentVelocity;
7.         return force-(wind+Physics.gravity)*h*0.5f; // Intended for ForceMode.VelocityChange with zero drag
8.     }
9.