Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Angle calculations
- BASIC4U
-
Scratcher
54 posts
Angle calculations
I need a math formula. I have a missle flying overhead, and I want to shoot another missle so that it will intercept the first one perfectly. The first missle, missle A is going at 2 steps per second. The second one, missle B is going at 3 steps per second. If missle B is going to launch in 3 seconds, how do I know where to aim missle B so it will hit missle A? I would like a block where I can put in the speed of missle A, the speed of missle B, and the time until launch of the missle. Thanks!
- asivi
-
Scratcher
1000+ posts
Angle calculations
I need a math formula. I have a missle flying overhead, and I want to shoot another missle so that it will intercept the first one perfectly. The first missle, missle A is going at 2 steps per second. The second one, missle B is going at 3 steps per second. If missle B is going to launch in 3 seconds, how do I know where to aim missle B so it will hit missle A? I would like a block where I can put in the speed of missle A, the speed of missle B, and the time until launch of the missle. Thanks!Sorry if i can't give you the block you want, i remember when teenager doing pre-calculus like yours but it was too much years ago…
Your block would be including direction of the target and initial positions of both missiles.
- gtoal
-
Scratcher
1000+ posts
Angle calculations
I need a math formula. I have a missle flying overhead, and I want to shoot another missle so that it will intercept the first one perfectly. The first missle, missle A is going at 2 steps per second. The second one, missle B is going at 3 steps per second. If missle B is going to launch in 3 seconds, how do I know where to aim missle B so it will hit missle A? I would like a block where I can put in the speed of missle A, the speed of missle B, and the time until launch of the missle. Thanks!
Do you know how to solve quadratics? If not this may be more advanced maths than your current class level.
Google for target prediction formula.
offhand I think given x=0 it might simplify down to arcsin(2/3)*y or something like that but don't quote me - get the formula and work it out.
Graham
- footsocktoe
-
Scratcher
1000+ posts
Angle calculations
I need a math formula. I have a missle flying overhead, and I want to shoot another missle so that it will intercept the first one perfectly. The first missle, missle A is going at 2 steps per second. The second one, missle B is going at 3 steps per second. If missle B is going to launch in 3 seconds, how do I know where to aim missle B so it will hit missle A? I would like a block where I can put in the speed of missle A, the speed of missle B, and the time until launch of the missle. Thanks!
If missile A is standing still, you would launch missile B directly at it. You know how far apart A and B are and you know the speed of B so you know how long it will be before they collide.
If missile A is moving to the right, then the time it will take missile B to reach where it was standing still is how much time it has to move right. If you know how fast missile A is moving then you know where it will be at that time. That will describe a triangle and give you the angle at which to fire missile B.
BUT, since the distance B has to travel is now longer, it will arrive there too late, so you do a second calculation to determine how long missile B will take.
BUT that will give missile A that much more time to move and it will be a little further to the right, so you calculate it's new position. From that you can get the new angle to fire B.
BUT, since the distance B has to travel is now a little bit longer, it will arrive there a little bit late, so you do a new calculation to determine how long missile B will take.
Each time the error is getting smaller and smaller until eventually it's so small it doesn't matter. It's essentially zero.
Of course, if you had to do the calculations yourself, it would be very tedious, but the computer can do them in an instant.

- asivi
-
Scratcher
1000+ posts
Angle calculations
What about a missile guided by laser or infrared sensors?
It would do the job easier.
It would do the job easier.
- asivi
-
Scratcher
1000+ posts
Angle calculations
I need a math formula. I have a missle flying overhead, and I want to shoot another missle so that it will intercept the first one perfectly. The first missle, missle A is going at 2 steps per second. The second one, missle B is going at 3 steps per second. If missle B is going to launch in 3 seconds, how do I know where to aim missle B so it will hit missle A? I would like a block where I can put in the speed of missle A, the speed of missle B, and the time until launch of the missle. Thanks!Hi, did you succeed? I am very interested.
Greets.
- SBissay
-
Scratcher
500+ posts
Angle calculations
So, you have a missile A, with a known position (xA, yA) (I'll assume planar case) and speed v (vxA, vyA), and missile B with a know position (xB, yB) and norm of speed |v'| (that is you only know how fast it moves, not in which direction), plus a time of launch T0 for missile B.
What you want to find first, is the time when both missile will have the same position.
Position of missile A is (x + a * t, y + b * t).
Position of missile B is (x' + a' * (t - T0), y' + b' * (t - T0)).
Where t is the time.
So now you have a system of equations :
xA + vxA * t = xB + vxB * (t - T0) <=> vxB = (xA - xB + vxA * t) / (t - T0)
yA + vyA * t = yB + vyB * (t - T0) <=> vyB = (yA - yB + vyA * t) / (t - T0)
where vxB and vyB will be the components of v' (that is the horizontal and vertical speed of B).
Also, we know that |v'| = sqrt (vxB² + vyB²), or again v'² = vxB² + vyB².
So we have 3 equations with 3 unknowns, which is great.
BUT one of these equations isn't linear, which is… less great, to say the least.
so let's replace them in the last one :
v'² = ((xA - xB + vxA * t) / (t - T0))² + ((yA - yB + vyA * t) / (t - T0))²
or (easier on the eye) : v'² * (t - T0)² = (xA - xB + vxA * t)² + (yA - yB + vyA * t)²
Developping it, we get a quadratic equation : v'² * t² - 2 * v'² * T0 * t + v'² * T0² = xA ² + xB² + vxA² * t² - 2 * xA * xB + 2 * xA * vxA * t - 2 * xB * vxA * t + yA ² + yB ² + vyA² * t² - 2 * yA * yB + 2 * yA * vyA * t - 2 * yB * vyA * t (argh).
We solve it using discriminant, and we get t (we choose the positive one).
Once we have t, it is easy to know the coordinates of the point of impact O :
xO = xA + t * vxA
yO = yA + t * vyA
And now, you know that your missile B will go from point of launch (xB, yB) to impact O (xO, yO), so the angle to give is :
Angle = atan2(yO - yB, xO - xB)
where atan2(y, x) = arctan(y / x) if x > 0
arctan(y / x) +180 if x < 0 and y >= 0
arctan(y / x) - 180 if x < 0 and y < 0
90 if x = 0 and y > 0
-90 if x = 0 and y < 0
whatever you want if x = 0 and y = 0 (in our case that would mean that missile A is going to meet B where we launch B ^^)
Voilà !
What you want to find first, is the time when both missile will have the same position.
Position of missile A is (x + a * t, y + b * t).
Position of missile B is (x' + a' * (t - T0), y' + b' * (t - T0)).
Where t is the time.
So now you have a system of equations :
xA + vxA * t = xB + vxB * (t - T0) <=> vxB = (xA - xB + vxA * t) / (t - T0)
yA + vyA * t = yB + vyB * (t - T0) <=> vyB = (yA - yB + vyA * t) / (t - T0)
where vxB and vyB will be the components of v' (that is the horizontal and vertical speed of B).
Also, we know that |v'| = sqrt (vxB² + vyB²), or again v'² = vxB² + vyB².
So we have 3 equations with 3 unknowns, which is great.
BUT one of these equations isn't linear, which is… less great, to say the least.
so let's replace them in the last one :
v'² = ((xA - xB + vxA * t) / (t - T0))² + ((yA - yB + vyA * t) / (t - T0))²
or (easier on the eye) : v'² * (t - T0)² = (xA - xB + vxA * t)² + (yA - yB + vyA * t)²
Developping it, we get a quadratic equation : v'² * t² - 2 * v'² * T0 * t + v'² * T0² = xA ² + xB² + vxA² * t² - 2 * xA * xB + 2 * xA * vxA * t - 2 * xB * vxA * t + yA ² + yB ² + vyA² * t² - 2 * yA * yB + 2 * yA * vyA * t - 2 * yB * vyA * t (argh).
We solve it using discriminant, and we get t (we choose the positive one).
Once we have t, it is easy to know the coordinates of the point of impact O :
xO = xA + t * vxA
yO = yA + t * vyA
And now, you know that your missile B will go from point of launch (xB, yB) to impact O (xO, yO), so the angle to give is :
Angle = atan2(yO - yB, xO - xB)
where atan2(y, x) = arctan(y / x) if x > 0
arctan(y / x) +180 if x < 0 and y >= 0
arctan(y / x) - 180 if x < 0 and y < 0
90 if x = 0 and y > 0
-90 if x = 0 and y < 0
whatever you want if x = 0 and y = 0 (in our case that would mean that missile A is going to meet B where we launch B ^^)
Voilà !
Last edited by SBissay (Jan. 17, 2017 17:28:16)
- BASIC4U
-
Scratcher
54 posts
Angle calculations
I haven't gotten around to it yet, but thank you all for your help! 

- gtoal
-
Scratcher
1000+ posts
Angle calculations
What about a missile guided by laser or infrared sensors?
It would do the job easier.
actually, the math behind a heat-seeking missile (where it continuously points itself in the direction of the target, as opposed to what was asked for which was a straight-line path that predicts the intersection point) is *much* harder maths! In fact the problem was the first numerical methods problem I remember doing at University because it was too hard to compute analytically at first-year level. somewhere on scratch (sorry I forget where - one of ffred's or s_federici's studios perhaps?) there's a project that does the heat-seeking missile math.
Graham
- asivi
-
Scratcher
1000+ posts
Angle calculations
Lol, I should have added a “like if”What about a missile guided by laser or infrared sensors?
It would do the job easier.
actually, the math behind a heat-seeking missile (where it continuously points itself in the direction of the target, as opposed to what was asked for which was a straight-line path that predicts the intersection point) is *much* harder maths! In fact the problem was the first numerical methods problem I remember doing at University because it was too hard to compute analytically at first-year level. somewhere on scratch (sorry I forget where - one of ffred's or s_federici's studios perhaps?) there's a project that does the heat-seeking missile math.
Graham
Regards.
- titanscratch
-
Scratcher
100+ posts
Angle calculations
point towards [enemy v] // for both missiles. I don't know how to do the rest.
- BASIC4U
-
Scratcher
54 posts
Angle calculations
That doesn't work when you're trying to point to a clone thoughpoint towards [enemy v] // for both missiles. I don't know how to do the rest.

- scubajerry
-
Scratcher
1000+ posts
Angle calculations
Not exactly the same problem, but may be helpful. Clones are launched from the STAGE.
https://scratch.mit.edu/projects/12622829/#player
https://scratch.mit.edu/projects/12622829/#player
- gtoal
-
Scratcher
1000+ posts
Angle calculations
What about a missile guided by laser or infrared sensors?
It would do the job easier.
actually, the math behind a heat-seeking missile (where it continuously points itself in the direction of the target, as opposed to what was asked for which was a straight-line path that predicts the intersection point) is *much* harder maths! In fact the problem was the first numerical methods problem I remember doing at University because it was too hard to compute analytically at first-year level. somewhere on scratch (sorry I forget where - one of ffred's or s_federici's studios perhaps?) there's a project that does the heat-seeking missile math.
Graham
FOUND IT! https://scratch.mit.edu/projects/3045644/
popswilson. dapontes(gr) had something similar.
(those four users are a great source for *anything* mathemagical)
G
- Discussion Forums
- » Help with Scripts
-
» Angle calculations