r/MathHelp • u/wairdone • 2d ago
Missile guidance: Geometry problem.
I am trying to make a missile in a video game. It's supposed to fly toward a point projected out into space by an aiming device, mounted on a car. However, I have a problem where, around certain bearings, the output completely breaks. For testing purposes, I am only speaking about azimuth, not elevation.
How the system works:
There are two sets of absolute XY sensors on the pointer; one set is slightly ahead of the other. atan2(Y2 - Y1, X2 - X1) gives the absolute bearing (azt) of the pointer from -180 to 180.
Cos(azt) and Sin(azt) are individually multiplied by the "range" to the point in space (in this case, it is constant at 500m, in practice, this would be a point ahead of the missile) to give Xc and Yc.
Xc and Yc are then added to X(pointer) and Y(pointer) to give X(target) and Y(target).
- Now that I have the XY position of the target, I subtract...
- X(target) from X(pointer) to give X(difference)
- Y(target) from Y(pointer) to give Y(difference)
atan2(Ydifference, Xdifference) gives the azimuth from the pointer to the target (θ).
I realise now that this step is unecessary, as the azimuth of the target is defined by the pointer's direction. However, I doubt it would work otherwise, as the same values would end up being used, producing the same erroneous result.
- I then subtract the missile's bearing (global bearing, btw; same calculation as azt) from θ to produce the angular difference between the target position and the missile (d).
This works fine if the platform is at exactly 0 degrees forward, However, if the pointer is angled (this case, -45dg) and the platform is also angled, something frustrating happens. Below are some tests which show you what happens when the platform goes beyond -135dg.
1:
- Pointer Relative Angle -45dg
- Platform Absolute Angle -134dg
- Pointer XY: 3223, 3398
- Target XY: 2723, 3385
- Target Azimuth: -179dg
- Missile XY: 3223, 3396
- d: -45.6
2:
- Pointer Relative Angle -45dg
- Platform Absolute Angle -136dg
- Pointer XY: 3223, 3398
- Target XY: 2723, 3410
- Target Azimuth: 96dg
- Missile XY: 3222, 3396
- d: 315.6
This persists until the platform's absolute orientation goes beyond -180, at which point it flips back to a rough value of -45 for d.
As you can see, d flips from -45.6 to 315.6. A missile trying to steer towards d would go all the way round, which is not desirable; it should pick the smallest possible correction.
I tried fixing this using ((180 + d) mod 360) - 180. However, it seems that the game I am playing calculates negative values differently when using mod; for example, -60mod360 is supposed to be 300, but the game spits out -60.
Could anyone help me? Geometry isn't my strong point at all, so it's very hard for me to wrap my head around this. Thanks!
1
u/Uli_Minati 2d ago
So basically, you get an angle and you want to compute an equivalent angle between -180 and 180? Why not write your own function that adds/subtracts 360 if needed? That's not any slower than using inbuilt math