Marcus von Appen avatar Marcus von Appen committed 90912cb

Collision fixes.

Comments (0)

Files changed (5)

 * Added vertical scrolling support via GameField.scrollY().
 * Added SpriteGroup.render() and Sprite.render() overloads, which allow
   to force redrawing of sprites.
+* Added SpriteGroup.updateDirtyAreaSprites() method for updating sprites
+  in dirty areas.
 * Added simple collision and scrolling example.
 * Fixed artifact drawing for Sprite instances.
 * Renamed moveX() and moveY() methods of ParallaxBackground to setX()
   <target name="compile-test" depends="clean,init"
           description="compile the source with tests">
     <javac srcdir="${src}" destdir="${build}"
-           classpath="lib/jl1.0.1.jar">
+           classpath="lib/jl1.0.1.jar" debug="on">
         <compilerarg value="-Xlint:all"/>
     </javac>
   </target>

src/org/ljgf/sprites/Sprite.java

         this.x = x;
         this.oldx = x;
         this.y = y;
-        this.oldy = y;
         this.setDirty ();
     }
     

src/org/ljgf/sprites/collision/RectCollisionArea.java

      */
     public int bottom ()
     {
-        return this.y - this.height;
+        return this.y + this.height;
     }
     
     /**
         int tcx = this.x + this.width / 2;
         int tcy = this.y + this.height / 2;
 
+        // System.out.println ("ax: " + ax + " ay: " + ay + " ar: " + ar
+        //     + " ab: "  + ab + " acx: " + acx + " acy: " + acy);
+        // System.out.println ("tx: " + tx + " ty: " + ty + " tr: " + tr
+        //     + " tb: "  + tb + " tcx: " + tcx + " tcy: " + tcy);
+
         // Special cases: if the areas have no width or height, a
         // collision can't happen.
         if (tb == ty && tr == tx || ar == ax && ab == ay)
             return CollisionType.NO_COLLISION;
         
-        if (tb > ay) // this above area
+        if (tb < ay) // this above area
             return CollisionType.NO_COLLISION;
-        if (ab > ty) // area above this
+        if (ab < ty) // area above this
             return CollisionType.NO_COLLISION;
         if (tr < ax) // this left from area
             return CollisionType.NO_COLLISION;
         if (ar < tx) // area left from this
             return CollisionType.NO_COLLISION;
-         
-        // this._r1.setBounds (tx, ty, this.width, this.height);
-        // this._r2.setBounds (ax, ay, area.width, area.height);
-        // rr = this._r1.intersection (this._r2);
         
         // Now the different collision types.
         if (tcx == acx && tcy == acy)
         int spclr = Math.abs (tx - ar);
         int spcrl = Math.abs (tr - ax);
         int res = Math.min (spctb, Math.min (spcbt, Math.min (spclr, spcrl)));
+
+        // System.out.println ("stb: " + spctb + " sbt: " + spcbt + " slr: " +
+        //     spclr + " srl: "  + spcrl + " sres: " + res);
+
         if (res == spctb)
             return CollisionType.TOP_BOTTOM_COLLISION;
         if (res == spcbt)

src/org/ljgf/test/RectCollisionAreaTest.java

         Assert.assertEquals (2, area.y);
         Assert.assertEquals (4, area.height);
         Assert.assertEquals (2, area.top ());
-        Assert.assertEquals (-2, area.bottom ());
+        Assert.assertEquals (6, area.bottom ());
         area.y = -5;
         Assert.assertEquals (-5, area.y);
         Assert.assertEquals (4, area.height);
         Assert.assertEquals (-5, area.top ());
-        Assert.assertEquals (-9, area.bottom ());
+        Assert.assertEquals (-1, area.bottom ());
         area.y = 5;
         Assert.assertEquals (5, area.y);
         Assert.assertEquals (4, area.height);
         Assert.assertEquals (5, area.top ());
-        Assert.assertEquals (1, area.bottom ());
+        Assert.assertEquals (9, area.bottom ());
     }
 
     @Test
         // rect1 overlaps with the top of rect2
         area1 = new RectCollisionArea (0, 0, 5, 8);
         area2 = new RectCollisionArea (0, -5, 8, 9);
+        Assert.assertEquals (CollisionType.TOP_BOTTOM_COLLISION,
+            area1.intersects (area2));
         Assert.assertEquals (CollisionType.BOTTOM_TOP_COLLISION,
-            area1.intersects (area2));
-        Assert.assertEquals (CollisionType.TOP_BOTTOM_COLLISION,
             area2.intersects (area1));
 
         // rect1 overlaps with the bottom of rect2
         area1 = new RectCollisionArea (0, 5, 5, 8);
         area2 = new RectCollisionArea (0, 10, 8, 9);
+        Assert.assertEquals (CollisionType.BOTTOM_TOP_COLLISION,
+            area1.intersects (area2));
         Assert.assertEquals (CollisionType.TOP_BOTTOM_COLLISION,
-            area1.intersects (area2));
-        Assert.assertEquals (CollisionType.BOTTOM_TOP_COLLISION,
             area2.intersects (area1));
 
         area1.width = area1.height = 10;
                 else if (j == 0)
                     Assert.assertEquals (CollisionType.CENTER_COLLISION, t);
                 else if (j < 0)
+                    Assert.assertEquals (CollisionType.BOTTOM_TOP_COLLISION, t);
+                else if (j > 0)
                     Assert.assertEquals (CollisionType.TOP_BOTTOM_COLLISION, t);
-                else if (j > 0)
-                    Assert.assertEquals (CollisionType.BOTTOM_TOP_COLLISION, t);
                 
                 area1.x = i;
                 t = area1.intersects (area2);
         t = area1.intersects (area2);
         Assert.assertEquals (CollisionType.NO_COLLISION, t);
 
+        area1 = new RectCollisionArea (119, 64, 64, 64);
+        area2 = new RectCollisionArea (128,  0, 64, 64);
+        t = area1.intersects (area2);
+        Assert.assertEquals (CollisionType.TOP_BOTTOM_COLLISION, t);
+
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.