I'm going to do my average, routine critiques that I do to everybody in the interest of making their code better. I don't have anything against you personally or your code. I'm not judging you. </disclaimer>
Sorry, I have to say that so that all of the forum hippies don't go on their usual "OMFG FALCO IS AN ARROGANT PRICK" bitchspree... If you're
rewriting your engine, I would assume that you are interested in producing a quality product and would much prefer realistic, practical advice over sitting around a fire singing kumbaya with me...
THe Floating Brain wrote:Code: Select all
#define Magn(x, y, Mag) Mag = ((x)*(x) + (y)*(y))
This is actually magnitude SQUARED or the dot product of vector <x,y> with itself (same thing). Your macro is misnamed.
THe Floating Brain wrote:
You're squaring that value AGAIN before taking the square root. Your magnitude is incorrect.
I take it that this function is to return a vector in the direction from <X, Y> to <GotoX, GotoY> with a magnitude of the desired speed. The problem is that your vector is incorrect, because your "squared magnitude" was squared yet again before taking the square root.
Also, I highly recommend you rethink your design here. This is a critical, core mathematics class. In my opinion, you're over-encapsulating your vector data by making it a class rather than a structure. You should always be wary of get/set accessor methods in classes like this. Speed is critical here, especially considering the fact that you're gaining nothing but overhead by encapsulating three floats in your vector...
Now to the main question...
THe Floating Brain wrote:But I am receiving a N.A.N value from it which throws off my entire collision calculation (im using this to calculate vectors for collision detection). [..]I am wondering how to handle the N.A.N variable after I detect it as N.A.N.
You're going about it incorrectly. The correct way is not to have special logic to handle a NAN. The correct way is to have special logic to
prevent a NAN.
NAN is not something that should ever happen in well-designed code. Certain architectures may not even gracefully return a NAN, they might shit their pants.
The only place that this function can even potentially produce a NAN (that I can see) is during the floating point division (divide-by-zero). That's only going to happen when vector <GotoX, GotoY> == <X, Y>, which means that you're already at the location that you're trying to head to. You can much more gracefully add that check BEFORE the mathematics and return early (preventing you from even having to calculate anything in that case).