Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (767) Games in Android Showcase (229) games submitted by our members Games in WIP (854) 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 635 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

 EgonOlsen (1256 views) 2018-06-10 19:43:48 EgonOlsen (1124 views) 2018-06-10 19:43:44 EgonOlsen (862 views) 2018-06-10 19:43:20 DesertCoockie (1271 views) 2018-05-13 18:23:11 nelsongames (1104 views) 2018-04-24 18:15:36 nelsongames (1335 views) 2018-04-24 18:14:32 ivj94 (2074 views) 2018-03-24 14:47:39 ivj94 (1226 views) 2018-03-24 14:46:31 ivj94 (2169 views) 2018-03-24 14:43:53 Solater (792 views) 2018-03-17 05:04:08
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 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