- Home /
How to make targeting code for a physics based projectile
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).
Seems like a standard physics question. Why don't you calculate the trajectory and then break that into x and y components?
I don't quite get your X1, Y1 things but the little I get, you know the distance and you are looking for the force to apply?
If that is the case then look there http://en.wikipedia.org/wiki/Projectile_motion, it has all you need.
I don't want to lock it to a single angle, I want it to calculate both an angle and a launch velocity, which is basically the same as calculating an X velocity and a Y velocity. Normal algebra solves for 1 unknown in an equation, but trying to solve for angle and launch velocity is solving for 2 unknowns. This has multiple correct solutions, for example: X + Y = 10 has the solutions, X = 5, Y =5, as well as X = 1, Y = 9, and X = 3, Y = 7.
I want the script to find one of these solutions, and I'm not sure how to get it to do that.
Right. The problem is there's infinite solutions to your equation which is exactly why multivariate solving techniques are not easy to implement...
Even with constraints on y speed and x speed, its still a bear to implement. $$anonymous$$aybe you should pick 5 different y velocities and solve for the corresponding x values, but trying to implement a multivar solver isn't going to be fun... ask yourself if it's really worth it and reevaluate what you're actually trying to accomplish
And then pick a random of the 5 y velocities? Or mayhaps the one closest to the X velocity? Hmmm.
Answer by Mendenbarr · Feb 28, 2014 at 04:44 PM
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);
}
Answer by Owen-Reynolds · Feb 26, 2014 at 04:39 PM
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.
If you're firing at max speed Z, you can break down the X and Y components of the velocity to Z*sinθ and Z*cosθ. θ is still an unknown, as it's what I'm trying to solve for, and so is the X and Y component of the velocity. $$anonymous$$y physics is a bit rusty, and I can't think of anyway to take a given target position and max speed and find out the angle of fire required to hit it.
Exactly -- theta is the single unknown. You aren't trying to compute the x velocity and the y velocity and the angle. All you need is to get the angle. It's tricky, but is a known problem with known solutions, given speed, distance and change in height.
Or, it you want less math, just beep guessing angles and bracket the solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
In what way can I calculate a predicted target destination based on force to be added? 1 Answer
Drawing a Mandelbrot Set problem 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Finding the axis of a curving pipe 0 Answers