Sign in to follow this  

comparing floats

This topic is 4664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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; //Predefined
float maxDist = 2.0; //The distance the mouse can be from the object
int mouseX, mouseY; //Also predefined

//Get differences
float 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; //Predefined
float maxDist = 4.0; //2 squared this time
int mouseX, mouseY; //Also predefined

//Get differences
float diffX = posX - mouseX;
float diffY = posY - mouseY;
diffX = diffX * diffX; //Square for pythagorean theorem
diffY = 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

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this