Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2] 3 4 ... 6
  ignore  |  Print  
  LWJGL - Voxel Engine  (Read 22521 times)
0 Members and 1 Guest are viewing this topic.
Offline steg90

Senior Devvie


Medals: 1



« Reply #30 - Posted 2013-04-08 18:45:57 »

Good about the culling, although depending on how many faces you are checking, it sometimes can slow the renderer down unless you partition your chunks into some form of data structure such as octree.

As for stop player moving through a block, I do something like:

1  
2  
3  
4  
5  
6  
7  
8  
if (Keyboard.isKeyDown(Keyboard.KEY_W))// move forwards
{
   
   if(!isCollision(1))
   {
      camera.walkBackwards(movementSpeed * dt);
   }
}


Where isCollision returns true or false, if false, no collision, so player here can move forwards. 

PS - did you create a function for the trees?

Offline Pauler
« Reply #31 - Posted 2013-04-08 18:50:44 »

Good about the culling, although depending on how many faces you are checking, it sometimes can slow the renderer down unless you partition your chunks into some form of data structure such as octree.

As for stop player moving through a block, I do something like:

1  
2  
3  
4  
5  
6  
7  
8  
if (Keyboard.isKeyDown(Keyboard.KEY_W))// move forwards
{
   
   if(!isCollision(1))
   {
      camera.walkBackwards(movementSpeed * dt);
   }
}


Where isCollision returns true or false, if false, no collision, so player here can move forwards. 

PS - did you create a function for the trees?



I randomly place blocks at the shape of a tree at the top of each chunk.
Offline steg90

Senior Devvie


Medals: 1



« Reply #32 - Posted 2013-04-09 15:50:34 »

Going to be looking at putting trees in myself.  Did you create a method that would not put any blocks on top of a tree?

Thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Agro
« Reply #33 - Posted 2013-04-09 15:54:53 »

Going to be looking at putting trees in myself.  Did you create a method that would not put any blocks on top of a tree?

Thanks

I don't think you'd need that if you're only using 2D perlin/simplex noise. If you plant the trees at random locations, its impossible for a block to be above it. Just make sure you're planting the trees on grass Smiley

Offline steg90

Senior Devvie


Medals: 1



« Reply #34 - Posted 2013-04-09 16:08:29 »

Hi @Argo,

So, I'm using the simplex noise class, this is how I generate a simple 16^3 chunk with it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   public int SetupLandscape()
   {
      Random rr = new Random();
      for (int x = 0; x < CHUNK_SIZE; x++) {
         for (int z = 0; z < CHUNK_SIZE; z++) {      
            int height = (int) (SimplexNoise.noise(x / 16f, z / 16f) *16f);
            if(height<1) height=1;
            for(int y = 0; y <= height; y++) // set these blocks on
            {
               Blocks[x][y][z].SetActive(true);
               if(rr.nextBoolean())
                  Blocks[x][y][z].SetType(BlockType.BlockType_Grass);
               else
                  Blocks[x][y][z].SetType(BlockType.BlockType_Dirt);
                 
            }
         }
      }
      return this.rebuildChunk();
   }


So, you are saying when I have done that I can place a tree anywhere as long as it is grass? I think I need to call the above, then create another method which would plant a tree at random locations within this chunk as long as the height of the block we are putting it on is not going to allow our tree to go out of the bounds of the chunk.  i.e. if our tree is going to be 12 blocks in height, we need to put it on a block that is less than 4 on the Y-axis - this make sense or am I talking garbage?!

Thanks
Offline Pauler
« Reply #35 - Posted 2013-04-09 16:55:40 »

Going to be looking at putting trees in myself.  Did you create a method that would not put any blocks on top of a tree?

Thanks
I don't use any advanced methods to generate my world. I just generate some random ints for each block type, and then I call my my generateTree function which generates 2 number from 1 to 6 (one for x, and on for z axis) and I just spawn a tree there.



<a href="http://www.youtube.com/v/0UCS_vIceRI?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/0UCS_vIceRI?version=3&amp;hl=en_US&amp;start=</a>

  • Added Collision Detection.
  • Added Gravity, and jumping.
  • Enabled vsync to 60.
  • I moved to arrays from lists.
Offline steg90

Senior Devvie


Medals: 1



« Reply #36 - Posted 2013-04-09 17:00:16 »

Looking good.  Did you use voxel space for collision or AABB?

I basically just got player position and converted to voxel space and indexed into chunk/block array.

Take it your trees are all the same height then?  How do you manage placing the tree on top of a block so it doesn't appear below one?
Offline Pauler
« Reply #37 - Posted 2013-04-09 17:03:55 »

I used AABB.

After chunk generation I save the top y value so I can use it to stuff like spawning trees.
Online Agro
« Reply #38 - Posted 2013-04-09 17:04:21 »

Steg, how I would do it is first generate the whole entire world. Then pick out random X and Z values that have space in your world, get the highest block in that place, and spawn a tree on that block above it. That's how I would do it atm. You can also check the distance from other trees to make sure they aren't too close together.

Also, if you want, you can just loop through your X and Z width and spawn trees every 5x5 or so blocks.

Offline steg90

Senior Devvie


Medals: 1



« Reply #39 - Posted 2013-04-09 17:08:30 »

@Argo - sounds good, any good advice on finding space, guess checking blocks above?

@Pauler - With your AABB, do you check every block?

Thanks guys
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Pauler
« Reply #40 - Posted 2013-04-09 17:10:09 »

@Argo - sounds good, any good advice on finding space, guess checking blocks above?

@Pauler - With your AABB, do you check every block?

Thanks guys
I save to a list the chunk I am colliding with, and I do all the checks there.
Offline steg90

Senior Devvie


Medals: 1



« Reply #41 - Posted 2013-04-09 17:11:41 »

Chunk you are colliding with or block?

Online Agro
« Reply #42 - Posted 2013-04-09 17:16:45 »

The easiest solution right now is to go through those coordinates from Y0, to Yn, and if there is a block there, set that to the highest block.

Offline steg90

Senior Devvie


Medals: 1



« Reply #43 - Posted 2013-04-09 17:18:36 »

Thanks, will take a look at that.

Just looking at my collision again, may switch to AABB, do you have any experience of this?
Online Agro
« Reply #44 - Posted 2013-04-09 17:21:34 »

You SHOULD switch to AABB. Its one of the easiest algorithms to use because the the blocks are aligned to the axises, and spheres are round.

http://www.java-gaming.org/topics/basic-collision-detection/27326/view.html

^ take a good look at that. it explains AABB-AABB, AABB-Sphere, and Sphere-Sphere

Offline Pauler
« Reply #45 - Posted 2013-04-09 17:22:44 »

I used this

http://www.java-gaming.org/topics/fast-simple-aabb-collision-detection/28059/view.html
Offline steg90

Senior Devvie


Medals: 1



« Reply #46 - Posted 2013-04-09 17:24:35 »

Thanks for that,

I've already put together a simple AABB class, this look ok?
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  
public class AABB {

   Vector3f vecMax = new Vector3f(0,0,0);
   Vector3f vecMin = new Vector3f(0,0,0);
   
   public AABB(Vector3f bottom, Vector3f top)
   {
      vecMax.x = top.x; vecMax.y = top.y; vecMax.z = top.z;
      vecMin.x = bottom.x; vecMin.y = bottom.y; vecMin.z = bottom.z;
     
   }
   public AABB(float tx, float ty, float tz, float bx, float by, float bz)
   {
      vecMax.x = tx; vecMax.y = ty; vecMax.z = tz;
      vecMin.x = bx; vecMin.y = by; vecMin.z = bz;
     
   }
   public static boolean detectCollision(AABB tBox1, AABB tBox2)
   {
//      System.out.println("minx: " + tBox1.vecMin.x + " obj max:" + tBox2.vecMax.x);
//      System.out.println("miny: " + tBox1.vecMin.y + " obj max:" + tBox2.vecMax.y);
//      System.out.println("minz: " + tBox1.vecMin.z + " obj max:" + tBox2.vecMax.z);
     
      if(tBox1.vecMin.x > tBox2.vecMax.x) return false;
      if(tBox1.vecMin.y > tBox2.vecMax.y) return false;
      if(tBox1.vecMin.z > tBox2.vecMax.z) return false;
      if(tBox1.vecMax.x < tBox2.vecMin.x) return false;
      if(tBox1.vecMax.y < tBox2.vecMin.y) return false;
      if(tBox1.vecMax.z < tBox2.vecMin.z) return false;
     
      return true;
     
   }
   
}


My issue is, how many blocks do you check for collision with?  Somebody mentioned the blocks around the player which still means using voxel space and also AABB.
Offline steg90

Senior Devvie


Medals: 1



« Reply #47 - Posted 2013-04-09 17:25:28 »

@Pauler - take it you modified it to be 3d?

Thanks
Offline Pauler
« Reply #48 - Posted 2013-04-09 17:26:04 »

@Pauler - take it you modified it to be 3d?

Thanks
Just added another check for z axis and it worked more than fine.
Online Agro
« Reply #49 - Posted 2013-04-09 17:26:13 »

All you need to do is run AABB-Sphere(if you are using a sphere for the player) against the block above, below, to the right, to the left.

Offline steg90

Senior Devvie


Medals: 1



« Reply #50 - Posted 2013-04-09 17:27:08 »

Ok - so I guess you still need to convert from camera space (position of player) to voxel space then?
Online Agro
« Reply #51 - Posted 2013-04-09 17:28:00 »

Yes, and make sure you are using the position of the voxel you are contained in, not the position of the voxel you are standing on.

Offline Pauler
« Reply #52 - Posted 2013-04-09 17:28:21 »

Personally, I didn't.
Online Agro
« Reply #53 - Posted 2013-04-09 17:29:45 »

Wait, it depends on if you store your blocks with world position(where in the world) or block position.

Offline steg90

Senior Devvie


Medals: 1



« Reply #54 - Posted 2013-04-09 17:40:57 »

Blocks are translated to world positions, was meaning, as they are held in a 3d array, we can offset into this to get the block.

I render my blocks in each chunk with the following:

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  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
private int createChunkList() {

      tx = 0; ty = 0; tz = 0;
      int iHidden = 0;
      int iBlockCount = 0;
      boolean bLeft = !false;
      boolean bRight = !false;
      boolean bAbove = !false;
      boolean bBelow = !false;
      boolean bFront = !false;
      boolean bBack = !false;

      boolean bDefault = true;
     
       glEnable(GL11.GL_CULL_FACE);
       GL11.glEnable(GL_TEXTURE_2D);

       // some how need a way of getting the blocks texture bound...
      GL11.glBindTexture(GL_TEXTURE_2D, TextureManager.getInstance().getId());

      glBegin(GL11.GL_QUADS);

      for (int x = 0; x < Chunk.CHUNK_SIZE; x++) {
         for (int y = 0; y < Chunk.CHUNK_SIZE; y++) {
            for (int z = 0; z < Chunk.CHUNK_SIZE; z++) {
               Block block = GetBlock(x,y,z);
                texture = TextureManager.getInstance().getTexture(block.getType());
           
               if(!GetBlock(x,y,z).IsActive())  //
               {
                  continue;
               }
               bLeft = bDefault;
               if(x > 0)
               {
                  bLeft = Blocks[x-1][y][z].IsActive();
               } else bLeft = false;
               bRight = bDefault;
               if(x < Chunk.CHUNK_SIZE - 1)
               {
                  bRight = Blocks[x+1][y][z].IsActive();
               } else bRight = false;
               bAbove = bDefault;
               if(y > 0)
               {
                  bAbove = Blocks[x][y-1][z].IsActive();
               } else bAbove = false;
               bBelow = bDefault;
               if(y < Chunk.CHUNK_SIZE - 1)
               {
                  bBelow = Blocks[x][y+1][z].IsActive();
               } else bBelow = false;
               bFront = bDefault;
               if(z > 0)
               {
                  bFront = Blocks[x][y][z-1].IsActive();
               } else bFront = false;
               bBack = bDefault;
               if(z < Chunk.CHUNK_SIZE - 1)
               {
                  bBack = Blocks[x][y][z+1].IsActive();
               } else bBack = false;
               
               boolean bResult = bLeft & bRight & bAbove &
                               bBelow & bFront & bBack;
               
               if(!bResult) // Block is not hidden by neighbouring blocks
               {
                  tx = ((xOffset * Chunk.CHUNK_SIZE) ) + (x);// << 1);
                       ty = ((yOffset * Chunk.CHUNK_SIZE) ) + (y);// << 1);
                       tz = ((zOffset * Chunk.CHUNK_SIZE) ) + (z);// << 1);
                 
                  renderBlock(GetBlock(x,y,z).getType());
             
                  iBlockCount++;   // total of blocks that can be seen
               }
               else
                  iHidden++;   // amount of blocks that are surrounded              
            }
         }
      }
      glEnd();
       glDisable(GL11.GL_CULL_FACE);

      return iBlockCount;

   }

private void renderBlock(Block.BlockType type) {
     
      GL11.glTexCoord2f(texture.u * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f(-0.5f, -0.5f, 0.5f);   // Bottom Left Of The Texture and Quad
      GL11.glTexCoord2f((1+texture.u) * 0.0625f, (texture.v+1)  * 0.0625f);
      off_glVertex3f( 0.5f, -0.5f, 0.5f);   // Bottom Right Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, (0+texture.v) * 0.0625f);
      off_glVertex3f( 0.5f, 0.5f, 0.5f);   // Top Right Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 0) * 0.0625f, (texture.v + 0) * 0.0625f);
      off_glVertex3f(-0.5f, 0.5f, 0.5f);   // Top Left Of The Texture and Quad
     
      // Back Face
      GL11.glTexCoord2f(texture.u * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f(-0.5f, -0.5f, -0.5f);   // Bottom Right Of The Texture and Quad
      GL11.glTexCoord2f(texture.u * 0.0625f, texture.v  * 0.0625f);
      off_glVertex3f(-0.5f, 0.5f, -0.5f);   // Top Right Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, texture.v * 0.0625f);
      off_glVertex3f( 0.5f, 0.5f, -0.5f);   // Top Left Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f( 0.5f, -0.5f, -0.5f);   // Bottom Left Of The Texture and Quad
     
     
      // Top Face
      GL11.glTexCoord2f(texture.ut * 0.0625f, (texture.vt + 1) * 0.0625f);
      off_glVertex3f(-0.5f, 0.5f, -0.5f);   // Top Left Of The Texture and Quad
      GL11.glTexCoord2f(texture.ut * 0.0625f, texture.vt  * 0.0625f);
      off_glVertex3f(-0.5f, 0.5f, 0.5f);   // Bottom Left Of The Texture and Quad
      GL11.glTexCoord2f((texture.ut + 1) * 0.0625f, texture.vt * 0.0625f);
      off_glVertex3f( 0.5f, 0.5f, 0.5f);   // Bottom Right Of The Texture and Quad
      GL11.glTexCoord2f((texture.ut + 1) * 0.0625f, (texture.vt + 1) * 0.0625f);
      off_glVertex3f( 0.5f, 0.5f, -0.5f);   // Top Right Of The Texture and Quad

      // Bottom Face
      GL11.glTexCoord2f(texture.u * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f(-0.5f, -0.5f, -0.5f);   // Top Right Of The Texture and Quad
      GL11.glTexCoord2f(texture.u * 0.0625f, texture.v  * 0.0625f);
      off_glVertex3f( 0.5f, -0.5f, -0.5f);   // Top Left Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, texture.v * 0.0625f);
      off_glVertex3f( 0.5f, -0.5f, 0.5f);   // Bottom Left Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f(-0.5f, -0.5f, 0.5f);   // Bottom Right Of The Texture and Quad
     
      // Right face
      GL11.glTexCoord2f(texture.u * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f( 0.5f, -0.5f, -0.5f);   // Bottom Right Of The Texture and Quad
      GL11.glTexCoord2f(texture.u * 0.0625f, texture.v  * 0.0625f);
      off_glVertex3f( 0.5f, 0.5f, -0.5f);   // Top Right Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, texture.v * 0.0625f);
      off_glVertex3f( 0.5f, 0.5f, 0.5f);   // Top Left Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f( 0.5f, -0.5f, 0.5f);   // Bottom Left Of The Texture and Quad
   
      // Left Face
      GL11.glTexCoord2f(texture.u * 0.0625f, (texture.v + 1) * 0.0625f);
      off_glVertex3f(-0.5f, -0.5f, -0.5f);   // Bottom Left Of The Texture and Quad
      GL11.glTexCoord2f((1+texture.u) * 0.0625f, (1+texture.v)  * 0.0625f);
      off_glVertex3f(-0.5f, -0.5f, 0.5f);   // Bottom Right Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 1) * 0.0625f, (0+texture.v) * 0.0625f);
      off_glVertex3f(-0.5f, 0.5f, 0.5f);   // Top Right Of The Texture and Quad
      GL11.glTexCoord2f((texture.u + 0) * 0.0625f, (texture.v + 0) * 0.0625f);
      off_glVertex3f(-0.5f, 0.5f, -0.5f);   // Top Left Of The Texture and Quad      
        glColor3f(1,1,1);
   }


private void off_glVertex3f(float x,float y,float z){
            glVertex3f(tx+x,ty+y,tz+z);
   }


So, without using voxel space and using AABB instead, how could I do the collision checks?

Thanks
Offline steg90

Senior Devvie


Medals: 1



« Reply #55 - Posted 2013-04-09 18:32:58 »

Think will need to look at the AABB because the collision isn't great just using voxel space i.e.is there a block in front, back etc...

With AABB, surely you still need to know details of the block you are testing against, I mean, is the block active or not - so, you still need to pull out the blocks data from your block array?!

My block picking and removing needs to be changed to, looks like using Bresenham's line algorithm or a voxel traversal algorithm.  The latter, here is a paper on it, what does anybody think? http://www.cse.yorku.ca/~amana/research/grid.pdf

Here is a link of my project with the voxel space collision checks:


https://www.youtube.com/watch?v=QXLRP_R9Uak


Offline HeroesGraveDev

JGO Kernel


Medals: 310
Projects: 11
Exp: 3 years


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


« Reply #56 - Posted 2013-04-10 05:27:55 »

Collision detection:

For each of the eight corners of the player (+-x, +-y, +-z):

Floor the position.
Get the voxel for that position.
If null or not solid then collision is false.

If collision is false for all points, move. Otherwise don't move.

Depending on the size of the player (if it's bigger then one block), you may need to add more points along the edges/faces.

Offline steg90

Senior Devvie


Medals: 1



« Reply #57 - Posted 2013-04-10 08:14:52 »

@HeroesGraveDev

Thanks for the input.  Could you not just take the players near bottom left and far top right corners to define its AABB?

So:

1. Get AABB for player
2. Get voxel at cameras/player position
3. If not air
         i. Get AABB for block
         ii. If player AABB overlaps block AABB - collision so don't move

Does that sound correct?

Thanks         
Offline HeroesGraveDev

JGO Kernel


Medals: 310
Projects: 11
Exp: 3 years


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


« Reply #58 - Posted 2013-04-10 08:25:01 »

The method I explained doesn't use AABB.

Online Agro
« Reply #59 - Posted 2013-04-10 08:39:53 »

I would rather use a sphere for the player instead of an AABB. IT needs more calculations, but it makes more sense to me :/

Pages: 1 [2] 3 4 ... 6
  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.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (59 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!