Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  LWJGL - AABB Problem  (Read 740 times)
0 Members and 1 Guest are viewing this topic.
Offline ADGDeveloping

Senior Newbie


Exp: 2 years



« Posted 2014-03-07 20:56:16 »

Hello I got a lil problem.
I made a camera class and added AABB.
AABB on the Y axis works.
AABB on the X and Z axis don't.

I can walk into a block, at each side (Except up and down (Y Axis)), I tried many things and idk how to fix.
When walking on a block its OK. When walking to a block. I can get inside and it pushes me down inside the world.
The world is empty from the inside because it's not visible so not rendered (Face checking for more fps).

Y Axis Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
    if(space && !shift){
       if(Game.world.getBlock(this.x, this.y - 3, this.z) != null){
          this.y -= 0F;
       }else{
          this.y += speed * delta * 0.003F;
       }
    }else if(!space && !shift){
       if(Game.world.getBlock(this.x, this.y - 3, this.z) != null){
          this.y += 0F;
       }else{
          this.y -= (speed / 8) * 1F;
       }
    }


X And Z Axis Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
    if((keyUp)){
       if(Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z) != Block.air && Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z) == null && !Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z).isOpaque() || Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2) != Block.air && Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2) == null && !Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2).isOpaque()) {
          moveLookDir(0.0F, 0.0F, -1.0F);
       }else{
          moveLookDir(0.0F, 0.0F, -speed * delta * 0.003F);
       }
    }
   
    if((keyDown)){
       if(Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z) != Block.air && Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z) == null && !Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z).isOpaque() || Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2) != Block.air && Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2) == null && !Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2).isOpaque()) {
          moveLookDir(0.0F, 0.0F, -1.0F);
       }else{
          moveLookDir(0.0F, 0.0F, speed * delta * 0.003F);
       }
    }
   
    if((keyRight)){
       if(Game.world.getBlock((int)this.x, (int)this.y, (int)this.z - 2) != Block.air && Game.world.getBlock((int)this.x, (int)this.y, (int)this.z - 2) == null && !Game.world.getBlock((int)this.x, (int)this.y, (int)this.z - 2).isOpaque() || Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2) != Block.air && Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2) == null && !Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2).isOpaque()) {
          moveLookDir(-1.0F, 0.0F, 0.0F);
       }else{
          moveLookDir(speed * delta * 0.003F, 0.0F, 0.0F);
       }
    }
   
    if((keyLeft)){
       if(Game.world.getBlock((int)this.x, (int)this.y, (int)this.z - 2) != Block.air && Game.world.getBlock((int)this.x, (int)this.y, (int)this.z - 2) == null && !Game.world.getBlock((int)this.x, (int)this.y, (int)this.z - 2).isOpaque() || Game.world.getBlock((int)this.x  - 2, (int)this.y, (int)this.z - 2) != Block.air && Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2) == null && !Game.world.getBlock((int)this.x - 2, (int)this.y, (int)this.z - 2).isOpaque()) {
          //do collision response for x axis. In this case, you can simply not move the player. Repeat this if statement for the y and z axis accordingly. You must check each axis separately though.
         moveLookDir(-1.0F, 0.0F, 0.0F);
       }else{
          moveLookDir(-speed * delta * 0.003F, 0.0F, 0.0F);
       }
    }  


I hope someone can help me.
If u need more detail or code tell me.

Thanks for reading
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2014-03-07 21:08:27 »

I think you need to make your code a bit more readable.

Offline ADGDeveloping

Senior Newbie


Exp: 2 years



« Reply #2 - Posted 2014-03-07 21:33:58 »

What do you mean?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2014-03-07 21:53:24 »

The if statements are a few screens wide.

Make some functions to avoid duplicate code.

Offline ADGDeveloping

Senior Newbie


Exp: 2 years



« Reply #4 - Posted 2014-03-07 22:38:20 »

It's not duplicate code Smiley
It's all the code needed to check if the block isn't Air, isn't null, isOpaque and hasBoundings.
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2014-03-07 22:45:33 »

Yes, but you're doing it 4 times. And any tiny error could be in just one of them and will be extremely hard to find.

Offline ADGDeveloping

Senior Newbie


Exp: 2 years



« Reply #6 - Posted 2014-03-07 22:56:40 »

Each of the 4 iss for different positions. Smiley

1: Forward
2: Backward
3: right
4: Left
Online BurntPizza
« Reply #7 - Posted 2014-03-07 23:06:05 »

Think he means using something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
//where x, y, and z are offsets from this.* for convenience
private Block getBlock(int x, int y, int z) {
      ...
}

//so then you can do this:

if((keyUp)){
      Block b = getBlock(-2, 0, 0);
      Block b2 = getBlock(-2, 0, -2);

      if(b != Block.air && b == null && !b.isOpaque() || b2 != Block.air && b2 == null && !b2.isOpaque()) {
            moveLookDir(0.0F, 0.0F, -1.0F);
      }else{
            moveLookDir(0.0F, 0.0F, -speed * delta * 0.003F);
      }
}


Don't have to be that 'aggressive' with the simplification, but it cleans it up a bit.

Also not much changes between those 4 direction check blocks. You could pretty easily evaluate a conditional expression at the several places where the information does change, for example:

1  
moveLookDir(keyRight || keyLeft ? -1.0F : 0.0F, 0.0F, keyUp || keyDown ? -1.0F : 0.0F);
Offline ADGDeveloping

Senior Newbie


Exp: 2 years



« Reply #8 - Posted 2014-03-08 12:42:10 »

Ah ok, Smiley, Ill send the code soon.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2014-03-08 13:09:51 »

It's not duplicate code Smiley

Usually we're not talking about 100% duplicate code, but about seeing repetitive patterns.

BurntPizza made the first step with the
getBlock()
method, which I'd like to call
getRelativeBlock()
for now  Pointing

You could also add another function for finding out, whether a block is visible:

1  
2  
3  
4  
5  
6  
public boolean isVisibleBlock(Block b) {
    return b != null && b != Block.air && !b.isOpaque();
}
public boolean isRelativeBlockVisible(int x, int y, int z) {
    return isBlockVisible(getRelativeBlock(x, y, z));
}

And then you could simplify your code and make it more readable (only x-axis code for now):
1  
2  
3  
4  
5  
6  
7  
8  
float xResponsiveMove = keyRight || keyLeft ? -1.0f : 0.0f;
float zResponsiveMove = keyUp || keyDown ? -1.0f, 0.0f;

if (isRelativeBlockVisible(-2, 0, 0) || isRelativeBlockVisible(-2, 0, -2)) {
    moveLookDir(xResponsiveMove, 0.0F, zResponsiveMove);
} else {
    moveLookDir(0.0F, 0.0F, -speed * delta * 0.003F);
}


Which is, (I admit it Wink ) pretty much what BurntPizza suggested Grin

This syntax:
1  
float variable = someStatement ? oneExpression : anotherExpression;

de-sugers into this:
1  
2  
float variable = anotherExpression;
if (someStatement) variable = oneExpression;


(not quite, but almost. The difference with strictness doesn't need to be mentioned here...)

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!