[SOLVED] Computing the offset of two 2D shape projections (related to SAT)

Hey guys!..

I’ve got a pretty easy question for you… I can’t solve this trivial problem. Probably due to the fact I was coding all day today. I’ve implemented lots of Vector and Matrix math and the Seperating axis theorem for collision detection. My head buuuuuurnssssss…

It almost works, but it’s somehow a little bit “off”, when detecting collisions… anyways, here is the code for the computation of the overlaps:


private static boolean noProjOverlap(Vec2 proj0, Vec2 proj1) {
	return (proj0.x >= (proj1.y) || ((proj0.y) <= proj1.x));
}

private static float projOverlap(Vec2 proj0, Vec2 proj1) {
	return Math.min(Math.abs(proj0.x - proj1.y), Math.abs(proj0.y - proj1.x));
}

I use Vec2’s for representing projections, where the x field is “min” and the y field is “max”.
Followed this super-duper awesome tutorial.

I’ve got a problem with the second function: It always returns positive number, obviously due to the “Math.abs”…

The first function works, but it’s the function which “produces” this “off”-ness… :confused:

I’m sure you can help me with this trivial math :slight_smile:

What’s wrong with the second function returning a positive number.
Don’t you want to know how much it overlaps by? (I’m assuming that’s the prupose of the function)
If it was a negative number then it would mean it WASN’T overlapping, therefore destroying the purpose of calling that function when you could just call the first function and find that they weren’t overlapping.

Yeah, but returning a negative number indicating, that it doesn’t overlap is the purpose of this function as well…

But doesn’t the first function do that?


private static float projOverlap(Vec2 proj0, Vec2 proj1) {
	return Math.sqrt(Math.min((proj0.x - proj1.y)*(proj0.x - proj1.y), (proj0.y - proj1.x)*(proj0.y - proj1.x)));
}

This is a guess here, as I don’t know what you are trying to achieve. ???
I’m assuming you want the value closest to zero.

[EDIT:] You can remove the sqrt by adding an if-else to the function and returning the non-squared values if you want performance.

Nah, you’re calculating the length of the vector inbetween the two vectors. This is not what I wanted…

The problem is this one: (I want “gap”)
`
SCENARIO 1:
min0 max0
|-------|
|--------------|
min1 max1
|–|
gap (negative in this case)


SCENARIO 2:
min0 max0
|-----------| min1 max1
|-----------|
|--------|
gap (positive in this case)
`

Hope this helps…
But thank you anyways :slight_smile:

Oh, and:
min0 = proj0.x
max0 = proj0.y
min1 = proj1.x
max1 = proj1.y


private static float projOverlap(Vec2 proj0, Vec2 proj1)
{
	if(Math.abs(proj0.x - proj1.y) < Math.abs(proj0.y - proj1.x))
		return proj0.x - proj1.y;
	else
		return proj0.y - proj1.x;
}

Is that right?

Eh, no, it didn’t work. It didn’t return something negative either.

I fixed the problem :confused:

When creating the normals for the sides of the polygons, I forgot to build the perpendicular of the normalized delta vectors…

Yeah… a mistake, which happens if you code more than 12 hours a day :wink:

Anyways, great thanks HeroesGraveDev! :slight_smile: