Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 SAT collision detection - handling parallel edges  (Read 1998 times) 0 Members and 1 Guest are viewing this topic.
Danik

Junior Newbie

 « Posted 2010-04-23 21:27:01 »

Hi
I have a problem with a collision detection method I have written. I use the separating axis theorem to detect collisions and the detection is working well.
The problem is that the minimum translation vector I get from the method is sometimes wrong if there are parallel edges in the polygons.

Parallel edges, although in opposite directions, give the same overlap distance, so it's just a matter of chance if the returned vector will point in the right direction.

How would I go about to fix this?

My code:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101 `   /**    * Checks to see it there is a collision between two convex polygons.     * If there is a collision it returns a vector representing the minimal     * translation necessary to move 'a' so that it is not overlapping 'b',     * else a vector with x and y equal to 0.    *     * The returned vector is pointing towards the first polygon 'a'.    *     * Uses the separating axis theorem for convex polygons.    *     * @param a The first polygon    * @param b The second polygon    * @return The minimum translation vector directed towards the first     * polygon, or a Vector with (x,y)==(0,0) if there is no overlap.    */   Vector getCollisionSAT(Polygon a, Polygon b) {      ArrayList aVerts = new ArrayList(a.getVertices());      ArrayList bVerts = new ArrayList(b.getVertices());            ArrayList normals = new ArrayList();            // Add normals of a to the list of normals      for (int i = 0; i < aVerts.size(); i++) {            Vector p1, p2;                  p1 = aVerts.get(i);         p2 = aVerts.get((i + 1 == aVerts.size()) ? 0 : i + 1);                  Vector normal = p2.subtract(p1).perpendicularCCW();         normal = normal.normalize();                  normals.add(normal);               }            // Add normals of b to the list normals      for (int i = 0; i < bVerts.size(); i++) {            Vector p1, p2;                  p1 = bVerts.get(i);         p2 = bVerts.get((i + 1 == bVerts.size()) ? 0 : i + 1);                  Vector normal = p2.subtract(p1).perpendicularCW();         normal = normal.normalize();                  normals.add(normal);               }            Vector minOverlapVector = null;      double minOverlap = Double.MAX_VALUE;            for (Vector normal : normals) {                  double aMin = aVerts.get(0).dotProduct(normal); // some value in the range         double aMax = aMin;                  // Project each vertex of a onto the current normal and save the min and max value         for (Vector v : aVerts) {            double dot = v.dotProduct(normal);            if (dot < aMin) {               aMin = dot;            }            else if (dot > aMax) {               aMax = dot;            }         }                  double bMin = bVerts.get(0).dotProduct(normal); // some value in the range         double bMax = bMin;                  // Do the same for b         for (Vector v : bVerts) {            double dot = v.dotProduct(normal);            if (dot < bMin) {               bMin = dot;            }            if (dot > bMax) {               bMax = dot;            }         }                           // Compare min and max values to see if there is an overlap         if (aMin > bMax || bMin > aMax) {            return new Vector(0,0); // no overlap - no collision         }                  else {            double overlap = Math.min(aMax - bMin, bMax - aMin);             if (overlap <= minOverlap) {               minOverlap = overlap;               minOverlapVector = normal.scalarMultiplication(overlap);            }         }         }            return minOverlapVector;         }`
Danik

Junior Newbie

 « Reply #1 - Posted 2010-04-24 16:32:12 »

I have solved the problem with the following code right before the "return minOverlapVector".
It checks if the vector between the two polygons is pointing in the opposite direction from the translation vector and if so reverses it.
It's a bit of a hack though, so if anyone has any insight on how to do it properly I would appreciate it.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `      // The following is not optimal but is used to find the correct       // direction of minOverlapVector until a better solution is found.      Vector aMidPoint = new Vector();      Vector bMidPoint = new Vector();      for ( Vector v : aVerts) {         aMidPoint = aMidPoint.add(v);      }      for ( Vector v : bVerts) {         bMidPoint = bMidPoint.add(v);      }      aMidPoint = aMidPoint.scalarDivision(aVerts.size());      bMidPoint = bMidPoint.scalarDivision(bVerts.size());            Vector ba = aMidPoint.subtract(bMidPoint);            if (ba.dotProduct(minOverlapVector) < 0) {         minOverlapVector = minOverlapVector.scalarMultiplication(-1);         System.out.println("switch");      }      `
Pages: [1]
 ignore  |  Print

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

 Ralphanese (19 views) 2015-05-29 13:07:55 Gibbo3771 (16 views) 2015-05-29 06:54:17 Ralphanese (23 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (36 views) 2015-05-27 22:21:08 CopyableCougar4 (27 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (33 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21
 Riven 25x Rayvolution 24x theagentd 22x BurntPizza 19x Spasi 17x orangepascal 16x KevinWorkman 15x Drenius 15x ra4king 13x DavidBVal 12x princec 12x opiop65 12x EgonOlsen 11x Husk 11x kevglass 9x Slyth2727 8x
 Intersection 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:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02
 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