I haven't actually wrote anything for this yet but I'm just wondering the best way to go about it.
Say i have a Vector2:
1 2 3 4 5
class Vector2
{
//Not going to throw everything in here you get the idea
float x, y;
}
Say i want to write an == and != operator for it. So i can check if instance1 == instance2 of vector2.
Now considering in this case something like this would be used for a co-ord on a 2D map. So the players position is equal to (x = 20.0f, y = 25.123f) what would be the best check to see if something else is in the same spot as him. As if they were (x = 20.000001f, y = 25.123f) it's going to return false. Which in this case would be a pain in the arse.
So how can I go about checking like only a couple of digits after the decimal point or something like that. Something thats neat and not ugly for the situation.
Position of objects would be updated by a Delta Time movement. So this would be the main reason I need to do better checks.
The most common method to compare floats is
if (abs(x-y)<=epsilon) //equal
epsilon being an arbitrarily small value (but not so small that the check is always false).
I think the value would depend on how detailed you get in tracking character positions. Would a character be considered in a different position between, say 20.002 and 20.003? Or would such a small difference be only because of the floating point error? It's all based on how accurate you want it to be.
Exactly. It obviously depends on the application, but I think for a game an epsilon of .01 or .005 should be enough. Again, it depends on the application. I don't know what kind of magnitudes your vectors are dealing with.
Consider the following pseudo code:
Suppose you have two int vectors x and y.
if(vector<int>x==vector <int>y)cout<<"TRUE";
else cout<<"FALSE";
If you instanciate vector x then:
x=y;
cout<< will always be TRUE.