Comments and answers for "Mathematicians wanted = trajectory target hitting question"
http://answers.unity.com/questions/242624/mathematicians-wanted-trajectory-target-hitting-qu.html
The latest comments and answers for the question "Mathematicians wanted = trajectory target hitting question"Comment by Cypras on Cypras's answer
http://answers.unity.com/comments/863324/view.html
@aldonaletto Is there a way to change this function so it's calculated on the z axis rather than the y axis? I've tried multiple things, but I can't seem to get it to work. http://answers.unity3d.com/questions/862037/convert-trajectory-script-to-2d.htmlSat, 27 Dec 2014 16:22:03 GMTCyprasComment by irgendeine on irgendeine's answer
http://answers.unity.com/comments/243629/view.html
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 :)
CheersMon, 23 Apr 2012 11:26:04 GMTirgendeineComment by Kleptomaniac on Kleptomaniac's answer
http://answers.unity.com/comments/242915/view.html
+1 for an excellent pictorial representation of the problem Aldo!Sat, 21 Apr 2012 14:27:48 GMTKleptomaniacComment by aldonaletto on aldonaletto's answer
http://answers.unity.com/comments/242893/view.html
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.Sat, 21 Apr 2012 13:13:25 GMTaldonalettoComment by irgendeine on irgendeine's answer
http://answers.unity.com/comments/242680/view.html
I already got so far and I can actually check whether it will clear the fence. My 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 = Mathf.Atan((vel*vel + Mathf.Sqrt(Mathf.Pow(vel,4) -gravity*(gravity * xDist * xDist + 2*yDist*vel*vel)))/(gravity * xDist))*Mathf.Rad2Deg;
float angle2 = Mathf.Atan((vel*vel - Mathf.Sqrt(Mathf.Pow(vel,4) -gravity*(gravity * xDist * xDist + 2*yDist*vel*vel)))/(gravity * xDist))*Mathf.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
Maybe 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...Fri, 20 Apr 2012 16:02:08 GMTirgendeineComment by fafase on fafase's answer
http://answers.unity.com/comments/242684/view.html
I guess I can't compete...Fri, 20 Apr 2012 15:57:35 GMTfafaseAnswer by aldonaletto
http://answers.unity.com/answers/242681/view.html
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:
![alt text][1]
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.<br>
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!<br>
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.<br>
Just as a convenience, I posted the function BallisticVel below:
<pre>
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;
}
</pre>
[1]: http://www.draldo.com.br/data/ballistictrajectory.pngFri, 20 Apr 2012 15:54:23 GMTaldonalettoAnswer by fafase
http://answers.unity.com/answers/242671/view.html
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 = heightFri, 20 Apr 2012 15:37:56 GMTfafase