Distance calculations

Hi everyone,

I am trying to use b2Distance for calculating the distance between two shapes. However, it seems to ruin collision detection (i.e. shapes fall through other shapes). Can anyone shed some light on this? How would I go about calculating the distance if not with this?

If I only need two points, wouldn't

Math.sqrt(Math.abs(vector1.x - vector2.x) + Math.abs(vector1.y - vector2.y))

do the trick?

Cheers,
Toby
b2Distance, eh, what's that?

Code:
var lengthVec:b2Vec2 = vector1.Copy();
lengthVec.Substract(vector2);

lengthVec.Length() //that's what you need
Neosano wrote:
b2Distance, eh, what's that?

It's in the latest SVN version...
SVN SVN SVN SVN, it's time to make a release...
Anyway, my method works.
toby wrote:
If I only need two points, wouldn't

Math.sqrt(Math.abs(vector1.x - vector2.x) + Math.abs(vector1.y - vector2.y))

do the trick?


Neosano's code probably works, but for the record, you want a formula like...

Math.sqrt(((vector1.x - vector2.x) * (vector1.x - vector2.x)) + ((vector1.y - vector2.y) * (vector1.y - vector2.y)));

Or, if you prefer slightly shorter code...

Math.sqrt(Math.pow((vector1.x - vector2.x), 2) + Math.pow((vector1.y - vector2.y), 2));

Think of it like creating a right triangle where your two points define the diagonal, and then you use the Pythagorean theorem to solve it.
slightly shorter code is mine, and it works in the same way as your code does.

Original thread: https://box2d.org/forum/viewtopic.php?t=2604