Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (168) games submitted by our members Games in WIP (646) 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 1063 times) 0 Members and 1 Guest are viewing this topic.
matheus23

JGO Kernel

Medals: 122
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: 359
Projects: 11
Exp: 3 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: 122
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: 359
Projects: 11
Exp: 3 years

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

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

But doesn't the first function do that?

HeroesGraveDev

JGO Kernel

Medals: 359
Projects: 11
Exp: 3 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: 122
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: 122
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: 359
Projects: 11
Exp: 3 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: 122
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.

 deepthought (47 views) 2015-06-30 15:39:44 deepthought (50 views) 2015-06-30 15:39:09 deepthought (62 views) 2015-06-30 15:36:52 Za\'Anzabar (37 views) 2015-06-29 05:44:54 TritonDreyja (46 views) 2015-06-24 17:10:40 CopyableCougar4 (46 views) 2015-06-23 00:34:45 BurntPizza (52 views) 2015-06-21 20:36:46 cookiecompiler (93 views) 2015-06-11 15:42:53 cookiecompiler (55 views) 2015-06-11 15:41:14 NegativeZero (80 views) 2015-06-11 09:49:18
 princec 29x wessles 22x BurntPizza 19x CopyableCougar4 19x opiop65 17x ags1 15x nsigma 15x EgonOlsen 14x Riven 13x theagentd 12x KaiHH 12x SauronWatchesYou 11x KevinWorkman 11x Archive 11x sunburn 11x NegativeZero 10x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 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