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

JGO Kernel


Medals: 110
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 Smiley

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

JGO Kernel


Medals: 269
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.

Offline matheus23

JGO Kernel


Medals: 110
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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


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


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

But doesn't the first function do that?

Offline HeroesGraveDev

JGO Kernel


Medals: 269
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.  Huh
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.

Offline matheus23

JGO Kernel


Medals: 110
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 Smiley

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

JGO Kernel


Medals: 110
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
Offline HeroesGraveDev

JGO Kernel


Medals: 269
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?

Offline matheus23

JGO Kernel


Medals: 110
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 Wink

Anyways, great thanks HeroesGraveDev! Smiley

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.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (69 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (61 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (46 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!