Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (780) Games in Android Showcase (233) games submitted by our members Games in WIP (857) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [SOLVED] Computing the offset of two 2D shape projections (related to SAT)  (Read 2592 times) 0 Members and 1 Guest are viewing this topic.
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Posted 2012-11-17 20:54:35 »

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:
 1  2  3  4  5  6  7 `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... :/

I'm sure you can help me with this trivial math

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #1 - Posted 2012-11-17 21:05:08 »

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.

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #2 - Posted 2012-11-17 21:07:05 »

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

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #3 - Posted 2012-11-17 21:07:54 »

But doesn't the first function do that?

HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #4 - Posted 2012-11-17 21:12:01 »

 1  2  3 `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.

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #5 - Posted 2012-11-17 21:29:45 »

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

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #6 - Posted 2012-11-17 21:31:39 »

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

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #7 - Posted 2012-11-17 21:36:02 »

 1  2  3  4  5  6  7 `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?

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #8 - Posted 2012-11-17 22:03:32 »

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

I fixed the problem :/

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

Anyways, great thanks HeroesGraveDev!

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
 ignore  |  Print

 hadezbladez (1165 views) 2018-11-16 13:46:03 hadezbladez (525 views) 2018-11-16 13:41:33 hadezbladez (1185 views) 2018-11-16 13:35:35 hadezbladez (270 views) 2018-11-16 13:32:03 EgonOlsen (2567 views) 2018-06-10 19:43:48 EgonOlsen (2786 views) 2018-06-10 19:43:44 EgonOlsen (1564 views) 2018-06-10 19:43:20 DesertCoockie (2267 views) 2018-05-13 18:23:11 nelsongames (2120 views) 2018-04-24 18:15:36 nelsongames (2814 views) 2018-04-24 18:14:32
 Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org