Can std::complex be used in C++ programming?

I’m building a shooting mechanic for my game in C++. I have included functionality for aiming to moving targets and this comes down to an grade 4 equation:

228926-gravity-plus-motion-4.png

The only reliable and exact solution to solving this equation seems to be using complex numbers. My implementation is using std::complex to do much of the calculations.

Are there any compatibility / deployability issues with std::complex? I’ve seen people on the forums say that containers like std::vector or std::stack are not recommended.

Note: I intend to deploy on Windows, Android and maybe others. Definitely not on MacOS or iOS.

This might be a silly question but why would you be interested in complex zeroes of this polynomial -
surely you’re looking for real-valued solutions? You could also extend this implementation to 3D.

I should be fine as long as it not using it with UPROPERTY() or UFUNCTION() or other things like that. you need to understand that not using those means that engine don’t see them, but on other hand engine don’t support that type so it really does not matter.

There 2 major reason why they using C++ std library as well as other types from elsewhere is not recommended:

-UE4 has set of own core implementations, some are just wrappers of std, platform, system, third party libraries and other APIs. Point of it is to make UE4 code and much as portable to supported platform as possible without you UE4 deweloper to even think about it. By using something else UE4 don’t give you guaranty that it will work (or atleast inform that it does not work on specific platfrom) with any target platform case. Sadly UE4 indeed don’t have any implementation of complex numbers, whole there is alternatives for std::vector and std::stack.

-UE4 code expects you to use only UE4 APIs, which means a) reflection system don’t support them (mentioned U macros above) b) using it with UE4 APIs risks crashes as UE4 don’t know what to do with them, UE4 also might be not prepared for what specific library doing as it was not design to cooperate with it. Other potantion compatibiliuty issues is advance UE4 memory manipulation might have potention issue dilling with object that use those types, epcially if you implement to asset type. But as long as you using external types and function with isolation from the engine it should be fine.

Best practice here is to do what UE4 is doing, make your own implementation of complex numbers in UE4 or try creating a wrapper from UE4 APIs, simply create FComplex struct, try to make std::complex to be operatable C++ primitive type that UE4 support, if oyu up to iyt you can even make custom UI for your property type and bluepritn nodes so you can it in Blueprints too. And as point 1 saying UE4 and Epic won’t guaranty you that things will work out and work on every platform (you will need to implement specific platfrom support if needed on your own), you on your own in this journey.

Also look up how UE4 deals with third party libraries or find plugins that do so, those are best examples you can find to how to interact with things outside of UE4 APIs and how to make them work with UE4.

Thanks for answering!

Before I posted this question i already created a FComplex struct with operators but i tried using and got horrible results. After a few days of debugging nothing has changed…
Then a mathematician friend of mine pointed out that I was not calculating the complex argument correctly ( I was using atan(y/x) instead of atan2(y, x) ) and this solved all the issues.

I will post tomorrow my FComplex code in case this question comes up in any searches and someone might need it.

Update: Code can be found on Git. I also included my function for solving quartic equations.

I am only looking for real solutions, of course.
I know it sounds weird, but calculating these requires the use of complex numbers (in rare cases, but they still show up).

If you can use UE4’s FMath insted of math.h it will will integrate UE4 code even more and reduce risk of any compatibility issues