Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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 6
  ignore  |  Print  
  LWJGL - Voxel Engine  (Read 15753 times)
0 Members and 1 Guest are viewing this topic.
Offline steg90

Senior Member


Medals: 1



« Reply #60 - Posted 2013-04-10 10:43:16 »

Hi,

I've implemented testing voxel at player position, this is what I've already done and it isn't very accurate, you can still pass through blocks - hence the reason I was asking over using AABB.

Or are you saying which maybe you are - use each corner of the player as position for voxel, thus you could test 8 positions?

@Argo - so, once I've got the block I then use sphere test with the blocks AABB?

I think doing a sphere (player) to AABB (voxel) test would prove best method.  I've modified some AABB/Circle/CollisionLibrary class code to support Z:

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  
public class AABB {
   public Vector center;
   public float r[];

   public AABB(final float width, final float height, final float depth) {
      center = new Vector();
      r = new float[3];
      r[0] = width * 0.5f;
      r[1] = height * 0.5f;
      r[2] = depth * 0.5f;
   }

   public void update(final Vector position) {
      center.x = position.x;
      center.y = position.y;
      center.z = position.z;
   }
}

public class Circle {
   public Vector center;
   public float radius;

   public Circle(final float radius) {
      center = new Vector();
      this.radius = radius;
   }

   public void update(final Vector position) {
      center.x = position.x;
      center.y = position.y;
      center.z = position.z;
   }
}

public class CollisionLibrary {
   public static boolean testAABBAABB(final AABB box1, final AABB box2) {
         if (Math.abs(box1.center.x - box2.center.x) > (box1.r[0] + box2.r[0])) return false;
         if (Math.abs(box1.center.y - box2.center.y) > (box1.r[1] + box2.r[1])) return false;
         if (Math.abs(box1.center.z - box2.center.z) > (box1.r[2] + box2.r[2])) return false;
         return true;
      }
   
   public static float sqDistPointAABB(final Vector p, final AABB aabb) {
         float sqDist = 0.0f;
         float v;
         float minX, minY, minZ, maxX, maxY, maxZ;
           
         // get the minX, maxX, minY, maxY and minZ, maxZ points of the AABB
        minX = aabb.center.x - aabb.r[0];
         maxX = aabb.center.x + aabb.r[0];
           
         minY = aabb.center.y - aabb.r[1];
         maxY = aabb.center.y + aabb.r[1];
           
         minZ = aabb.center.z - aabb.r[2];
         maxZ = aabb.center.z + aabb.r[2];
           
         // test the bounds against the points X axis
        v = p.x;
           
         if (v < minX) sqDist += (minX - v) * (minX - v);
         if (v > maxX) sqDist += (v - maxX) * (v - maxX);
           
         // test the bounds against the points Y axis
        v = p.y;
           
         if (v < minY) sqDist += (minY - v) * (minY - v);
         if (v > maxY) sqDist += (v - maxY) * (v - maxY);
           
         // test the bounds against the points Z axis
        v = p.z;
           
         if (v < minZ) sqDist += (minZ - v) * (minZ - v);
         if (v > maxZ) sqDist += (v - maxZ) * (v - maxZ);
           
         return sqDist;
      }
   
      public static boolean testCircleAABB(final Circle circle, final AABB box) {
         // get the squared distance between circle center and the AABB
        float sqDist = sqDistPointAABB(circle.center, box);
         float r = circle.radius;
           
         return sqDist <= r * r;
      }
   
}


Thanks
Offline Pauler
« Reply #61 - Posted 2013-04-10 18:56:56 »

Why don't you use the collision check I am using? It is really accurate.

Anyway, I am trying to create a basic block picking but I cannot really get it working.
Offline steg90

Senior Member


Medals: 1



« Reply #62 - Posted 2013-04-10 20:41:51 »

Do you check which block you are on before doing your AABB, so you know if block is active or not?

You will need to do a ray cast to do the picking, I'm going to try that tomorrow providing I get this collision working!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online opiop65

JGO Kernel


Medals: 123
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #63 - Posted 2013-04-10 20:45:23 »

Do you check which block you are on before doing your AABB, so you know if block is active or not?

You will need to do a ray cast to do the picking, I'm going to try that tomorrow providing I get this collision working!

You don't need to test if the block is active. The only thing you need to test for is if the player vector position is within the block vector. If it is, handle collision appropriately. I don't understand why you keep bringing up your
1  
active
boolean Tongue

Offline Pauler
« Reply #64 - Posted 2013-04-10 20:46:53 »

I save the current chunk to a list.

Then, every time that I move I check only the current chunk for solid blocks. If I am inside a solid block after my movement, you get back to position you were before colliding.

Also, I set my onGround bool to false each time I move, so my gravity script can check if I am still walking on solid blocks.

Paul.
Online opiop65

JGO Kernel


Medals: 123
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #65 - Posted 2013-04-10 21:41:37 »

That seems a little sketchy... you really shouldn't really on that way of collision unless you're writing anything other than a basic 2D game. You really should just AABBs and implement sliding movement.

Offline steg90

Senior Member


Medals: 1



« Reply #66 - Posted 2013-04-10 22:33:03 »

@opiop65 - Surely you need to test if the block is active else you would collide with every block in the chunk?!?!?  My chunks are made up of 16^3 blocks, of course, some of them are not active, so how do I check the blocks in the chunk?!

Thanks
Offline Agro
« Reply #67 - Posted 2013-04-10 22:49:37 »

So, why is this so hard again? Just get the blocks around you. <.< Its not that hard if you know how big the cubes are. Its like a grid. Treat the chunk as a grid, and find where you are in the grid. Then test collisions around it.

Offline steg90

Senior Member


Medals: 1



« Reply #68 - Posted 2013-04-10 23:57:06 »

Hi @Argo,

So, get blocks around player and do Sphere to AABB on these blocks...

All my blocks are 1x1x1.

I've set player radius as 1 as they are also 1 unit.

Example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
Vector playerPosition = new Vector();
playerPosition.x = Math.abs(v.x);        // get player/camera position
playerPosition.y = Math.abs(v.y);
playerPosition.z = Math.abs(v.z);
playerSphere.update(playerPosition);   // playerSphere is radius of 1
     
AABB voxel = new AABB(1,1,1);  // width, height, depth
Vector voxelPosition = new Vector();
voxelPosition.x = 7; voxelPosition.y = 1; voxelPosition.z = 7;
voxel.update(voxelPosition);

if(CollisionLibrary.testCircleAABB(playerSphere, voxel)) {
    // do whatever
}


The above code is just an example of a block that is at position x:7, y:1 and z:7, so, when player is at that position, collision occurs, and it does, and is a whole lot better than just doing collision with, is player on block N.

Of course, the example above needs to get the blocks position around the player - top, bottom, front, back, left, right - then check for collision with these.

When I get a block - I NEED to see if it is active or not, otherwise what is the point in testing for a collision with an empty block?!

Will need a slide vector when collision for more realism - this is in fact an impulse force, so guess it could be:

impulse = -(playerVelocity * blockCollidedWithNormal) * blockCollidedWithNormal;
playerVelocity += impulse;

Or, has anybody else any good ways of doing a slide vector without tons of trig?!

*EDIT - I found this article on sliding vectors / collision response :

http://www.peroxide.dk/papers/collision/collision.pdf


Thanks
Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #69 - Posted 2013-04-11 06:47:00 »

So, why is this so hard again? Just get the blocks around you. <.< Its not that hard if you know how big the cubes are. Its like a grid. Treat the chunk as a grid, and find where you are in the grid. Then test collisions around it.

Collision detection is easy.
Convincing them that they don't need AABBs to collide with the world is hard.

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-04-11 09:38:58 »

Some forms of collision detection is easy, some not so.  I'm finding AABB the best solution for what I am trying to do having tried another method which wasn't too successful.

What I'm struggling with is collision response, what to do when collision occurs.  Of course, we need to determine if the collision was in front, behind, above, below, left or right of the player first of all.

When I land on top of a block, I then cannot move forward as my code thinks there is a collision in front, even though it is with the ground... :-(
Offline Vermeer

JGO Coder


Medals: 16



« Reply #71 - Posted 2013-04-11 19:34:30 »

You could split the vertical test and forward movement test up. I always knew the height I was testing, before I tried to test forward movement. So when you move forward there is only 1 test.

There only needs to be one test - in the direction you want to move.
When you test, you should not alter the actual players position.

If a collision occurs, you can either:

Not move,
Or are repelled with some force, so that you are pushed sideways, so you don't feel like you are sticking to a wall when approaching it at an angle.
Offline Pauler
« Reply #72 - Posted 2013-04-11 20:18:13 »

I still cannot make block picking to work.
Offline steg90

Senior Member


Medals: 1



« Reply #73 - Posted 2013-04-12 09:14:23 »

@Vermeer - I believe you need to add the height on when checking collision so we don't get a collision with block below.  I do the ground test in a separate collision check, from there I get the height of the ground we are on and now use that when moving.

As for not moving player when testing, you mean store the cameras position in a player vector and check this before allowing movement? Makes sense.

I think getting the players normal (direction vector) so we always know which way the player is facing would be beneficial to this form of collision detection, thus, we would know if we collided from front of player, behind or left, right - below shows P as player and arrow showing players normal vector.

     /|\        P
      |         |
      P       \|/   

So if a collision occurs, we could check the players normal vector...would this work?
Offline Vermeer

JGO Coder


Medals: 16



« Reply #74 - Posted 2013-04-12 11:45:31 »

Hi Steg

I guess there are lots of ways to approach this, but I think the important thing that you know how the solution works in your game so that you can fix issues with it.

Yes - I don't apply transformations directly to the player, but a dummy object. If There is no collision, I then move the player.

I also use the players vector too yes, so I will know which block to test. I think I didn't explain below that once you test the height, you do use that height information when testing the forward movement.

Have you got it working how you want yet?
Offline Vermeer

JGO Coder


Medals: 16



« Reply #75 - Posted 2013-04-12 11:46:36 »

I still cannot make block picking to work.

What part is not working, how far have you got with it?
Offline Pauler
« Reply #76 - Posted 2013-04-12 13:24:31 »

I don't really know how to apply the rotation to the block picking.
Offline steg90

Senior Member


Medals: 1



« Reply #77 - Posted 2013-04-12 19:10:39 »

@Pauler -

Off top of my head, not at computer with my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
double cameraPitchInRadians = Math.toRadians(camera.getCameraPitch());
double cameraYawInRadians = Math.toRadians(camera.getCameraYaw());
float f = (float)Math.cos(cameraPitchInRadians);
         
float newX = distanceFromCameraYouWantToMeasure * f * (float)Math.sin(cameraYawInRadians);
float newZ = distanceFromCameraYouWantToMeasure * f * (float)Math.cos(cameraYawInRadians);
float newY = distanceFromCameraYouWantToMeasure * (float)Math.sin(cameraPitchInRadians);
float newX = distancefromcamera * Math.sin(cameraxrotInRadians);
float newZ = distancefromcamera * Math.cos(camerayrotInRadians);


@Vermeer - not had time to look at it yet, hopefully get some time later this evening.  Think going to try and get normal vector of player direction...
Offline Vermeer

JGO Coder


Medals: 16



« Reply #78 - Posted 2013-04-12 20:46:47 »


I still cannot make block picking to work.

These are my methods to send a ray from the camera, it may be very in-efficiant, but it works to test it.
You will notice I have lots of small methods to add vectors and suchlike.

Basically I use trig to calculate where the camera is pointing, then test along the ray at 0.1f intervals, and check if It hits a solid block.
Someone may suggest a better way, and it can certainly be optimised a lot. Trig functions are expensive, Once the angle is know you can
make use of the same angle, without recalculating. But this may help you get it working, so you can develop it.

http://pastebin.java-gaming.org/94c3e0b3f53
Offline steg90

Senior Member


Medals: 1



« Reply #79 - Posted 2013-04-12 20:58:36 »

I believe Sparky83 put some code up for a good ray cast - the 3d game primer book by Fletcher Dunn explains it :-)

PS - does anybody know how to do a slide vector once a collision has occurred, I believe I need this as sometimes my player gets stuck when collision happens...
Offline Pauler
« Reply #80 - Posted 2013-04-15 17:10:12 »

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

Added some basic block picking.
Offline Vermeer

JGO Coder


Medals: 16



« Reply #81 - Posted 2013-04-15 17:21:52 »

That is looking great, lots of features! It all seems to be working well. Great video. Smiley

What's next?
Offline Longor1996
« Reply #82 - Posted 2013-04-15 18:58:39 »

Hi there.

That looks pretty good so far.
How many cubes & chunks in total are in memory (RAM) right now?

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline Pauler
« Reply #83 - Posted 2013-04-15 19:13:32 »

256 chunks, 1048576 blocks.
Offline Longor1996
« Reply #84 - Posted 2013-04-15 19:29:46 »

256 chunks, 1048576 blocks.

And the next Question:
How big is the memory footprint with 256,1024,2048,4096 and 8192 chunks?
My experimental engine can handle ~16000 16³ Chunks without crashing and a stable framerate of 50 fps.
Im just wondering what your engine is capable of.

- Longor1996

PS: Your CHunk counter in the video seems to go a little bit, wrong?

Sorry for my bad English! That's because i am from Germany.
Offline Pauler
« Reply #85 - Posted 2013-04-15 19:34:06 »

16384 chunks, 67108864 blocks @60 fps. (vsync enabled)
Offline Longor1996
« Reply #86 - Posted 2013-04-15 20:02:47 »

16384 chunks, 67108864 blocks @60 fps. (vsync enabled)

Yay! Your Engine is as fast as mine.
The difference in the FPS comes from my PC.
My PC is a big piece of crap, but im happy with it.

Oh, and i think you forgot something: Memory Footprint.
That's what im interested in the most.

- Longor1996

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

Senior Member


Medals: 1



« Reply #87 - Posted 2013-04-15 21:19:44 »

Looking great @Pauler!

I've got an issue in mine now, I'm only rendering chunks when at a certain view distance, this seems to work, but problem is, CPU memory usage goes up on each chunk that gets rendered?!
I am using Display lists, could this be the issue, and this is on my Macbook Pro which has a intel 3000 HD graphics card...

This is how chunks are determined if they are to be rendered or not:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void render(float x, float z)
   {
      for(Chunk c : ChunkList)
      {
         if( (Math.abs(c.getX() * Chunk.CHUNK_SIZE) - Math.abs(x)) < Chunk.CHUNK_SIZE * 3)
         {
            if( Math.abs((c.getZ() * Chunk.CHUNK_SIZE) - Math.abs(z)) < Chunk.CHUNK_SIZE * 3)
            {
               
               c.render();
            }
           
         }
      }
   }


x and z are the players/camera position and getX and getZ return the chunks voxel space position.  CHUNK_SIZE is 16.  ChunkList is an ArrayList of my chunk objects:

1  
private ArrayList<Chunk> ChunkList = new ArrayList<Chunk>();



And render just does a glCallList.

Thanks
Offline Pauler
« Reply #88 - Posted 2013-04-15 21:29:10 »

Lists are really slow. You should change to arrays as I did.
Offline steg90

Senior Member


Medals: 1



« Reply #89 - Posted 2013-04-15 22:00:49 »

Hmmmmm, I believe display lists are just as quick for static data?

My Macbook supports openGL 2.1, VBO's I believe are supported in this version?

Maybe when I'm rendering, the primitives are still going to the CPU, or, maybe my calculation is incorrect for checking if chunks in view...

I think this is incorrect:

1  
2  
3  
4  
5  
6  
7  
if( (Math.abs(c.getX() * Chunk.CHUNK_SIZE) - Math.abs(x)) < Chunk.CHUNK_SIZE * 3)
         {
            if( Math.abs((c.getZ() * Chunk.CHUNK_SIZE) - Math.abs(z)) < Chunk.CHUNK_SIZE * 3)
            {
               c.render();
            }
}


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

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

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

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

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

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

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

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

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

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

CJLetsGame (184 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!