Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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 5 ... 7
  ignore  |  Print  
  Voxel - a start  (Read 18153 times)
0 Members and 1 Guest are viewing this topic.
Offline greenOwl

Junior Member


Medals: 1


from Germany


« Reply #60 - Posted 2013-03-16 20:53:21 »

I don't know how much you know about (vertex & fragment) shader but you might want to look into setting positions via a shader uniform rather than modifying the vertex data itself (which is much more expensive). Since you're already useing OpenGL 3.1+ (VBOs) you might as well use shaders as well - if you are not using them already.

The loop would look something like that:
1. use shader program (glUseProgram) and upload the uniform offset (glUseUniform)
2. render the vbo
3. get the next vbo and continue

Of course this only works as described if all vertices share the same offset which should be the case with chunks.

elfeck
Offline quew8

JGO Coder


Medals: 23



« Reply #61 - Posted 2013-03-17 09:52:21 »

Don't you mean OpenGL 2.1? Pretty sure vbos and shaders are standard in that.
But I agree completely, shaders are the way forward. I used to try and find "smart" ways of doing things without using shaders but it's just pointless. It's inefficient for you and your computer. Shaders are just simple. As soon as I read the first tutorial, I realized that and cursed for all the time I'd wasted avoiding them.
Offline steg90

Senior Member


Medals: 1



« Reply #62 - Posted 2013-03-17 12:39:07 »

Hi,

My gfx card in my macbook pro supports up to 2.1.

I'm familiar with shaders in DirectX.

Just would like to do this without shaders first, so was asking the question, how do you store all the geometry i.e.chunk in one display list and then render that?

Thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline steg90

Senior Member


Medals: 1



« Reply #63 - Posted 2013-03-17 13:14:54 »

Hi,

Ok, I've done the following to put a whole chunk into a display list:

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  
// Create a chunk and store in display list
private void createChunkVoxel() {
      chunkList = glGenLists(1);  // this hard coded value will need changing as some point
     glNewList(chunkList, GL_COMPILE);
      createDisplayList();
      glEndList();
   }

private void createDisplayList() {

     
         Chunk c = ChunkManager.getInsance().GetChunk(0);
   
         for (int x = 0; x < c.CHUNK_SIZE; x++) {
            for (int y = 0; y < c.CHUNK_SIZE; y++) {
               for (int z = 0; z < c.CHUNK_SIZE; z++) {
                  GL11.glPushMatrix();
                 
                  glTranslatef(x,y,z);
                                                // Only create block if it is active
                                               if(c.GetBlock(x,y,z).isActive())
                      renderCube();
                  GL11.glPopMatrix();
               }
            }
         }
     
   }

private void renderCube() {

      //GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
     glBegin(GL11.GL_QUADS);
         glColor3f(0.0f, 1.0f, 0.0f);
         glVertex3f(1.0f, 1.0f, -1.0f);
         glVertex3f(-1.0f, 1.0f, -1.0f);
         glVertex3f(-1.0f, 1.0f, 1.0f);
         glVertex3f(1.0f, 1.0f, 1.0f);
         
         glColor3f(0.0f, 0.4f, 0.0f);
         glVertex3f(1.0f, -1.0f, 1.0f);
         glVertex3f(-1.0f, -1.0f, 1.0f);
         glVertex3f(-1.0f, -1.0f, -1.0f);
         glVertex3f(1.0f, -1.0f, -1.0f);
         
         glColor3f(0.0f, 0.8f, 0.0f);
         glVertex3f(1.0f, 1.0f, 1.0f);
         glVertex3f(-1.0f, 1.0f, 1.0f);
         glVertex3f(-1.0f, -1.0f, 1.0f);
         glVertex3f(1.0f, -1.0f, 1.0f);
         
         glColor3f(0.0f, 0.7f, 0.0f);
         glVertex3f(1.0f, -1.0f, -1.0f);
         glVertex3f(-1.0f, -1.0f, -1.0f);
         glVertex3f(-1.0f, 1.0f, -1.0f);
         glVertex3f(1.0f, 1.0f, -1.0f);
         
         glColor3f(0.0f, 0.6f, 0.0f);
         glVertex3f(-1.0f, 1.0f, 1.0f);
         glVertex3f(-1.0f, 1.0f, -1.0f);
         glVertex3f(-1.0f, -1.0f, -1.0f);
         glVertex3f(-1.0f, -1.0f, 1.0f);
         
         glColor3f(0.0f, 0.5f, 0.0f);
         glVertex3f(1.0f, 1.0f, -1.0f);
         glVertex3f(1.0f, 1.0f, 1.0f);
         glVertex3f(1.0f, -1.0f, 1.0f);
         glVertex3f(1.0f, -1.0f, -1.0f);
      glEnd();
   }


And then in my update I just call:

1  
glCallList(chunkList);


This does the whole chunk in one go, does this seem correct?
Before I was create a display list just for one cube, not a whole chunk, now just have one call to
draw a chunk, not 4096 (which is 4096 glTranslatef calls too!).

I guess I need to have an array of display lists for each chunk? How many display lists
can you have?  PS - I'm doing it this way before eventually moving onto VBO's, want to walk
before I can run, and hey, Notch used display lists :-)

Thanks
Offline Longor1996
« Reply #64 - Posted 2013-03-17 15:01:59 »

Hi there!

How about using no glTranslate's at all?
It's very easy!

Modified 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  
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  
// Vertex Offset Variables
private int tx = 0;
private int ty = 0;
private int tz = 0;

// Create a chunk and store in display list
private void createChunkVoxel() {
      chunkList = glGenLists(1);  // this hard coded value will need changing as some point
     glNewList(chunkList, GL_COMPILE);
      createDisplayList();
      glEndList();
   }

private void createDisplayList() {

     
         Chunk c = ChunkManager.getInsance().GetChunk(0);
   
         for (int x = 0; x < c.CHUNK_SIZE; x++) {
            for (int y = 0; y < c.CHUNK_SIZE; y++) {
               for (int z = 0; z < c.CHUNK_SIZE; z++) {
                   // Only create block if it is active
                  if(c.GetBlock(x,y,z).isActive()){
                       
                       // Since your blocks are 2³ Units big, we have to multiply the coordinates by the blocks size.
                      tx = x * 2;
                       ty = y * 2;
                       tz = z * 2;
                       
                       // Render the Cube
                      renderCube();
                   }
               }
            }
         }
     
   }

private void renderCube() {

      //GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
     glBegin(GL11.GL_QUADS);
         glColor3f(0.0f, 1.0f, 0.0f);
         off_glVertex3f(1.0f, 1.0f, -1.0f);
         off_glVertex3f(-1.0f, 1.0f, -1.0f);
         off_glVertex3f(-1.0f, 1.0f, 1.0f);
         off_glVertex3f(1.0f, 1.0f, 1.0f);
         
         glColor3f(0.0f, 0.4f, 0.0f);
         off_glVertex3f(1.0f, -1.0f, 1.0f);
         off_glVertex3f(-1.0f, -1.0f, 1.0f);
         off_glVertex3f(-1.0f, -1.0f, -1.0f);
         off_glVertex3f(1.0f, -1.0f, -1.0f);
         
         glColor3f(0.0f, 0.8f, 0.0f);
         off_glVertex3f(1.0f, 1.0f, 1.0f);
         off_glVertex3f(-1.0f, 1.0f, 1.0f);
         off_glVertex3f(-1.0f, -1.0f, 1.0f);
         off_glVertex3f(1.0f, -1.0f, 1.0f);
         
         glColor3f(0.0f, 0.7f, 0.0f);
         off_glVertex3f(1.0f, -1.0f, -1.0f);
         off_glVertex3f(-1.0f, -1.0f, -1.0f);
         off_glVertex3f(-1.0f, 1.0f, -1.0f);
         off_glVertex3f(1.0f, 1.0f, -1.0f);
         
         glColor3f(0.0f, 0.6f, 0.0f);
         off_glVertex3f(-1.0f, 1.0f, 1.0f);
         off_glVertex3f(-1.0f, 1.0f, -1.0f);
         off_glVertex3f(-1.0f, -1.0f, -1.0f);
         off_glVertex3f(-1.0f, -1.0f, 1.0f);
         
         glColor3f(0.0f, 0.5f, 0.0f);
         off_glVertex3f(1.0f, 1.0f, -1.0f);
         off_glVertex3f(1.0f, 1.0f, 1.0f);
         off_glVertex3f(1.0f, -1.0f, 1.0f);
         off_glVertex3f(1.0f, -1.0f, -1.0f);
      glEnd();
   }

// Draw a vertex with a offset.
private void off_glVertex3f(float x,float y,float z){
         glVertex3f(tx+x,ty+y,tz+z);
}


You could also move the glBegin/glEnd's out of the renderBlock Method into the createDisplayList Method, wich would give you even more performance.

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline steg90

Senior Member


Medals: 1



« Reply #65 - Posted 2013-03-17 17:04:17 »

Cool :-)


Thanks again, really appreciate your help.

This is what I've got in createDisplayList now:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
private void createDisplayList() {

      for(int chunkAmount = 0; chunkAmount < 1;/*chunkManager.GetChunkCount(); */chunkAmount++)
      {
         Chunk c = ChunkManager.getInsance().GetChunk(0);
   
         for (int x = 0; x < c.CHUNK_SIZE; x++) {
            for (int y = 0; y < c.CHUNK_SIZE; y++) {
               for (int z = 0; z < c.CHUNK_SIZE; z++) {
                  GL11.glPushMatrix();                  
                  glTranslatef(x,y,z);
                  if(c.GetBlock(x,y,z).IsActive())
                  {
                     
                     glBegin(GL11.GL_QUADS);
                     renderCube();
                     glEnd();
                  }
                  GL11.glPopMatrix();
               }
            }
         }
      }
   }


This look ok?

The offsets on the vertices is obvious now ;-)

Just need introduce another variable for the chunk offset :-)

Steve
Offline steg90

Senior Member


Medals: 1



« Reply #66 - Posted 2013-03-17 17:45:27 »

Quick update, added offset which is basically chunk number we are up to, also
draws the correct 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  
private void createDisplayList(int chunkNumber) {

      tx = 0; ty = 0; tz = 0;

         Chunk c = ChunkManager.getInsance().GetChunk(chunkNumber);  
   
         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++) {
                  GL11.glPushMatrix();                  
               
                  if(c.GetBlock(x,y,z).IsActive())
                  {
                     tx = ((chunkNumber * Chunk.CHUNK_SIZE) << 1) + (x << 1);
                          ty = y << 1;
                          tz = z << 1;
                     glBegin(GL11.GL_QUADS);
                        renderCube();
                     glEnd();
                  }
                  GL11.glPopMatrix();
               }
            }
         }
   }
Offline steg90

Senior Member


Medals: 1



« Reply #67 - Posted 2013-03-17 20:33:26 »

Hi again,

Just wondering, when I get height from simplex noise, if it is zero I set it to one so at least a block is drawn - thus we have floor, is this correct?

Regards,
Steve
Offline Agro
« Reply #68 - Posted 2013-03-17 20:45:54 »

You should add a number to that height instead. For example, have a default ground area of 10 blocks of height.

so height + 10 should be your end height.

Offline steg90

Senior Member


Medals: 1



« Reply #69 - Posted 2013-03-17 21:51:26 »

Cheers Argo, so height of 10 blocks instead of 1 then when height is 0?  Would doing that
now screw up my array - i.e. arrayIndexOutOfBounds possibly? Or are you saying draw
a ground area separately?

Link to what I have so far:

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

Does that look ok?  Just rendering the same chunk, and a sphere.

Created a ChunkManager Singleton now which takes care of rendering the chunks.

Modified my chunk class to take in offsets for chunk position:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private void createChunkList() {

      tx = 0; ty = 0; tz = 0;

      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++) {
               if(GetBlock(x,y,z).IsActive())
               {
                  tx = ((xOffset * Chunk.CHUNK_SIZE) << 1) + (x << 1);
                       ty = ((yOffset * Chunk.CHUNK_SIZE) << 1) + (y << 1);
                       tz = ((zOffset * Chunk.CHUNK_SIZE) << 1) + (z << 1);

                  glBegin(GL11.GL_QUADS);
                     renderBlock();
                  glEnd();
               }
            }
         }
      }
   }


Simple matter to add a chunk to the world now:

Example:
1  
2  
ChunkManager.getInstance().AddChunk(ChunkType.ChunkType_Land,3,0,3);
ChunkManager.getInstance().AddChunk(ChunkType.ChunkType_Sphere,2,2,2);


An another note, what is best way of drawing block trees and display a wired cube around my chunks for debug purposes?

Also, I'm using display lists to render each chunk, anyways of speeding things up even when using display lists?

First time using OpenGL and I must say, it is great and the people on this forum are awesome!

Thanks,
Steve
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline steg90

Senior Member


Medals: 1



« Reply #70 - Posted 2013-03-18 10:27:29 »

Hi,

Added optimisation so we only draw blocks that aren't hidden by there neighbours, this saves drawing
a lot of unwanted blocks!

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  
   private int createChunkList() {

      tx = 0; ty = 0; tz = 0;
      int iHidden = 0;
      int iBlockCount = 0;

      boolean bDefault = true;
      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++) {
               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) << 1) + (x << 1);
                       ty = ((yOffset * Chunk.CHUNK_SIZE) << 1) + (y << 1);
                       tz = ((zOffset * Chunk.CHUNK_SIZE) << 1) + (z << 1);

                  glBegin(GL11.GL_QUADS);
                     renderBlock();
                  glEnd();
                  iBlockCount++;   // total of blocks that can be seen
              }
               else
                  iHidden++;   // amount of blocks that are surrounded
           }
         }
      }
      System.out.println("Block amount hidden by neighbours:" + iHidden +
            " and Block total rendered:" + iBlockCount);
      return iBlockCount;

   }


Next will be to add some FrustumCulling.

Anybody any tips on when to set blocks to certain types...i.e.dirt, grass...would this be dependant
on the height value?

Thanks
Offline steg90

Senior Member


Medals: 1



« Reply #71 - Posted 2013-03-18 22:29:59 »

Hi,

I am wanting to cull chunks of my voxels (those that don't appear in the view frustum).

I was wondering, am I correct or completely wrong in saying that I cull on a per chunk basis, not per block as my geometry is already at the GPU?

Also, is it correct to use an Octree structure when frustum culling in a voxel engine? Any tips?

Thanks,
Steve
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #72 - Posted 2013-03-18 22:32:32 »

merged Clueless

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline steg90

Senior Member


Medals: 1



« Reply #73 - Posted 2013-03-18 22:37:23 »

Ooops, sorry about that. 
 Roll Eyes

Offline Vermeer

JGO Coder


Medals: 16



« Reply #74 - Posted 2013-03-19 13:54:50 »

Hi, would you be able to show a picture of your game/rendering. I'm doing the same thing and I'm interested how yours is looking. Much of your post is interesting and relevant to mine also.

Will keep popping back to see how it's going.
Offline steg90

Senior Member


Medals: 1



« Reply #75 - Posted 2013-03-19 22:29:39 »

Hi,

No probs, here is a small video of it:


http://www.youtube.com/watch?v=zlBbj4kzkY8


Did you first use display lists, should I switch to VBO's?  Mine is still display lists, running
on my Macbook pro which only has the intel HD gfx 3000 video card, guess this isn't
very good?

Thanks
Offline Vermeer

JGO Coder


Medals: 16



« Reply #76 - Posted 2013-03-20 08:49:40 »

That's looking good, I like the lighting and the skybox. Thanks for posting that. I started with immediate mode. And I'm trying VBO but its quite a learning curve. Though it does draw much faster for me.

Looks like an interesting project, will keep checking on ur progress.
Offline steg90

Senior Member


Medals: 1



« Reply #77 - Posted 2013-03-20 19:25:03 »

Thanks,

Display lists seem to be performing fine for me, I've put some stuff into VBO's and tested, doesn't seem too bad to do, although it looks like it depends on the version of OpenGL you are using with some of the methods as they do change a lot?!

Think if I put in frustum culling, will need to partition the space up, octree comes to mind, have you implemented one in your project?

Thanks
Offline Vermeer

JGO Coder


Medals: 16



« Reply #78 - Posted 2013-03-20 19:32:46 »

Hi

My space is partitioned into 16^3 chunks. But no frustum culling or octree. I'm still struggling with the VBO's at the moment! Then I think I'll look at terrain generation next!
Offline steg90

Senior Member


Medals: 1



« Reply #79 - Posted 2013-03-20 20:25:48 »

Hi,

What do you mean by your space is partitioned into 16^3 chunks?  I guess you mean
you re using chunks of 16x16x16, this is the same as me.

What would be needed is to use an octree structure to partition the world into smaller spaces, thus when doing frustum culling there isn't as many checks to do - I can't really see the point of doing frustum culling without space partitioning unless somebody tells me otherwise?

I'm also doing neighbouring checks on the blocks so blocks that are hidden by other blocks aren't rendered.

Cheers



Offline steg90

Senior Member


Medals: 1



« Reply #80 - Posted 2013-03-20 21:57:44 »

Hi,

Wanting to add a player to the voxel engine now.  I've basically just centred a cube as the player
in the centre of the screen.  I'm just applying the camera's pitch to the player, something like this:

1  
      glRotatef(-yRot, 0.0f, 1.0f, 0.0f);


Where yRot is the camera's pitch, now I'm presuming I don't move the player about, just the world as I am doing with my camera?  I did try to make the player cube rotate on the X-axis, but it looks silly as the cube goes out of alignment with the world:

1  
      glRotatef(-xRot, 1.0f, 0.0f, 0.0f);


My question is, how do you add a player and move it around the world correctly - i.e. so it 'angles' with the land so to speak...

Regards,
Steve

Offline Vermeer

JGO Coder


Medals: 16



« Reply #81 - Posted 2013-03-20 22:16:49 »

Sorry I have no idea about octrees at the moment.

As far as your player is concerned...
Are you saying the player is viewed in the 3rd person?

If you want ur player to rotate in line with there facing direction you will have to use the vector components of x and z. 

I notice when u move the camera in ur video , u also rotate the landscape rather than use the camera rotation, then move it forward with its x and a vector.

I can paste code tomorrow for this if you would like.
Offline steg90

Senior Member


Medals: 1



« Reply #82 - Posted 2013-03-20 22:34:39 »

Hi,

Basically show a cube in the middle of the screen as the player, yes third person.

Here is a video of me of the player as a cube, looks ok now Smiley

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


Thanks
Offline Vermeer

JGO Coder


Medals: 16



« Reply #83 - Posted 2013-03-21 09:49:58 »

Yea that looks cool, moves fine. Are you going to use models or keep it as blocks?
Offline steg90

Senior Member


Medals: 1



« Reply #84 - Posted 2013-03-21 19:59:38 »

Hi,

The player will possibly be made up of a couple of blocks...

Just added in block detection - basically detect if block you are on is active or not, need check if block in front, back etc are active also and then need to stop you moving when you are!

How is yours going?
Offline Vermeer

JGO Coder


Medals: 16



« Reply #85 - Posted 2013-03-21 22:20:23 »

Hi,

The player will possibly be made up of a couple of blocks...

Just added in block detection - basically detect if block you are on is active or not, need check if block in front, back etc are active also and then need to stop you moving when you are!

How is yours going?

The way I did that is when u press to move it adds on you x and z vectors to your position, then if that block is solid at you height, then you can't move, otherwise you go ahead and apply those vectors.

It's going we'll thank you, will be posting a video update tomorrow. Got vertex lighting, and a new map loading done today.

I like how yours is looking, are your maps going to be bigger or are you having levels in your game? Like to solve puzzles on smaller maps? Sorry for asking questions!
Offline steg90

Senior Member


Medals: 1



« Reply #86 - Posted 2013-03-22 18:24:59 »

Hi,

I got camera moving up and down, my player always stays in same place, don't know if this is
correct?

I'm trying to put collision detection in, can detect when player has a block in front, underneath etc, but if player turns (camera turns) and then move's forwards my collision is all messed up.  Think I need to know which way the camera is facing, thought I had this with some trig:

direction  = atan(camerapos.x / camerapos.z );

How are you doing the player movement, collision detection?

Anybody else got any tips on collision detection with block - and how would I implement a vector slide?

Maps will be larger, just messing around at the moment, just want to get the collision stuff working, thought this would be simple.

Thanks,
Steve

Offline Vermeer

JGO Coder


Medals: 16



« Reply #87 - Posted 2013-03-22 19:25:28 »

Hi

This is how I impliment the collison and player moving:
1  
2  
3  
4  
5  
if(!isBlock(xpos - (float) Math.sin(yrot * piover180) * 0.35f,zpos - (float) Math.cos(yrot * piover180) * 0.35f)){
               
           
            xpos -= (float) Math.sin(yrot * piover180) * 0.1f;
            zpos -= (float) Math.cos(yrot * piover180) * 0.1f;


 piover180 = 0.0174532925f  to convert to radiens
 xpos, zpos are my float player positions. yrot, y axis rotation.
0.35 is the distanced moved (notice its more than the actual movment, this is so you cannot get too close to a block) (ie the camera has a size)
There is an issue with this and diagonally placed blocks. This will be sorted later.

the player updates are only applied if there is not a block.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   private boolean isBlock(float x, float z) {  
         
      int c = getChunk(x, z); //get the chunk of the block

                           x=x%16; //this will find the block within the chunk
     z=z%16;
                     
      if(chunks[c].getblockID(x, (int)playerheight,z)>0)return true;
     
      return false;
   }
   


This is a simplified version of my isBlock code. (ther is some stuff in there to sort out negativate coordinates)
The Chunk getBlockID just returns a value. 0 is Air, all other blocks are solid at the moment.
This assumes the player is 1 block high!

If you want any more code Ill pop it on. It works for me, but more experience programmers may have better solutions.
Offline steg90

Senior Member


Medals: 1



« Reply #88 - Posted 2013-03-22 19:42:22 »

Mmmmm, not too clear on your code, is that all your camera code?

This is my camera 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  
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  
public class GameCamera {

   //3d vector to store the camera's position in
   private Vector3f    position    = null;
    //the rotation around the Y axis of the camera
   private float       yaw         = 0.0f;
    //the rotation around the X axis of the camera
   private float       pitch       = 0.0f;
   
   
  //Constructor that takes the starting x, y, z location of the camera
   public GameCamera(float x, float y, float z, float pitch, float yaw)
    {
        position = new Vector3f(x, y, z);
        this.yaw(yaw);
        this.pitch(pitch);
    }

    public float getCameraYaw() { return yaw; }
    public float getCameraPitch() { return pitch; }
    public Vector3f getCameraPosition() { return position; }
   
  //increment the camera's current yaw rotation (Y)
   public void yaw(float amount)
    {
        //increment the yaw by the amount param
      if(yaw < 0.0) yaw += 360;
       if(yaw > 360) yaw -= 360;
          yaw += amount;
    }
     
    //increment the camera's current pitch rotation (X)
   public void pitch(float amount)
    {
        //increment the pitch by the amount param
      if(pitch > 90) pitch = 90;
       if(pitch < -90) pitch = -90;
          pitch += amount;
    }
   
    public void roll(float amount)
    {
       // not used
   }
   
    // X is sin(angle) * r
   public float getXPos(float angle)
    {
       return (float)Math.sin(Math.toRadians(angle));      
    }
    // Z is cos(angle) * r
   public float getZPos(float angle)
    {
       return (float)Math.cos(Math.toRadians(angle));      
    }
  //moves the camera forward relative to its current rotation (yaw)
   public void walkForward(float distance)
    {
        position.x -= distance * getXPos(yaw);
        position.z += distance * getZPos(yaw);
    }
     
    //moves the camera backward relative to its current rotation (yaw)
   public void walkBackwards(float distance)
    {
        position.x += distance * getXPos(yaw);
        position.z -= distance * getZPos(yaw);
    }
     
    //strafes the camera left relative to its current rotation (yaw)
   public void strafeLeft(float distance)
    {
       // player position is x=sin(yaw), z=cos(yaw)
        // x is distance * sin(yaw)
      // z is distance * cos(yaw)  
      // we subtract 90 to the angle so to strafe
       position.x -= distance * getXPos(yaw- 90);  // .5PI
       position.z += distance * getZPos(yaw- 90);  // .5PI
   }
     
    //strafes the camera right relative to its current rotation (yaw)
   public void strafeRight(float distance)
    {
       // x is distance * sin(yaw)
      // z is distance * cos(yaw)  
      // we add 90 to the angle so to strafe
       position.x -= distance * getXPos(yaw+90);
        position.z += distance * getZPos(yaw+90);
    }
   
    public void jump(float distance)
    {
       position.y -= 1;
    }
    public void down(float distance)
    {
       position.y += distance;
    }

    public void resetPosition(float x, float y, float z, float pitch, float yaw)
    {
       position.x = x; position.y = y; position.z = z;
       this.pitch = pitch;
       this.yaw = yaw;
    }
   
    //translates and rotate the matrix so that it looks through the camera
   //this does basically what gluLookAt() does
   public void lookThrough()
    {
        //rotate the pitch around the X axis
       GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        //rotate the yaw around the Y axis
       // remove line below for third person
       GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        //translate to the position vector's location
       GL11.glTranslatef(position.x, position.y, position.z);

    }
   
}


How do you initialise your camera position at the start and your floor?

PS - my math aint too good!

Regards,
Steve

Offline Vermeer

JGO Coder


Medals: 16



« Reply #89 - Posted 2013-03-22 20:58:06 »

You perhaps more organised than I am!
My camera is just xpos,ypos,zpos floats.     Ypos is player height
Then I have lookup (-lookup is lookdown!)
That's it. I don't even have a class for it yet!

The floor is the start x,z blocks top surface. The camera is 1.8 units above that.

So in my update(). I always fall.... Unless I'm = or a bit lower than the block surface.
I fall to the block surface.

So if block is at height 2.  The floor 3.  So the cam cannot be less than 4.8

When jumping I use acceleration, and falling is acceleration to rather than subtracting a linear value.

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

xsi3rr4x (23 views)
2014-04-15 18:08:23

BurntPizza (18 views)
2014-04-15 03:46:01

UprightPath (32 views)
2014-04-14 17:39:50

UprightPath (16 views)
2014-04-14 17:35:47

Porlus (32 views)
2014-04-14 15:48:38

tom_mai78101 (58 views)
2014-04-10 04:04:31

BurntPizza (116 views)
2014-04-08 23:06:04

tom_mai78101 (216 views)
2014-04-05 13:34:39

trollwarrior1 (183 views)
2014-04-04 12:06:45

CJLetsGame (190 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!