Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: How to get the tiles around a player for collision detection? on: 2013-12-12 04:40:36
Correct, but I'm simply trying to get the four tiles around the player. Your method is far to advanced for what I need to do, and it's driving me crazy that no one can help me.
2  Game Development / Newbie & Debugging Questions / Re: How to get the tiles around a player for collision detection? on: 2013-12-12 04:22:37
No offense, but that really doesn't help as you're using the Rectangle class, and I'm using my own AABB class. If I was using the Rectangle.intersects method, I would already be done. But I don't want to, I want to figure it out my own way.
3  Game Development / Newbie & Debugging Questions / Re: How to get the tiles around a player for collision detection? on: 2013-12-12 04:11:14
Really, no one can help me solve this simple problem? It's not like what I'm doing is hard... I just don't know what I'm doing wrong.
4  Game Development / Newbie & Debugging Questions / Re: AABB collision detection fails when moving to the left on: 2013-12-12 02:58:19
Guys, I really need help please. I've been trying to figure this out for a week now and I can't...

I tried to rewrite my move method, and now it's this:
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  
private void move(int x, int y, float sx, float sy) {
      Vector2f tempPos = new Vector2f((pos.getX() + (pos.getX() + 32)) / 2, (pos.getY() + (pos.getY() + 32)) / 2);
      box.update(new Vector2f(tempPos.getX() + sx, tempPos.getY() + sy), 16);
      if (getX() / 32 + x > 0 && getX() / 32 + x < world.mx && getY() / 32 + y > 0 && getY() / 32 + y < world.my) {

         for (int xx = 0; xx <= 1; xx++) {
            for (int yy = 0; yy <= 1; yy++) {
               if (world.getTile((int) (getX() / 32) + xx, (int) (getY() / 32) + yy) != null) {
                  Tile t = world.getTile((int) (getX() / 32) + xx, (int) (getY() / 32) + yy);
                  t.color = true;
                  if (t.type != 0 && !Collision.isColliding(box, t.getBox())) {
                     setPos(getX() + sx, getY() + sy);
                  } else if (t.type == 0) {
                     setPos(getX() + sx, getY() + sy);
                  }
               } else {
                  setPos(getX() + sx, getY() + sy);
               }
            }
         }

      } else {
         setPos(getX() + sx, getY() + sy);
      }
      glPushMatrix();
      glTranslatef(getX(), getY(), 0);
      glPopMatrix();
   }

I now have two four loops to get the tiles above and below the player, but now he only collides with tiles if he is in the middle of four solid tiles. I'm using this for my collision detection:
1  
2  
3  
4  
5  
6  
7  
8  
public static boolean isColliding(AABB a, AABB b) {
      if (Math.abs(a.pos.getX() - b.pos.getX()) < a.size + b.size) {
         if (Math.abs(a.pos.getY() - b.pos.getY()) < a.size + b.size) {
            return true;
         }
      }
      return false;
   }

But my main issue is that the move method does not work at all, and I have no idea why. Can anyone please just point me to a link or show me some code that selects the tiles around the player, and then checks to see if the player is colliding with them?

I'm so desperate!
5  Game Development / Newbie & Debugging Questions / Re: AABB collision detection fails when moving to the left on: 2013-12-11 04:38:08
So I figured out my problem was that when I move onto two tiles at once,  I get the tile that I am standing on, but I actually round down so that the tile I get is actually the incorrect one. My question is, how do I get the tiles surrounding the player, and then check to see if any of them collide with the player? My player is one tile tall/wide so I only need to get the four tiles around him. But I think the way I am doing it right now is wrong because it's so hard to implement, and it's such an easy idea. Any help?
6  Game Development / Newbie & Debugging Questions / How to get the tiles around a player for collision detection? on: 2013-12-09 03:12:57
I implemented some basic collision detection using AABBs and it mostly works, except for the fact that when I move down towards a collidable object, the player can pass through, but only when approaching the object after moving to the left. Here's a picture of what I mean:


If you can see, the player (the white square), is intersecting a tile. The player can only pass through a tile when there is no tiles under the player, and you are moving left and then down. Here's my collision detection code:

1  
2  
3  
4  
5  
6  
7  
8  
    public static boolean isColliding(AABB a, AABB b) {
      if (Math.abs(a.pos.getX() - b.pos.getX()) < a.size + b.size) {
         if (Math.abs(a.pos.getY() - b.pos.getY()) < a.size + b.size) {
            return true;
         }
      }
      return false;
   }

The pos variable is actually the center of the AABB, and the size variable is half the size. Here's how I detect a collision in the entity class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
    private void move(float x, float y, float sx, float sy) {
      if (getX() / 32 + x >= 0 && getX() / 32 + x <= world.mx && getY() / 32 + y >= 0 && getY() / 32 + y <= world.my) {
         if (world.tiles[(int) ((int) getX() / 32 + x)][(int) ((int) getY() / 32 + y)] != null) {
            Vector2f tempPos = new Vector2f((pos.getX() + (pos.getX() + 32)) / 2, (pos.getY() + (pos.getY() + 32)) / 2);
            box.update(new Vector2f(tempPos.getX() + sx, tempPos.getY() + sy), 16);
            Tile t = world.tiles[(int) ((int) getX() / 32 + x)][(int) ((int) getY() / 32 + y)];
            t.color = true;
            if (!Collision.isColliding(box, t.getBox())) {
               setPos(getX() + sx, getY() + sy);
            } else if (Collision.isColliding(box, t.getBox())) {
               if (!onGround)
                  onGround = true;
            }
         } else {
            setPos(getX() + sx, getY() + sy);
         }
      } else {
         setPos(getX() + sx, getY() + sy);
      }
      glPushMatrix();
      glTranslatef(getX(), getY(), 0);
      glPopMatrix();
   }

Specifically these lines are important:

1  
2  
3  
4  
5  
6  
    Vector2f tempPos = new Vector2f((pos.getX() + (pos.getX() + 32)) / 2, (pos.getY() + (pos.getY() + 32)) / 2);
            box.update(new Vector2f(tempPos.getX() + sx, tempPos.getY() + sy), 16);
            Tile t = world.tiles[(int) ((int) getX() / 32 + x)][(int) ((int) getY() / 32 + y)];
            t.color = true;
            if (!Collision.isColliding(box, t.getBox())) {
               setPos(getX() + sx, getY() + sy);


I construct a new Vector (tempPos) that is the center of the entity in question. Then I update the AABB with the center of the entity, and then I add either sx or sy, which is the movespeed, generally speaking this is usually set to 8, so I move 8 pixels every update, and the tiles are 32 pixels in size. Then I get the tile immediately to the wherever the player is moving using the x and y variables.

Just for reference, here is my keyboard update code for the player:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
         move(0, 1, 0, MOVESPEED);
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
         move(1, 0, MOVESPEED, 0);
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
         move(-1, 0, -MOVESPEED, 0);
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
         move(0, -1, 0, -MOVESPEED);
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && onGround) {
         onGround = false;
         vY = 4f;
      }

If I change this line:

1  
    box.update(new Vector2f(tempPos.getX() + sx, tempPos.getY() + sy), 16);

To instead add the x and y variables, like this:

1  
                box.update(new Vector2f(tempPos.getX() + x, tempPos.getY() + y), 16);


Then the collision works correctly, but now it doesn't detect collisions on the y axis at all. x and y are constrained between 1 and -1, as you can see in the keyboard update code. It's used to look up the tile where the player wants to move, so I don't know why only the x axis is working, and not the y.

Sorry if I didn't explain this well enough, ask me and I'll clarify anything!
7  Game Development / Newbie & Debugging Questions / Re: Collision response on two axis on: 2013-12-03 04:10:21
Hey guys, sorry for bumping the topic, but I solved part of my error, but now a new problem arose. So, I figured out how to move even when colliding, but now it seems that I get stuck in the entity I'm performing collision detection on at random times. For instance, sometimes I get stuck in the player when I hit him from above, but other times I don't. I don't know why this is happening though as I'm moving the player very slow (2 px), so it can't be because I move too fast and the game has no time to calculate collisions! Here's my collision detection code:
1  
2  
3  
4  
5  
6  
public static boolean isColliding(AABB a, AABB b){
      if(a.pos.getX() + a.size >= b.pos.getX() && a.pos.getX() <= b.pos.getX() + b.size && a.pos.getY() + a.size >= b.pos.getY() && a.pos.getY() <= b.pos.getY() + b.size){
         return true;
      }
      return false;
   }

And this is how I check for collisions in my entity class:
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  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
public void updatePlayer(Entity enemy) {
      if (isPlayer) {
         if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
            Vector2f fPos = new Vector2f(position.getX(), position.getY() - MOVESPEED);
            box.update(fPos, SIZE);
            if (!Collision.isColliding(box, enemy.box)) {
               position = fPos;
               move(0, -MOVESPEED);
            } else {
               box.update(position, SIZE);
               move(0, 0);
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
            Vector2f fPos = new Vector2f(position.getX(), position.getY() + MOVESPEED);
            box.update(fPos, SIZE);
            if (!Collision.isColliding(box, enemy.box)) {
               position = fPos;
               move(0, MOVESPEED);
            } else {
               box.update(position, SIZE);
               move(0, 0);
            }
           
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
            Vector2f fPos = new Vector2f(position.getX() - MOVESPEED, position.getY());
            box.update(fPos, SIZE);
            if (!Collision.isColliding(box, enemy.box)) {
               position = fPos;
               move(-MOVESPEED, 0);
            } else {
               box.update(position, SIZE);
               move(0, 0);
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
            Vector2f fPos = new Vector2f(position.getX() + MOVESPEED, position.getY());
            box.update(fPos, SIZE);
            if (!Collision.isColliding(box, enemy.box)) {
               position = fPos;
               move(MOVESPEED, 0);
            } else {
               box.update(position, SIZE);
               move(0, 0);
            }
         }
      }
   }

This is my AABB class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class AABB {
   
   public Vector2f pos;
   public float size;
   
   public AABB(Vector2f pos, float size){
      this.pos = pos;
      this.size = size;
   }
   
   public void update(Vector2f pos, float size){
      this.pos = pos;
      this.size = size;
   }

}

You'll notice I create a future position vector (fPos) every time a key is pressed, and then I update the AABB with it so I can perform collisions and see if they will happen. I then revert back to the old position if I don't collide. The move method is this:
1  
2  
3  
4  
5  
6  
7  
private void move(float x, float y) {
      this.position.setPosition(position.getX() + x, position.getY() + y);
      box.update(position, SIZE);
      glPushMatrix();
      glTranslatef(position.getX(), position.getY(), 0);
      glPopMatrix();
   }

Is this a common problem with AABBs? Why is this happening?
8  Java Game APIs & Engines / OpenGL Development / Why are display lists running faster than VBOs in my game? on: 2013-11-27 03:15:49
I created two simple voxel engines, literally just chunks that hold cubes. For the first one, I use display lists and can render hundreds of chunks at 60 FPS no problem, despite the fact that the technology behind it is years old and deprecated by now. With my VBO version, I try to render 27 chunks and I suddenly drop to less than 50 FPS. What gives? I use shaders for my VBO version, but not for display list one. Without shaders for the VBO version, I still get the same FPS rate. I'll post some relevant code:

VBO
-----
Initialization of chunk:

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  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
    public void initGL() {
      rand = new Random();
     
      sizeX = (int) pos.getX() + CHUNKSIZE;
      sizeY = (int) pos.getY() + CHUNKSIZE;
      sizeZ = (int) pos.getZ() + CHUNKSIZE;

      tiles = new byte[sizeX][sizeY][sizeZ];

      vCoords = BufferUtils.createFloatBuffer(CHUNKSIZE * CHUNKSIZE * CHUNKSIZE * (3 * 4 * 6));
      cCoords = BufferUtils.createFloatBuffer(CHUNKSIZE * CHUNKSIZE * CHUNKSIZE * (4 * 4 * 6));

      createChunk();

      verticeCount = CHUNKSIZE * CHUNKSIZE * CHUNKSIZE * (4 * 4 * 6);

      vCoords.flip();
      cCoords.flip();

      vID = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vID);
      glBufferData(GL_ARRAY_BUFFER, vCoords, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);

      cID = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, cID);
      glBufferData(GL_ARRAY_BUFFER, cCoords, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }
    private void createChunk() {
      for (int x = (int) pos.getX(); x < sizeX; x++) {
         for (int y = (int) pos.getY(); y < sizeY; y++) {
            for (int z = (int) pos.getZ(); z < sizeZ; z++) {
               if (rand.nextBoolean() == true) {
                  tiles[x][y][z] = Tile.Grass.getId();
               } else {
                  tiles[x][y][z] = Tile.Void.getId();
               }
               vCoords.put(Shape.createCubeVertices(x, y, z, 1));
               cCoords.put(Shape.getCubeColors(tiles[x][y][z]));
            }
         }
      }
   }

And then rendering:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
    public void render() {
      glBindBuffer(GL_ARRAY_BUFFER, vID);
      glVertexPointer(3, GL_FLOAT, 0, 0L);

      glBindBuffer(GL_ARRAY_BUFFER, cID);
      glColorPointer(4, GL_FLOAT, 0, 0L);

      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);

      shader.use();
      glDrawArrays(GL_QUADS, 0, verticeCount);
      shader.release();

      glDisableClientState(GL_COLOR_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
   }

I know I use quads, and that's bad, but I'm also using quads for my display list engine. The shaders are very simple, all they do is take a color and apply it to the vertices, I won't even post them they are that simple.

Display List
------------
Initialization:

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  
    public void init() {
      rand = new Random();

      opaqueID = glGenLists(1);

      tiles = new byte[(int) lPosition.x][(int) lPosition.y][(int) lPosition.z];

      genRandomWorld();
      rebuild();
   }
    public void rebuild() {
      glNewList(opaqueID, GL_COMPILE);
      glBegin(GL_QUADS);
      for (int x = (int) sPosition.x; x < (int) lPosition.x; x++) {
         for (int y = (int) sPosition.y; y < (int) lPosition.y; y++) {
            for (int z = (int) sPosition.z; z < (int) lPosition.z; z++) {
               if (checkCubeHidden(x, y, z)) {
                  // check if tiles hidden. if not, add vertices to
                 // display list
                 if (type != 0) {
                     Tile.getTile(tiles[x][y][z]).getVertices(x, y, z, 1, spritesheet.getTextureCoordsX(tiles[x][y][z]), spritesheet.getTextureCoordsY(tiles[x][y][z]));
                  } else {
                     Tile.getTile(tiles[x][y][z]).getVertices(x, y, z, 1);
                  }
               }
            }
         }
      }
      glEnd();
      glEndList();
      spritesheet.bind();
   }

I should note that in my display list version, I only add in the visible cubes. So, that may be an unfair advantage, but it should not bring the VBO version down to that FPS with just 27 chunks versus 500 chunks for the display list version.
I render like this:

1  
2  
3  
4  
5  
6  
7  
    public void render() {
      if (tiles.length != -1) {
         glEnable(GL_BLEND);
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
         glCallList(opaqueID);
      }
   }

So, after all of that code, I really still wonder why my VBO version is just so darn slow? I do have a one dimensional list of chunks in my display list version for when I'm calling them to render, and a 3 dimensional one in my VBO version, but I think the JVM pretty much eliminates any lag with the extra dimensions. So, what am I doing wrong?

Edit: I just turned off individual block culling in my display list version and it still ran just as fast, albeit the start up time was slower, as expected.
9  Game Development / Newbie & Debugging Questions / Re: Collision response on two axis on: 2013-11-24 19:14:12
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  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
public void update(Mob mob) {
      if (player) {
         if (pos.getX() < 0)
            pos.setX(0);
         if (pos.getX() > 1216)
            pos.setX(1216);
         if (pos.getY() < 0)
            pos.setY(0);
         if (pos.getY() > 656)
            pos.setY(656);
         if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
            fpos.setPosition(fpos.getX(), fpos.getY() + MOVESPEED);
            if (!Collision.isColliding(box, mob.box)) {
               pos.setPosition(fpos.getX(), fpos.getY());
            } else {
               pos.setPosition(pos.getX(), pos.getY());
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
            fpos.setPosition(fpos.getX(), fpos.getY() - MOVESPEED);
            if (!Collision.isColliding(box, mob.box)) {
               pos.setPosition(fpos.getX(), fpos.getY());
            } else {
               pos.setPosition(pos.getX(), pos.getY());
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
            fpos.setPosition(fpos.getX() + MOVESPEED, fpos.getY());
            if (!Collision.isColliding(box, mob.box)) {
               pos.setPosition(fpos.getX(), fpos.getY());
            } else {
               pos.setPosition(pos.getX(), pos.getY());
            }
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
            fpos.setPosition(fpos.getX() - MOVESPEED, fpos.getY());
            if (!Collision.isColliding(box, mob.box)) {
               pos.setPosition(fpos.getX(), fpos.getY());
            } else {
               pos.setPosition(pos.getX(), pos.getY());
            }
         }
      }
   }

So, this code kind of works, except the player can pass through objects if he just sits there and keeps pressing the key down. For instance, say I'm colliding on the left. If I keep pressing 'A', the player will eventually move through the tile. I test the AABB with the fpos position, which is supposed to be the future position.
10  Game Development / Newbie & Debugging Questions / Re: Collision response on two axis on: 2013-11-24 19:08:46
No, I'm OP, I don't know why opiop was responding like that  Huh

I'm not that great of a coder, and this is really just a simple project, so I'm not going to implement your method because it's just far too advanced, and I don't need perfect collision, just something that works easy.
11  Game Development / Newbie & Debugging Questions / Re: Collision response on two axis on: 2013-11-24 18:39:25
Wait... is simple collision detection like what I'm after seriously that hard to do? I've seen other people's code where they only check one axis as a time, and it didn't require any sort of slopes or anything. Not to say your way is bad, but it seems overly complicated for the simple thing I'm trying to do. I think what I should be doing is seeing if the "future" position is going to collide with another entity, and if so, don't move in that direction. This means I'll never be actually colliding with the tile, and I'll be free to move on the other axis.
12  Game Development / Newbie & Debugging Questions / Collision response on two axis on: 2013-11-24 15:46:25
Well, I implemented a AABB system of collision detection, and it works fine, but now I need to make it so that the player can move after he collides with an object. Right now, when the player collides with an object, he just gets stuck and you can't move him. I realize why this is happening, I just don't know how to solve it because of the algorithm I'm using. Here's the player movement 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  
public void update(Mob mob) {
      if (player) {
         if (pos.getX() < 0)
            pos.setX(0);
         if (pos.getX() > 1216)
            pos.setX(1216);
         if (pos.getY() < 0)
            pos.setY(0);
         if (pos.getY() > 656)
            pos.setY(656);
         if (Keyboard.isKeyDown(Keyboard.KEY_W) && !Collision.isColliding(box, mob.box)) {
            pos.setPosition(pos.getX(), pos.getY() + MOVESPEED);
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_S) && !Collision.isColliding(box, mob.box)) {
            pos.setPosition(pos.getX(), pos.getY() - MOVESPEED);
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_D) && !Collision.isColliding(box, mob.box)) {
            pos.setPosition(pos.getX() + MOVESPEED, pos.getY());
         }
         if (Keyboard.isKeyDown(Keyboard.KEY_A) && !Collision.isColliding(box, mob.box)) {
            pos.setPosition(pos.getX() - MOVESPEED, pos.getY());
         }
      }
   }

And then the collision detection algorithm:
1  
2  
3  
4  
5  
6  
public static boolean isColliding(AABB a, AABB b) {
      if (a.x + a.size < b.x || b.x + b.size < a.x || a.y + a.size < b.y || b.y + b.size < a.y) {
         return false;
      }
      return true;
   }

AABB is just a class that holds the position and size of the mob, nothing special. But, as you can see my code only checks if both axis are colliding or not, but I need to test it its colliding on only one at a time. How can I do this?
13  Games Center / WIP games, tools & toy projects / Re: Level Editor Tool for 2D games on: 2013-11-14 01:48:20
I like it! Put a little more work into it, and it would be great for everyone to use! Smiley

@opiop65 I agree, why use C++ codes?
14  Java Game APIs & Engines / OpenGL Development / GLSL Undeclared Identifier on: 2013-11-10 15:40:55
I've been working on vertex lighting recently with shaders (obviously), but I've ran into an issue. Now, forgive me if there is an apparent fix, but I barely know GLSL. So, I'll show you how I set up my shaders:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
   private void setupShaders(){
      Shader geomShaders = new Shader("res/landscape.vert", "res/landscape.frag");
      geomProgram = new ShaderProgram(geomShaders.getvShader(), geomShaders.getfShader());
     
      glUniform1i(glGetUniformLocation(geomProgram.getProgram(), "lookup"), 0);
     
      Shader lightShaders = new Shader("res/lighting.vert", "res/lighting.frag");
      lightProgram = new ShaderProgram(lightShaders.getvShader(), lightShaders.getfShader());
     
   }

ShaderProgram and Shader are just some shader parsing utilities I created, they aren't the problem because I've already used them before lots. Here is my vertex shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
varying vec3 color;

void main(){

   vec3 vertexPos = (gl_ModelViewMatrix * gl_Vertex).xyz;
   vec3 lightDir = normalize(gl_LightSource[0].position.xyz - vertexPos);
   vec3 surfaceNormal = (gl_NormalMatrix * gl_Normal).xyz;

   float diffuseIntensity = max(0, dot(surfaceNormal, lightDir));
   color.rgb = diffuseIntensity * gl_FrontMaterial.diffuse.rgb;
   color += gl_LightModel.ambient.rgb;  
   
   vec3 reflectDir = normalize(reflect(-lightDir, surfaceNormal));
   float specular = max(0.0, dot(surfaceNormal, reflectDir));
   if(diffuseIntesity != 0){
      float fspecular = pow(specular, gl_FrontMaterial.shininess);
      color.rgb += vec3(fspecular, fspecular, fspecular);
   }

   gl_Position = gl_ModelViewMatrix * gl_Vertex;  
   
}

And my fragment shader:
1  
2  
3  
4  
5  
varying vec3 color;

void main(){
   gl_FragColor = vec4(varyingColor, 1);
}

And this is how I set up the lights:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private void setupLighting(){
      glShadeModel(GL_SMOOTH);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glLightModel(GL_LIGHT_MODEL_AMBIENT, BufferTools.asFlippedFloatBuffer(new float[]{0.05f, 0.05f, 0.05f, 1f}));
      glLight(GL_LIGHT0, GL_POSITION, BufferTools.asFlippedFloatBuffer(new float[]{-70, -15, -30, 1}));
      glEnable(GL_COLOR_MATERIAL);
      glColorMaterial(GL_FRONT, GL_DIFFUSE);
      glColor3f(0.4f, 0.27f, 0.17f);
   }

When I run this, I get the following errors;
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
Vertex shader failed to compile with the following errors:
ERROR: 0:15: error(#143) Undeclared identifier: diffuseIntesity
ERROR: error(#273) 1 compilation errors.  No code generated


Error compiling vertex shader at Shader.java location: res/lighting.vert
Fragment shader failed to compile with the following errors:
ERROR: 0:4: error(#143) Undeclared identifier: varyingColor
ERROR: 0:4: error(#174) Not enough data provided for construction constructor
ERROR: error(#273) 2 compilation errors.  No code generated


Error compiling fragment shader at Shader.java location: res/lighting.frag
Vertex shader failed to compile with the following errors:
ERROR: 0:15: error(#143) Undeclared identifier: diffuseIntesity
ERROR: error(#273) 1 compilation errors.  No code generated


Error compiling vertex shader at Shader.java location: res/lighting.vert
Fragment shader failed to compile with the following errors:
ERROR: 0:4: error(#143) Undeclared identifier: varyingColor
ERROR: 0:4: error(#174) Not enough data provided for construction constructor
ERROR: error(#273) 2 compilation errors.  No code generated


What does it mean undeclared identifier? I've declared them all, and I've set the color using glColor3f in the setuplighting method. I quite honestly have no idea how to solve this, so I'm hoping someone can help!
15  Game Development / Newbie & Debugging Questions / Re: Centering an object in the screen on: 2013-11-08 03:33:07
So I rewrote all my stuff, and I figured out most of the problem. Now, however, the player slowly slides out of the center of the screen. Here's my new camera class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
private Player p;
   private Vector2f pos;
   
   public Camera(Player p){
      this.p = p;
      this.pos = new Vector2f(0, 0);
   }
   
   public void update(){
      glTranslatef(-p.getPos().x + pos.x, -p.getPos().y + pos.y, 0);
      if(Keyboard.isKeyDown(Keyboard.KEY_D)){
         p.getPos().setX(p.getPos().x + 1);
         pos.setX(pos.x - 1 * 64);
      }
      if(Keyboard.isKeyDown(Keyboard.KEY_A)){
         p.getPos().setX(p.getPos().x - 1);
         pos.setX(pos.x + 1 * 64);
      }
   }

That's really the only relevant code, nothing else has changed. I'm so close, I just need to fix this last part! Any help?
16  Game Development / Newbie & Debugging Questions / Re: Centering an object in the screen on: 2013-11-07 12:37:18
Currently if you move the player 100px, you're moving the camera 50px.
1  
+ -p.getPos().x / 2

Thus why you need to update the player based on the camera position.

Edit:
Create a vector for the camera position, and do this:
1  
2  
3  
public void move(float x, float y){
      this.getPos().set(this.getPos().x + x, this.getPos().y + y).sub(camera.getPos);
}

Ok, so how should I move the camera now? Should I not have the input change the players position? I did it like you said, and the player doesn't move anymore because I don't know what I should be putting in my translate method of my camera anymore.
17  Game Development / Newbie & Debugging Questions / Re: Centering an object in the screen on: 2013-11-07 12:27:35
Because that's translating it to the center of the screen, I believe. Dividing by two to get to the center of the screen, then dividing by 64 which is the tile size to get to the center of the map. Or something like that.
18  Game Development / Newbie & Debugging Questions / Re: Centering an object in the screen on: 2013-11-07 12:25:14
Well, I think I should give up Sad Still not working... I have absolutely no idea what the problem could be! I mean, here's how I create the player:
1  
2  
3  
public Player() {
      super(new Vector2f(Main.WIDTH / 2 / 64, Main.HEIGHT / 2 / 64), Type.PLAYER);
   }

And then the update method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void update(){
      MOVESPEED += .0001f;
      if(Keyboard.isKeyDown(Keyboard.KEY_D)){
         this.move(MOVESPEED, 0);
      }
     
      if(Keyboard.isKeyDown(Keyboard.KEY_A)){
         this.move(-MOVESPEED, 0);
      }
   }

Its literally the most simple thing ever! Would the moveSpeed variable have anything to do with what's wrong?
19  Game Development / Newbie & Debugging Questions / Re: Centering an object in the screen on: 2013-11-07 12:18:27
@Dxu1994 That still didn't work Sad Quite honestly, I think the camera moved slower! And the player wasn't centered in the window anymore, he was up and to the left! I should note that my origin is in the top left, no the bottom because I am using slick-util to render my text.

@Troubleshoots, I would do that, but my camera doesn't have a vector. I literally just use my player's vector for the camera.
20  Game Development / Newbie & Debugging Questions / Re: Centering an object in the screen on: 2013-11-07 12:08:37
Nope, that didn't work :/ Here, I'll show you how I update the camera, maybe that'll help:
1  
2  
3  
4  
5  
6  
7  
8  
public void render() {
      glLoadIdentity();
      glPushMatrix();
      cam.update(mobManager.getP());
      tileManager.render();
      mobManager.render();
      glPopMatrix();
   }

That's my method for rendering all my stuff. cam.update is there because it calls glTranslate, and it needs to translate the geometry in the tileManager and mobManager. I added the glLoadIdentity call like you said.

Edit: The weird thing is, is that I print out the camera and player position vectors, and they're the same. Yet, the "camera" scrolls slower.
21  Game Development / Newbie & Debugging Questions / Re: Centering an object in the screen on: 2013-11-07 12:04:20
I already do, that shouldn't matter.
22  Games Center / WIP games, tools & toy projects / Re: NEngine - Nishu Game Engine on: 2013-11-07 04:25:29
Whoa, this is cool! Did you make balanced energy using this library? Your game looks cool!
23  Discussions / Miscellaneous Topics / Re: Speed Code Tutorials? on: 2013-11-07 04:24:17
I would say do something other than games. Create a networking program or a image editor. I think too many people focus on games when they first start programming, and not enough on other stuff.
24  Games Center / Contests / Re: Game contests thread on: 2013-11-07 04:21:25
My favorite contest is Ludum Dare, personally Smiley I haven't actually managed to take part in one, but I plan on doing it soon after I learn a little more!
25  Games Center / WIP games, tools & toy projects / Re: Horde 8-Bit Waves Of Death! - [WIP UPDATED JUNE 26 2013] on: 2013-11-07 04:19:49
Whoa, this is really cool I like the demo! Can't wait for you to add weapons and stuff, maybe I'll try out your library!
26  Games Center / WIP games, tools & toy projects / Re: Renoria - Java MMORPG on: 2013-11-07 04:19:05
I personally like the new HUD better!
27  Games Center / WIP games, tools & toy projects / Re: Harvest Online on: 2013-11-07 04:18:31
This game looks really cool! I like your textures, they look very nice Smiley
28  Game Development / Newbie & Debugging Questions / Centering an object in the screen on: 2013-11-07 03:58:39
Hey guys! First post here Smiley

So I have a little problem. I have a 2D platformer, and I want to keep the player in the center of the screen. Well, that should be easy right? Just set the camera position to half the size of the screen plus half the size of the player. So, I did that. But the camera actually scrolls slower than the player, so that the player can actually escape the screen. I'll show you some code now:

The camera class:
1  
2  
3  
4  
5  
6  
public Camera(){
   }
   
   public void update(Player p){
      glTranslatef(Main.WIDTH / 64 / 2 + -p.getPos().x / 2, Main.HEIGHT / 64 / 2 - -p.getPos().y / 2, 0);
   }

Here's how I update my player's position:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public void update(){
      if(Keyboard.isKeyDown(Keyboard.KEY_D)){
         this.move(MOVESPEED, 0);
      }
     
      if(Keyboard.isKeyDown(Keyboard.KEY_A)){
         this.move(-MOVESPEED, 0);
      }
   }

Which then in turn calls the move method:
1  
2  
3  
public void move(float x, float y){
      this.getPos().set(this.getPos().x + x, this.getPos().y + y);
   }

I create the player and initialize it's position to half the screen, which works fine. But when I start scrolling, the player isn't centered in the screen! How can I fix this?
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

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

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

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

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
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

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!