Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  i need help with a weird logic problem (kinda long topic)  (Read 234 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-05-08 23: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  Roll Eyes ) )
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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!