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.
3 Likes
yeap! you are right, but i tried. did not understand the hole thing but i know little more now 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
yea! that whas the problem, my brother tried to say it to, but i whas stubborn. _knowIhaveLearnedSomething! thanks
thanks for all good help if i can help in the future i whil be here
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.
I use .Distance all the time. Can you shine some light on the above? Or run through a small example, so I’m sure I understand?
.Distance uses square root which is slow on computers, so if you don’t necessarily need to know the exact distance of things, and are only comparing distances, use .DistanceSquared.
Instead of using:
Vector2 p1, p2;
//if (p1 - p2). Distance()< 5;`
if (p1 - p2). DistanceSquared() < 5 * 5;`
The above example used fixed values, but it is the same for variables, Given x
is a scalar and y
is a Vector2
:
x*x < y.DistanceSquared()
is always faster than x < y.Distance();
There are times when the square root is needed normalization being one.
If there is a way to avoid the square root, do it.
Another faster distance method is the Manhattan distance:
d=(Math.Abs(a.X - b.Y) + Math.Abs(a .Y- b.Y)
Where a
and b
are Vector2
s.
This distance involves a diamond shaped zone instead of a circular zone.
.