I was trying to make a catapult take the inputs of an initial speed, and the position of a target, and figuring out what angle it needs to shoot at to hit the target. Below is the answer I found.
Leaving the original question in for legacy:
I’m trying to write a script to calculate the X and Y velocity required to hit a target with a catapult, but since it’s a multivariable equation, c# is having none of it. Is there any way to do a min( X1/Y1 = Z and X2/Y2 = Z), where X1 and X2 are the only known variables?
I’ve pulled the distance between the launcher and the target using transform and finding the difference, which are X1 and X2. distance/speed = time, and I want to try and find the Y1 and Y2 speeds that cause the time to be as low as possible. I realize that a pure min solution will return an insanely high value for X to try to get there instantly, so I’m also looking for a way to work in an upper and lower bound for the speed (Y1 and Y2).
Think you’ve confused yourself thinking about it. If you want to get there as fast as possible, the catapult should always fire at maximum speed. So speed is a constant. There are two angles to hit any target in range – a lofting one above 45, and a flat one below 45. Wikipedia has this. The flat one is the fastest (proof: as the angle decreases, x velocity increases.)
The only time you might need variable starting speed is if you need to loft just barely over obstacles. Normally, if the low trajectory would hit a wall, you simply switch to the high one. But, say you have a roof. Then I think you could pick a point just above the obstacle, and use the three points (start, obstacle+y, end) to solve for the parabola.
Also, it’s a catapult. AFAIK, these things were aimed by dead reckoning and trial&error.
So I found what I was looking for, and here is the code I’m using that works. It grabs the positions of itself, and it’s target, and takes a public variable “maxLaunch”, which is the launch velocity, and determines the angle, and through the angle the X and Y velocities required to hit the target.
void Target (){
xTarget = target.transform.position.x;
yTarget = target.transform.position.y;
xCurrent = transform.position.x;
yCurrent = transform.position.y;
xDistance = Math.Abs(xTarget - xCurrent);
yDistance = yTarget - yCurrent;
fireAngle = 1.57075f - (float)(Math.Atan((Math.Pow(maxLaunch, 2f)+ Math.Sqrt(Math.Pow(maxLaunch, 4f) - 9.8f * (9.8f * Math.Pow(xDistance, 2f) + 2f * yDistance * Math.Pow(maxLaunch, 2f) )))/(9.8f * xDistance)));
xSpeed = (float)Math.Sin(fireAngle) * maxLaunch;
ySpeed = (float)Math.Cos(fireAngle) * maxLaunch;
if ((xTarget - xCurrent) < 0f){
xSpeed = - xSpeed;
}
launchSpeed.x = (xSpeed);
launchSpeed.y = (ySpeed);
}