jrenner / gdx-proto

GDX Proto - a lightweight 3D engine built with libgdx
Apache License 2.0
161 stars 28 forks source link

Terrain System #8

Open jrenner opened 10 years ago

jrenner commented 10 years ago

Here's some code I used in a previous project for creating heightmapped terrain from reading a PNG file:

HeightMap reads the PNG file and creates a 2d array of vertices

HeightMapModel creates a 3d model based on the HeightMap

it doesn't have LOD or anything like that. At the time I tried to create a Bullet btHeightfieldTerrain body based on the data but could never get it to work.

HeightMap.java HeightMapModel.java

Caresilabs commented 10 years ago

Awesome! I try to bake it in my terrain manager.

I googled libgdx heighmap and there are a height map test that might be useful. However we need to get it working with bullet of course.

Caresilabs commented 10 years ago

I forgot to ask, how does this render? Isn't it easier to build a model class from the vertices and them just create a model instance? :)

jrenner commented 10 years ago

I forgot to add the GroundChunk class, you can see it is used inside HeightMapModel: https://gist.github.com/jrenner/169438e75773d0dbe861

The ground chunks are just added to a list, and then rendered like this:

    private void renderGroundChunks() {
        Lists.visibleGroundChunks.clear();
        for (GroundChunk ground : Lists.groundChunks) {
            if (ground.isVisible(cam)) {
                modelBatch.render(ground, environment);
                Lists.visibleGroundChunks.add(ground);
            }
        }
    }

visibleGroundChunks was just for debugging to see how many were visible in that project, it's not necessary

jrenner commented 10 years ago

You might also want these files for testings: Here is the height map data file: http://www.superior-tactics.com:12080/hm1.png

Here is the texture you can use for it: http://www.superior-tactics.com:12080/hm1_paint.png

It should look something like this: https://www.youtube.com/watch?v=xNbi63i8aT4

Caresilabs commented 10 years ago

This looks great!

Im home from work in about 2/3h and have a lot of time to fix this. cant wait hehe.

Have you used BtHeightFieldTerrainShape and know how it works?

jrenner commented 10 years ago

a while back when I was working on that terrain stuff I tried to create a btHeightFieldTerrainShape but the shape always came out corrupted/distorted, and I never got it working correctly. I ended up just writing a function that checks the height (y) of the ground at the 2d point (x,z) to do simple ground collisions. That worked fine, but it won't for GDX-proto, because we need the bullet shape to get the ground terrain working properly with ray casting.

Caresilabs commented 10 years ago

What I see there are two options:

  1. get heightmapshape working
  2. Use the generetaed Model and creating a mesh collision on it

Im going to go with #2 first but when we find a soloution it will be easy as a pie to change it.

jrenner commented 10 years ago

Yeah, model-based object should be easy to get working, it's already used for the static geometry

Caresilabs commented 10 years ago

Alright!

So Im thinking That you have a Terrain manager that is responsible for all terrain related stuff.

Terrain Manager can created TerrainChunks/GroundChunks (depending on the name) for you by example writing Terrain.createMeshTerrain(parameters) and there should be a a couple of methods to do that. At the moment I have:

  1. CreateMeshTerrain
  2. CreateHeightMapTerrain
  3. ???

And when you have the returned Chunk you can modify it by:

  1. Adding normal and specular maps
  2. Adding static objects such as trees, rocks and more scene object that the user like to add.

Then you want to add the chunk to the game and here is what I need some help.

Because there are multiple ways of dealing with terrains. One way is to have one large chunk that is your world however in most cases that will not work. Lets say you want to create a Open World game. Well good luck on that. With some research I found that a common method is to scroll the terrain chunks with you.

000 010 000

By using 9 chunks where the 0 represent a distant chunk and the 1 is the chunk the player is on.

When the player steps over to a 0 new terrain are created and some terrain are destroyed to keep the player at the middle chunk.

Do you have any suggestions on how I should set up the terrain?