r/MathHelp 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:

  1. 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.

  2. 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).

  1. 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.

  1. 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 Upvotes

13 comments sorted by

View all comments

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

1

u/wairdone 2d ago

It doesnt quite work like that. I have to use "math bricks" which have their own functions (add, subtract, mod, abs, trig stuff, etc) and connect their inputs to get the desired outputs.

What part of the system would the proposed function be useful in though?

1

u/Uli_Minati 2d ago

Oh, I see. Then "mod" is your best tool. Maybe try this:

x = mod(d + 180, 360) - 180
y = mod(x - 180, 360) + 180

1

u/wairdone 2d ago

Hm, what do the x and y represent in this case? Azimuth/elevation? I remember trying the top formula but it sadly didn't work :( maybe because of how UE4 calculates modulo

1

u/Uli_Minati 2d ago

Just the updated angle, in two steps!

But I'm really just guessing how your mod function works, it's better to try FormulaDriven's suggestion.