Distance between 2 points

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Morseton
Posts: 8
Joined: July 4th, 2012, 10:29 pm

Distance between 2 points

Post by Morseton » July 6th, 2012, 8:07 am

I find this to be very useful, if you want a player to collide with something, instead of checking if the players position is at the objective position, you can calculate the distance from the player and the objective, if its less than 1, do codes, I'm not sure if this works in C++, but I've tryed and came here to check with you guyz if it, because I some errors

Code: Select all

??? Distance2D( int x1, int y1, int x2, int y2 );
{<--- ?
	float Dist;
	int X = ( x2 - x1 );
	int Y = ( y2 - y1 );
	Dist = sqrt( (X*X) + (Y*Y ));
	return Dist;
}
??? = Don't really know what to put there, void won't work because void is when you don't return any value, but in this case you must return the distance value.

<--- ? = error c2447: '{' : missing function header (old-style format list?)

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Distance between 2 points

Post by LuX » July 6th, 2012, 9:32 am

remove the ";"
...From "Distance2D( int x1, int y1, int x2, int y2 );" <-----

But if you already make a function of it, why waste time and space by creating new variables when you can put the calculation on one line?
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Distance between 2 points

Post by chili » July 6th, 2012, 9:34 am

Yeah, and the ??? should be "float".
Chili

Morseton
Posts: 8
Joined: July 4th, 2012, 10:29 pm

Re: Distance between 2 points

Post by Morseton » July 6th, 2012, 10:15 am

thanks chili and LuX, and LuX, I don't know how to make a number square :s
btw sqrt isn't working:
error C2668 'sqrt': ambiguous call to overloaded function.

edit... googled this error and changed everything to:

Code: Select all

float Distance2D( int x1, int y1, int x2, int y2 )
{
	float Dist = sqrt( (float)( ( (x2 - x1)*(x2 - x1) ) + ( (y2 - y1)*(y2 - y1) ) ) );
	return Dist;
}
now everything works, thanks
Last edited by Morseton on July 6th, 2012, 10:24 am, edited 1 time in total.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Distance between 2 points

Post by chili » July 6th, 2012, 10:22 am

I covered this in the tutorials. There are 2 versions of sqrt: one that takes a float and one that takes a double. If you pass it an int, it doesn't know whether to convert to float or double, and thus the call is ambiguous.

Solution:
Dist = sqrt( (float)(X*X + Y*Y) );
Chili

Morseton
Posts: 8
Joined: July 4th, 2012, 10:29 pm

Re: Distance between 2 points

Post by Morseton » July 6th, 2012, 11:04 am

I've put it to test with these codes:

Code: Select all

	if( Distance2D(mouse_x, mouse_y, 400, 300) <5 )
	{
		gfx.PutPixel( 100, 100, 255, 255, 255 );
	}

Code: Select all

float Distance2D( int x1, int y1, int x2, int y2 )
{
	float Dist = sqrt( (float)( ( (x2 - x1)*(x2 - x1) ) + ( (y2 - y1)*(y2 - y1) ) ) );
	return Dist;
}
error C2120: 'void' illegal with all types
what am I doing wrong?

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Distance between 2 points

Post by chili » July 6th, 2012, 11:28 am

Not sure brah; post your solution.
Chili

Morseton
Posts: 8
Joined: July 4th, 2012, 10:29 pm

Re: Distance between 2 points

Post by Morseton » July 6th, 2012, 1:29 pm


User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Distance between 2 points

Post by LuX » July 6th, 2012, 2:50 pm

You could have uploaded the project here on this site.

You had a couple of problems:
1. In game.h you define the functions as void. You can't return a void float? Makes no sense.
2. In game.cpp You need to put Game:: in front of the function, else it will look for a global function definition, which doesn't exist.
3. Not really a problem, but you don't need to make a variable to be returned, just return the calculation: "return sqrt( (float)( ( (x2 - x1)*(x2 - x1) ) + ( (y2 - y1)*(y2 - y1) ) ) );"
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Distance between 2 points

Post by chili » July 6th, 2012, 2:54 pm

LuX wrote:You could have uploaded the project here on this site.

You had a couple of problems:
1. In game.h you define the functions as void. You can't return a void float? Makes no sense.
2. In game.cpp You need to put Game:: in front of the function, else it will look for a global function definition, which doesn't exist.
3. Not really a problem, but you don't need to make a variable to be returned, just return the calculation: "return sqrt( (float)( ( (x2 - x1)*(x2 - x1) ) + ( (y2 - y1)*(y2 - y1) ) ) );"
Word. 8-)
Chili

Post Reply