distance between two points. This is how i did it.

public static float GetDistance(float x1, float y1, float x2, float y2)

            return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
// and in the Draw();
_spriteBatch.DrawString(font, "Score: " + _distance, new Vector2(100, 100), Color.White);

// in the Update();

_distance = GetDistance(_boxA.X, _boxA.Y, _boxB.X, _boxB.Y);

I dont know if this is 100% right. pls confirm.

You can use Vector2.Distance() and Vector2.DistanceSquared()

Im sorry i cant understand, im new to this coding thing. I tried .Distance but i got error under Distance. thats why i went this way.

Can you pls put that in code example? Thats the best way to lern.

You’re missing squareroot:

 return Math.Sqrt( ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) );

It’s not showing up as an option, or when you run it gives you an error? Vector2.Distance and Vector2.DistanceSquared require Vector2s as parameters.

Btw .DistanceSquared is typically used over .Distance when we are comparing multiple distances because it doesn’t use Math.Sqrt (which is a more expensive operation), but won’t give you the exactly correct mathematical output; if you compare two distances using either of these methods, you will always get the correct true or false return.


yeap! you are right, but i tried. did not understand the hole thing but i know little more now :slight_smile: thanks

float distance = Vector2.Distance(vector_A , vector_B);

1 Like

no .Distance give me error. I will try to recreate the error., probably something i dont understand yeat!

i see it now ;D so simple but hard to se for a noob :smiley:

yea! that whas the problem, my brother tried to say it to, but i whas stubborn. _knowIhaveLearnedSomething! thanks :slight_smile:

thanks for all good help :slight_smile: if i can help in the future i whil be here :slight_smile:

I want to clarify this point, Vector2.Distance and Vector2.DistanceSquared are both as accurate and correct as possible within the precision of a single(float) floating point number, assuming FMA hardware,(otherwise the multiply loses a few bits in both operations.)

It is always faster to use Vector2.DistanceSquared and square the other side of the inequality.
The only times I use Distance is for normalization and UI display.