Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (542) Games in Android Showcase (133) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 i need help with a weird logic problem (kinda long topic)  (Read 266 times) 0 Members and 1 Guest are viewing this topic.
alaslipknot
 « Posted 2013-05-08 21:07:32 »

Hello,
i've been working on my tiled map "system" and today i found a new a problem which is normally do because of a bad use of an "if statement"
here is the thing :
i have an aabb collision class :
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `public boolean aabb(double x1, double y1, double w1, double h1, double x2,         double y2, double w2, double h2) {      double cntrX1 = x1 + w1 / 2;      double cntrY1 = y1 + h1 / 2;      double cntrX2 = x2 + w2 / 2;      double cntrY2 = y2 + h2 / 2;      double distanceX = Math.abs(cntrX1 - cntrX2);      double distanceY = Math.abs(cntrY1 - cntrY2);      double sumWidth = (w1 / 2 + w2 / 2);      double sumHeight = (h1 / 2 + h2 / 2);      if (distanceX < sumWidth) {         if (distanceY < sumHeight) {            return true;         }      }      return false;   }`

which i use to check collision between the player and the grid elements here :
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `private void collisionBooleans() {      String[][] myWorld = world.getWorld();      for (int y = 0; y < height; y++) {         for (int x = 0; x < width; x++) {            int blockX = x * 32;            int blockY = y * 32;            String block = myWorld[y][x];            if (tool.aabb(hero.getX(), hero.getY(), hero.getWidth(),                  hero.getHeight(), blockX, blockY, 32, 32)) {               System.out.println("world");               if (block.equals("g")) {                  onGround = true;                  System.out.println("ground");               } else {                  System.out.println("!ground");                  onGround = false;               }            }         }      }`

just to give you an idea about "g" , this is my tile map file
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 `*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g   g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g `

and this is how the game look

until now everything is fine, but once i draw another map elements ("g")
the collision detection doesn't work with the last drawn element for example :
i have create 3 blocks of type ground, if am on the 1st 2 of them (from the left) then everything is fine

but if am on the last one, it doesn't work

now here is the weird part, i turned off the gravity and make the program print something only if am on the ground, and guess what, it does print even if am standing on the last block which doesn't suppose to work

finally here is another 2 methods that you might need,
this one use the class CollisionBooleans to do stuff (gravity and jumping (jumping still doesn't work  ) )
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `private void collisionLogick() {      if (onGround) {         canJump = true;      } else {         heroY -= gravity * delta;         canJump = false;      }      double jum = 0;      if (jumping) {         jum += 0.5;         hero.setY(hero.getY() + jum);      }         }`

and this is the player update method that i call in the main update method
 1  2  3  4  5  6  7  8  9  10  11 `private void heroUpdate() {      control(delta);      hero.setX(heroX);      hero.setY(heroY);      hero.setWidth(heroW);      hero.setHeight(heroH);            collisionBooleans();      collisionLogick();   }`

thank you very much

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
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.
 Elsealabs (19 views) 2014-12-28 10:39:27 CopyableCougar4 (20 views) 2014-12-28 02:10:29 BurntPizza (25 views) 2014-12-27 22:38:51 Mr.CodeIt (15 views) 2014-12-27 04:03:04 TheDudeFromCI (20 views) 2014-12-27 02:14:49 Mr.CodeIt (26 views) 2014-12-23 03:34:11 rwatson462 (58 views) 2014-12-15 09:26:44 Mr.CodeIt (47 views) 2014-12-14 19:50:38 BurntPizza (98 views) 2014-12-09 22:41:13 BurntPizza (116 views) 2014-12-08 04:46:31
 Rayvolution 53x BurntPizza 31x basil_ 29x HeroesGraveDev 24x LiquidNitrogen 21x appel 18x kpars 18x KevinWorkman 15x Riven 15x princec 14x Spasi 14x NegativeZero 13x pitbuller 13x gouessej 13x SHC 13x KudoDEV 12x
 How 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:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50
 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