comparing floats

Started by
6 comments, last by Rob Loach 19 years, 1 month ago
I need to compare the position of an object, with the position of the mouse. the problem is the mouse is always a whole number and the object rarely is I dont expect them to always meet exactly. whats the best way to go about comparing them to see if there are within one or have the same whole part. I tried (int) to truncate it but they still only coincide rarely or when the object is traveling at certain angles.
Advertisement
You don't really want to know whether they meet *exactly*, as you said. Instead, what you really want to see is whether they are "close enough". That is, whether the distance between them is below a particular "border" distance. So just find the distance between the position and the target position, and see if it's less than the border distance.
can you not round the float to the nearest whole number and then compare?
roger that. I got it now. I was trying to do
((floor(ib->x) == idest->x || ceil(ib->x) == idest->x) && (floor(ib->y) == idest->y || ceil(ib->y) == idest->y))

Lol... Its amazing how true it is that hard code to write is ussually written the wrong way.
now its
(ib -idest) > dist

thanks man.
If you want the mouse to be within a certain limit in each direction, you could easily find the difference between the positions, and check to see if that is within the range you specify.

float posX, posY; //Predefinedfloat maxDist = 2.0;    //The distance the mouse can be from the objectint mouseX, mouseY; //Also predefined//Get differencesfloat diffX = posX - mouseX;float diffY = posY - mouseY;//compare with the limit you specify:if(diffX >= -maxDist && diffX <= maxDist     //Make sure it is within a distance of zero   && diffY >= -maxDist && diffY <= maxDist){   //Do whatever


If you want a circular hotspot, just change it to deal with the pythagorean theorem. x^2 + y^2 = z^2, so if the x and y distances, squared and added, are less than the max distance squared, you are good to go.
float posX, posY; //Predefinedfloat maxDist = 4.0;    //2 squared this timeint mouseX, mouseY; //Also predefined//Get differencesfloat diffX = posX - mouseX;float diffY = posY - mouseY;diffX = diffX * diffX; //Square for pythagorean theoremdiffY = diffY * diffY;float diff = diffX + diffY;  //Total distance//compare with the limit you specify:if(diff <= maxDist)   //because of squares, only need to check that it is under the maximum                      //And only need to check one thing.{   //Do whatever
--Eric BurnettI know I have mnemophobia, but I can't remember what it is.
[source lang=cpp]struct vector2f{    float x,y;    vector2f(float x_, float y_) : x(x_), y(y_) {}    void normalize()    {        float len = sqrt(x*x + y*y);        x /= len;        y /= len;    }	vector2f vabs()	{		vector2f temp(0,0);		temp.x = abs(x);		temp.y = abs(y);		return temp;	}    vector2f operator-(vector2f const& other)    {        return vector2f(x - other.x, y - other.y);    }	vector2f operator+(vector2f const& other)	{		return vector2f(x + other.x, y + other.y);	}	vector2f operator*(vector2f const& other)	{		return vector2f(x * other.x, y * other.y);	}		int operator>(vector2f const& other)	{		if(x >= other.x && y >= other.y) return 1;		else return 0;	}	int operator<(vector2f const& other)	{		if(x < other.x && y < other.y) return 1;		else return 0;	}	};void Update_Bullets(){	vector2f temp(0,0);	ib = bullets.begin();	id = dirs.begin();	idest = dests.begin();	while((ib != bullets.end() && id != dirs.end()) && idest != dests.end())	{		//move bullets according to dir and speed		*ib = *ib + (*id * speed);		// compensate for changes in viewpoint		if(old_world_x < world_x) ib->x-= TILE_SIZE * (world_x - old_world_x); 		if(old_world_x > world_x) ib->x+= TILE_SIZE * (old_world_x - world_x);		if(old_world_y < world_y) ib->y-= TILE_SIZE * (world_y - old_world_y); 		if(old_world_y > world_y) ib->y+= TILE_SIZE * (old_world_y - world_y);				temp = *ib - *idest;		temp = temp.vabs();		//erase offscreen bullets		if((ib->x <= 0 || ib->x >= SCREEN_W) || (ib->y <= 0 || ib->y >= SCREEN_H) || (temp < thresh))		{			bullets.erase(ib);			dirs.erase(id);			dests.erase(idest);		}		else		{			ib++;			id++;			idest++;		}	}	//reset veiw checking values	old_world_x = world_x;	old_world_y = world_y;}


this seems to work for me ;) I'd never have gotten his far with everyone help thanks everyone.
except when world_x or y != old_world_x the dont stop where they should... hmm
Typically (read: almost always) you compare non-integers with a certain "tolerance". ie if(abs(floatOne - floatTwo) <= TOLERANCE) rather than if(floatOne == floatTwo). This also avoids certain strangeness due to how non-integers are stored since in some cases only a close approximation can be stored (think 4.99999999999 instead of 5) which can lead to maths ops on paper producing different results on the computer due to these little innacuracies accumulating.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
Quote:Original post by SoulSkorpion
if(abs(floatOne - floatTwo) <= TOLERANCE)
That's what I use.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement