Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (495) Games in Android Showcase (114) games submitted by our members Games in WIP (563) 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 773 times) 0 Members and 1 Guest are viewing this topic.
matheus23

JGO Kernel

Medals: 107
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: 250
Projects: 11
Exp: 2 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: 107
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: 250
Projects: 11
Exp: 2 years

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

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

But doesn't the first function do that?

HeroesGraveDev

JGO Kernel

Medals: 250
Projects: 11
Exp: 2 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: 107
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: 107
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: 250
Projects: 11
Exp: 2 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: 107
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

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 Dwinin (28 views) 2014-09-12 09:08:26 Norakomi (57 views) 2014-09-10 13:57:51 TehJavaDev (72 views) 2014-09-10 06:39:09 Tekkerue (37 views) 2014-09-09 02:24:56 mitcheeb (57 views) 2014-09-08 06:06:29 BurntPizza (43 views) 2014-09-07 01:13:42 Longarmx (27 views) 2014-09-07 01:12:14 Longarmx (34 views) 2014-09-07 01:11:22 Longarmx (34 views) 2014-09-07 01:10:19 mitcheeb (40 views) 2014-09-04 23:08:59
 BurntPizza 33x princec 20x Riven 19x Rayvolution 17x ags1 16x basil_ 15x KevinWorkman 14x kevglass 12x theagentd 12x nsigma 11x LiquidNitrogen 10x SHC 8x The Lion King 7x HeroesGraveDev 7x cylab 6x EgonOlsen 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 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