Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (654) 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 312 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.

 SHC (28 views) 2015-08-01 03:58:20 Jesse (20 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (22 views) 2015-07-27 15:35:20 Riven (24 views) 2015-07-27 12:26:13 Riven (15 views) 2015-07-27 12:23:39 BurntPizza (36 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (31 views) 2015-07-24 06:06:53 NoxInc (37 views) 2015-07-22 22:16:53
 theagentd 51x wessles 49x basil_ 35x KaiHH 26x orangepascal 25x ags1 20x Riven 20x mooman219 17x bornander 15x KudoDEV 13x CelestialCreator 11x princec 11x Jesse 10x klaus 10x pquiring 10x philfrei 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection 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:21
 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