This site uses strictly necessary cookies. More Information

X- Home /

# Mathematicians wanted = trajectory target hitting question

I should really be able to figure this one out myself but it is driving me mad - my maths knowledge seems to be just too rusty.

I basically want to throw a ball from any height (position x,y,z in 3d space) onto a target point in 3D space. On top of this, I am checking that the ball with the calculated parabola does not hit a net with a certain height. I came up with the correct calculations to hit the target point based on this site: http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html

So I am defining a velocity and calculating the appropriate angle. But now I am checking whether it hits the net, and if it does want to change the angle and/or velocity so it goes higher.

But I am having a real problem changing the equation from the "where will it land" part so that I can adjust the height to go over the net height. How can I reverse the equation from x and time? I have the start x and y, the target x and y and need to determine the angle and velocity (I can always make up one of them, just need to get the velocity). The one I came up with is obviously wrong, I am missing the x and y part - I can only adjust the throwing velocity correctly if the start and end point are on the same y level.

Any help appreciated!

**Answer** by aldonaletto
·
Apr 20, 2012 at 03:54 PM

If both objects are at the same height, things are easier. If they have different heights, however, the equation becomes too complicated, so complicated that I was not able to get the inverse function. But I used a simple trick to correct the trajectory when the height difference is relatively small compared to the horizontal distance (how much is "small" depends on the elevation angle). The idea is to aim closer or farther than the target distance in such a way that the trajectory passes through its actual position:

This trick works fine for relatively small differences, as I said, but since it relies on a straight line approximation of the trajectory near to the zero height point, the error grows with the height difference.

You can see my answer with the complete ballistic calculation function in this question:
http://answers.unity3d.com/questions/148399/shooting-a-cannonball.html

**EDITED:** The equation you have is interesting because it takes the precise height difference into account, while mine just approximates it. Unfortunately, it returns the angle, not the velocity, and this complicate things a lot!

My function does the opposite - returns a velocity vector for a given angle - but isn't so precise when the heights are different. Maybe you could combine both and try a numeric solution: find an initial angle based on the fence height and distance from the target, use my function to find the necessary velocity, refine the angle with your equation (the second one - the first always returns the higher angle) and check if the fence is being hit: if yes, increase the initial angle (multiply it by 1.10, for instance) and repeat the process.

Just as a convenience, I posted the function BallisticVel below:

function BallisticVel(target: Transform, angle: float): Vector3 { var dir = target.position - transform.position; // get target direction var h = dir.y; // get height difference dir.y = 0; // retain only the horizontal direction var dist = dir.magnitude ; // get horizontal distance var a = angle * Mathf.Deg2Rad; // convert angle to radians dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle dist += h / Mathf.Tan(a); // correct for small height differences // calculate the velocity magnitude var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a)); // return the complete velocity vector return vel * dir.normalized; }

I already got so far and I can actually check whether it will clear the fence. $$anonymous$$y problem starts just there - I need to get a new angle and velocity in order to clear the net but still hit my target. Currently I am changing the velocity (vel--) and calculate my angle again, because I got the formula for the angle:

```
float angle1 = $$anonymous$$athf.Atan((vel*vel + $$anonymous$$athf.Sqrt($$anonymous$$athf.Pow(vel,4) -gravity*(gravity * xDist * xDist + 2*yDist*vel*vel)))/(gravity * xDist))*$$anonymous$$athf.Rad2Deg;
float angle2 = $$anonymous$$athf.Atan((vel*vel - $$anonymous$$athf.Sqrt($$anonymous$$athf.Pow(vel,4) -gravity*(gravity * xDist * xDist + 2*yDist*vel*vel)))/(gravity * xDist))*$$anonymous$$athf.Rad2Deg;
```

But this is not quite working, I'd need a way to actually get the velocity needed for a certain angle.

With each angle calculation I get two possible angles. I do not want anything over ca. 45 degrees because it just doesn't look right so I am kind of stuck with guessing...

I need to change the equation to something like that:

newVel(if hitting the net) = some formula with newAngle, xDistance, landing Y, minHeight at net point

$$anonymous$$aybe I just have to change my overall logic and ins$$anonymous$$d of applying an Impulse Force use some iTween Paths to hit the target...

I think you could try a numeric solution: I edited the answer to propose something like this - more a hack than a real numeric algorithm; a real numeric solution could be found from a 3 equation non-linear system, but it's way too complex for my taste.

for an excellent pictorial representation of the problem Aldo!

Thanks for the help, looks like it is just kind of a guessing game. I am just plaing around with a solution that looks like it is going to work taking your suggestions into account. If it all fails after some testing I guess I'll get rid of the net :) Cheers

**Answer** by fafase
·
Apr 20, 2012 at 03:37 PM

I think I found something that could help you out.

http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html#tra7

Go down the middle of the page to "Will it clear the fence?"

I thought that would be easier for you to read and understand than writing some (vel^2*sin^2(alpha))/2g = height

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Basic projectile trajectory with transform 1 Answer

Creating trajectory from a cannon 0 Answers

2D projectile trajectory prediction 2 Answers

3d Rigidbody Trajectory Prediction 1 Answer