Checking if two vectors are equal.

Oct 24, 2009 at 4:51am
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.
Oct 24, 2009 at 5:01am
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).
Oct 24, 2009 at 5:07am
In this case what would be an ideal value for the epsilon in your example. Or should I calculate it based on what the 2 vectors are?

Thanks.
Oct 24, 2009 at 5:12am
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.
Oct 24, 2009 at 5:16am
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.
Last edited on Oct 24, 2009 at 5:17am
Oct 24, 2009 at 5:22am
No thats fine - just the answer I was looking for helios - Just wanted to see what sort of values you might use for an epsilon in this case.

One last thing. For the abs in this case the x - y. Should I be doing 2 checks in terms of.

1
2
3
4
5
6
7
8
9
10

const float epsilon = 0.01f;

Vector2 v1;
Vector2 v2;

if (abs(v1.x-v2.x)<=epsilon)
...
if (abs(v1.y-v2.y)<=epsilon)
...


If they both return true then the Vectors are equal?
Oct 24, 2009 at 6:01am
Yes; if both the x and the y values are within the epsilon value you basically saying they are "close enough" to be considered equal.
Oct 24, 2009 at 9:15am
Awesome, thanks guys.
Oct 25, 2009 at 2:50am
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.
Oct 25, 2009 at 3:38am
wat
Oct 25, 2009 at 3:39am
Not really sure what your getting at there... not sure at all?
Topic archived. No new replies allowed.