Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
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   
  JavaGaming.org - Pastebin



Author: Riven (posted 2014-05-16 16:43:02, viewed 82 times)

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  
public static class Triangle {
      final Point a, b, c;
      final Line ab, bc, ca;

      public Triangle() {
         a = new Point();
         b = new Point();
         c = new Point();

         ab = new Line();
         bc = new Line();
         ca = new Line();
      }

      public void sync() {
         ab.x1 = a.x;
         ab.y1 = a.y;
         ab.x2 = b.x;
         ab.y2 = b.y;

         bc.x1 = b.x;
         bc.y1 = b.y;
         bc.x2 = c.x;
         bc.y2 = c.y;

         ca.x1 = c.x;
         ca.y1 = c.y;
         ca.x2 = a.x;
         ca.y2 = a.y;
      }
   }


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   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216  
   public static List<Triangle> occlude(Triangle litArea, Line occluder) {

      Point abIntersection = new Point();
      Point bcIntersection = new Point();
      Point caIntersection = new Point();

      boolean ab = Line.intersectSegment(litArea.ab, occluder, abIntersection);
      boolean bc = Line.intersectSegment(litArea.bc, occluder, bcIntersection);
      boolean ca = Line.intersectSegment(litArea.ca, occluder, caIntersection);
      int bits = ((ab ? 1 : 0) << 2) | ((bc ? 1 : 0) << 1) | ((ca ? 1 : 0) << 0);

      boolean abSide = litArea.ab.side(occluder.x1, occluder.y1) > 0.0f;
      boolean bcSide = litArea.bc.side(occluder.x1, occluder.y1) > 0.0f;
      boolean caSide = litArea.ca.side(occluder.x1, occluder.y1) > 0.0f;
      boolean inside = abSide == bcSide && bcSide == caSide;

      List<Triangle> out = new ArrayList<Occluder.Triangle>();

      switch (bits) {
      case 0b000: {
         if(inside) {
            Line l1 = new Line();
            l1.x1 = litArea.a.x;
            l1.y1 = litArea.a.y;
            l1.x2 = occluder.x1;
            l1.y2 = occluder.y1;

            Line l2 = new Line();
            l2.x1 = litArea.a.x;
            l2.y1 = litArea.a.y;
            l2.x2 = occluder.x2;
            l2.y2 = occluder.y2;

            Point intersectionBCl = new Point();
            Point intersectionBCr = new Point();
            Line.intersectExtended(litArea.bc, l1, intersectionBCl);
            Line.intersectExtended(litArea.bc, l2, intersectionBCr);

            boolean swap = false;
            swap ^= litArea.ab.side(occluder.x1, occluder.y1) < 0.0f;
            swap ^= occluder.side(litArea.a.x, litArea.a.y) < 0.0f;
            if(swap) { 
               Point tmp = intersectionBCl;
               intersectionBCl = intersectionBCr;
               intersectionBCr = tmp;
            }

            Triangle t1 = new Triangle();
            t1.a.load(litArea.a);
            t1.b.load(litArea.b);
            t1.c.load(intersectionBCl);
            out.add(t1);

            Triangle t2 = new Triangle();
            t2.a.load(litArea.a);
            t2.b.x = occluder.x1;
            t2.b.y = occluder.y1;
            t2.c.x = occluder.x2;
            t2.c.y = occluder.y2;
            out.add(t2);

            Triangle t3 = new Triangle();
            t3.a.load(litArea.a);
            t3.b.load(intersectionBCr);
            t3.c.load(litArea.c);
            out.add(t3);
         }
         break;
      }

      case 0b001: {
         Line center = new Line();
         center.x1 = litArea.a.x;
         center.y1 = litArea.a.y;
         center.x2 = inside ? occluder.x1 : occluder.x2;
         center.y2 = inside ? occluder.y1 : occluder.y2;
         Line.intersectExtended(litArea.bc, center, bcIntersection);

         Triangle t1 = new Triangle();
         t1.a.load(litArea.a);
         t1.b.load(litArea.b);
         t1.c.load(bcIntersection);
         out.add(t1);

         Triangle t2 = new Triangle();
         t2.a.load(litArea.a);
         t2.b.x = center.x2;
         t2.b.y = center.y2;
         t2.c.load(caIntersection);
         out.add(t2);
         break;
      }

      case 0b010: {
         Line center = new Line();
         center.x1 = litArea.a.x;
         center.y1 = litArea.a.y;
         center.x2 = inside ? occluder.x1 : occluder.x2;
         center.y2 = inside ? occluder.y1 : occluder.y2;
         Point bcIntersection2 = new Point();
         Line.intersectExtended(litArea.bc, center, bcIntersection2);

         boolean swap = false;
         swap ^= occluder.side(litArea.a.x, litArea.a.y) < 0.0f;
         swap ^= occluder.side(litArea.b.x, litArea.b.y) > 0.0f;
         if(swap) {
            Triangle t1 = new Triangle();
            t1.a.load(litArea.a);
            t1.b.load(litArea.b);
            t1.c.load(bcIntersection);
            out.add(t1);

            Triangle t2 = new Triangle();
            t2.a.load(litArea.a);
            t2.b.load(bcIntersection);
            t2.c.x = center.x2;
            t2.c.y = center.y2;
            out.add(t2);

            Triangle t3 = new Triangle();
            t3.a.load(litArea.a);
            t3.b.load(bcIntersection2);
            t3.c.load(litArea.c);
            out.add(t3);
         }
         else {
            Triangle t1 = new Triangle();
            t1.a.load(litArea.a);
            t1.b.load(litArea.b);
            t1.c.load(bcIntersection2);
            out.add(t1);

            Triangle t2 = new Triangle();
            t2.a.load(litArea.a);
            t2.b.load(bcIntersection);
            t2.c.load(litArea.c);
            out.add(t2);

            Triangle t3 = new Triangle();
            t3.a.load(litArea.a);
            t3.b.x = center.x2;
            t3.b.y = center.y2;
            t3.c.load(bcIntersection);
            out.add(t3);
         }
         break;
      }

      case 0b011: {
         Triangle t1 = new Triangle();
         t1.a.load(litArea.a);
         t1.b.load(bcIntersection);
         t1.c.load(caIntersection);
         out.add(t1);

         Triangle t2 = new Triangle();
         t2.a.load(litArea.a);
         t2.b.load(litArea.b);
         t2.c.load(bcIntersection);
         out.add(t2);
         break;
      }

      case 0b100: {
         Line center = new Line();
         center.x1 = litArea.a.x;
         center.y1 = litArea.a.y;
         center.x2 = inside ? occluder.x1 : occluder.x2;
         center.y2 = inside ? occluder.y1 : occluder.y2;
         Line.intersectExtended(litArea.bc, center, bcIntersection);

         Triangle t1 = new Triangle();
         t1.a.load(litArea.a);
         t1.b.load(abIntersection);
         t1.c.x = center.x2;
         t1.c.y = center.y2;
         out.add(t1);

         Triangle t2 = new Triangle();
         t2.a.load(litArea.a);
         t2.b.load(bcIntersection);
         t2.c.load(litArea.c);
         out.add(t2);
         break;
      }

      case 0b101: {
         Triangle t1 = new Triangle();
         t1.a.load(litArea.a);
         t1.b.load(abIntersection);
         t1.c.load(caIntersection);
         out.add(t1);
         break;
      }

      case 0b110: {
         Triangle t1 = new Triangle();
         t1.a.load(litArea.a);
         t1.b.load(abIntersection);
         t1.c.load(bcIntersection);
         out.add(t1);

         Triangle t2 = new Triangle();
         t2.a.load(litArea.a);
         t2.b.load(bcIntersection);
         t2.c.load(litArea.c);
         out.add(t2);
         break;
      }

      default:
         throw new IllegalStateException("occluder cannot intersect all sides of triangle");
      }

      return out;
   }


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19  
   public static class Point {
      public float x, y;

      public void load(Point src) {
         x = src.x;
         y = src.y;
      }

      @Override
      public String toString() {
         return "[" + x + ", " + y + "]";
      }

      public static float distance(Point a, Point b) {
         float dx = a.x - b.x;
         float dy = a.y - b.y;
         return (float) Math.sqrt(dx * dx + dy * dy);
      }
   }


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  
   public static class Line {
      public float x1, y1;
      public float x2, y2;

      public float side(float x, float y) {
         return (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1);
      }

      public static boolean intersectSegment(Line a, Line b, Point intersection) {
         float x1 = a.x1, y1 = a.y1, x2 = a.x2, y2 = a.y2;
         float x3 = b.x1, y3 = b.y1, x4 = b.x2, y4 = b.y2;
         float x1_x2 = x1 - x2;
         float x3_x4 = x3 - x4;
         float y1_y2 = y1 - y2;
         float y3_y4 = y3 - y4;

         float det = x1_x2 * y3_y4 - y1_y2 * x3_x4;
         if(det == 0.0f) {
            return false;
         }

         float cx, cy, dx, dy;
         float x1y2_y1x2 = (x1 * y2 - y1 * x2);
         float x3y4_y3x4 = (x3 * y4 - y3 * x4);
         float px = (x1y2_y1x2 * x3_x4 - x1_x2 * x3y4_y3x4) / det;
         float py = (x1y2_y1x2 * y3_y4 - y1_y2 * x3y4_y3x4) / det;

         dx = px - (cx = (x1 + x2) * 0.5f);
         dy = py - (cy = (y1 + y2) * 0.5f);
         if((dx * dx) + (dy * dy) < (x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy)) {
            dx = px - (cx = (x3 + x4) * 0.5f);
            dy = py - (cy = (y3 + y4) * 0.5f);
            if((dx * dx) + (dy * dy) < (x3 - cx) * (x3 - cx) + (y3 - cy) * (y3 - cy)) {
               intersection.x = px;
               intersection.y = py;
               return true;
            }
         }

         return false;
      }

      public static boolean intersectExtended(Line a, Line b, Point intersection) {
         float x1 = a.x1, y1 = a.y1, x2 = a.x2, y2 = a.y2;
         float x3 = b.x1, y3 = b.y1, x4 = b.x2, y4 = b.y2;
         float x1_x2 = x1 - x2;
         float x3_x4 = x3 - x4;
         float y1_y2 = y1 - y2;
         float y3_y4 = y3 - y4;

         float det = x1_x2 * y3_y4 - y1_y2 * x3_x4;
         if(det == 0.0f) {
            return false;
         }

         float x1y2_y1x2 = (x1 * y2 - y1 * x2);
         float x3y4_y3x4 = (x3 * y4 - y3 * x4);
         float px = (x1y2_y1x2 * x3_x4 - x1_x2 * x3y4_y3x4) / det;
         float py = (x1y2_y1x2 * y3_y4 - y1_y2 * x3y4_y3x4) / det;

         intersection.x = px;
         intersection.y = py;
         return true;
      }
   }





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

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

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

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

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

lcass (43 views)
2014-10-15 16:18:58
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!