Terrain Paging and Bullet

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
underworldguardian
Gnoblar
Posts: 12
Joined: Thu Nov 13, 2014 3:08 pm

Terrain Paging and Bullet

Post by underworldguardian »

Hello,

I am a bit stuck here. I took the endless world sample and used it in my project and so far everything works well (it loads and unloads terrain pages). I am also happy using the perlinnoise terrain generator as I need pretty random maps (I still need to figure out whether the terrain changes at point x when it gets generated, then I look away and it gets unloaded and then it gets loaded again when I look back). But now I run into the problem that I would like to get the currently loaded terrains to use their heightmap in the Bullet Physics engine. I already have some code to create a Bullet btHeightfieldTerrainShape from a terrain, but I can not get the currently loaded terrains. Can somebody tell me how this is done?

Furthermore I also need to know whether I can tell the paging system that it should also load terrain pages around other points that I tell it to, because I need the heightmap for the physics simulation at that point (Say I have a creature running in the terrain in bullet physics and I look with the camera to another point). How can I do this? Or where can I find examples or things to read up how this is done?

I am also completely okay with any kind of non-trivial sample just that I can understand how the terrain paging system works.


Thank you in advance!
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: Terrain Paging and Bullet

Post by lingfors »

Unloading the terrain from the graphics part surely doesn't mean you have to unload it from the physics engine? Sounds scary to be an NPC in that world, hero turns away from you and suddenly you're free-falling...
underworldguardian
Gnoblar
Posts: 12
Joined: Thu Nov 13, 2014 3:08 pm

Re: Terrain Paging and Bullet

Post by underworldguardian »

That is what I thought as well. But I was not completely sure whether one always keeps all the physics objects in the physics engine. The point is, since the perlin noise generates the world as it is used, it is not existing before not either the camera or the NPC "looks" at it. That means for me, that the terrain is either generated if the camera looks at it (the camera has no player attached, it is only a free floating cam) or the NPCs walk through it. So do I just check where my camera, NPCs position and check whether their terrain is defined:

Code: Select all

Ogre::Terrain * terrain = terrainGroup->getTerrain(x, y);
if(terrain != NULL)
{
 //yay we are fine
}
else{
PerlinNoiseTerrainGenerator::define( terrainGroup, x, y )
}
where the define method of the PerlinNoiseTerrainGenerator is (from EndlessWorldSample)

Code: Select all

void PerlinNoiseTerrainGenerator::define( TerrainGroup* terrainGroup, long x, long y )
{
	uint16 terrainSize = terrainGroup->getTerrainSize();
	float* heightMap = OGRE_ALLOC_T(float, terrainSize*terrainSize, MEMCATEGORY_GEOMETRY);

	Vector2 worldOffset( Real(x*(terrainSize-1)), Real(y*(terrainSize-1)) );
	worldOffset += mOriginPoint;

	Vector2 revisedValuePoint;
	for( uint16 i=0; i<terrainSize; i++ )
		for( uint16 j=0; j<terrainSize; j++ )
		{
			revisedValuePoint = (worldOffset + Vector2(j,i)) / mCycle;
			heightMap[i*terrainSize + j] = produceSingleHeight( revisedValuePoint ) * mHeightScale;
		}
	terrainGroup->defineTerrain(x,y,heightMap);
	OGRE_FREE(heightMap, MEMCATEGORY_GEOMETRY);
}
But how do I get the terrain heightmaps then to generate my physics terrain? Or am I completely wrong here?
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: Terrain Paging and Bullet

Post by lingfors »

I guess this is a problem you get as soon as you start trying to combine any two subsystems, doesn't matter if they are graphics and physics (object is handled by physics and should interact with terrain, both object and terrain should be rendered), graphics and sound (rocket should be rendered, and when it explodes an explosion sound should be played), physics and sound (two objects are moved by physics engine, and when they collide a collision sound should be played), graphics and AI (AI controlled character can perform different actions (controlled by AI) which are rendered using different animations (controlled by rendering engine), AI and sound (AI controlled character can talk), etc.

Ideally, all these different subsystems should be completely separated, i.e. no subsystem knows anything about any other subsystem, and can only affect each other indirectly. If you do this correctly, you can run different subsystems in different threads, with different update rates, etc. For instance, AI is running on one thread updated 10 times per second, physics is executed on another thread updated 30 times per second, graphics is updated on a third thread as fast as possible.

I.e. in your example, the rendering engine should not be responsible for generating the terrain, it should be handled by a separate subsystem that is somehow useable both from the terrain rendering engine and the physics engine.

Is this easy to accomplish? Probably not, I have never achieved it. I think there's a tutorial in the 2.1 branch that demonstrates how this could be accomplished, though.
underworldguardian
Gnoblar
Posts: 12
Joined: Thu Nov 13, 2014 3:08 pm

Re: Terrain Paging and Bullet

Post by underworldguardian »

So basically the endless world example is useful for graphics only and some self-written physics, but as soon as you want to access the terrain that it stores in the page handler, it gets ridiculously useless. Hmm, too bad..I will see if I find your mentioned tutorial. Thanks for the information!
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: Terrain Paging and Bullet

Post by lingfors »

underworldguardian wrote:So basically the endless world example is useful for graphics only and some self-written physics, but as soon as you want to access the terrain that it stores in the page handler, it gets ridiculously useless. Hmm, too bad..I will see if I find your mentioned tutorial. Thanks for the information!
Well, they are only samples made to showcase the capability of Ogre, after all. :wink:
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Terrain Paging and Bullet

Post by c6burns »

underworldguardian wrote:So basically the endless world example is useful for graphics only and some self-written physics, but as soon as you want to access the terrain that it stores in the page handler, it gets ridiculously useless.
What? Terrain paging is done by distance, not by frustum ... there is no "oops I turned the camera and the physics world changed because of frustum culling" scenario. Can you imagine? Just spinning the camera hard enough would stall the game. Frustum culling (aka not-rendering-that-which-is-not-in-the-camera's-view) will not change the terrain data. It just will stop you rendering what you can't see. In your case the problem is "oops its time to page in some terrain, but I literally am generating it on the fly so there's nothing to give to physics yet". The "normal" scenario for a game would be that you have a number of pre-generated terrain sections that you actually page in/out of memory using a PageManager + PageProvider, and you probably have pre-generated physics that you just deserialize ... still there's no problem doing what you are doing and generating on the fly. You are *pretending* to have a PageProvider but it doesn't do anything (DummyPageProvider).
emmanuel1359
Kobold
Posts: 32
Joined: Mon Feb 02, 2015 11:05 pm

Re: Terrain Paging and Bullet

Post by emmanuel1359 »

hello,

to help, you can read and use my code:
https://bitbucket.org/emmanuel1359/ogre ... mapterrain

there is an heigthmap terrain thats works not bad

and a testing terrain volume code

hope that helps
underworldguardian
Gnoblar
Posts: 12
Joined: Thu Nov 13, 2014 3:08 pm

Re: Terrain Paging and Bullet

Post by underworldguardian »

@emmanuel1359:
Unfortunately I find a dead link when I click you bitbucket link. I think this is the right one:
https://bitbucket.org/emmanuel1359/heightmapterrain/

So I see that I was unclear. So I will redescribe what I think I should do:

I use the perlinnoise generator to produce terrain as something walks into it. That means that all I want to do is basically ask the TerrainGroup for the Terrain at a certain position. If the terrain is not there yet, it should trigger the terrain loading which produces the terrain for it. Then I will put this terrain into the Bullet engine to make a bullet representation of it.

In the sense of 3D characters, this would mean:

Code: Select all

for each character:
    Terrain* terrain = terrainGroup.getTerrainAtPosition(character.position)
    BulletTerrain* bTerrain = new BulletTerrain();
    bTerrain.createFromTerrain(terrain);
    if(bTerrain.getID is not yet it world)
          bulletworld.add(bTerrain)
   end if
end for
Additionally I would drop bTerrains as they are not requested anymore.

So my question is, can this idea be implemented with the Ogre::TerrainGroup or similar parts? I just think that it is very annoying if I have to implement a whole terrain pager again just because I need one for the physics as well. I also think that it is not a good idea to load all physics terrains at the same time because my terrain is theoretically infinite. Furthmore how can I hide/detach the whole terrain from the world if it does not need to be shown (let us say if the hero enters a house)? Any hints on anything I have said?
crancran
Greenskin
Posts: 138
Joined: Wed May 05, 2010 3:36 pm
x 6

Re: Terrain Paging and Bullet

Post by crancran »

underworldguardian wrote:So my question is, can this idea be implemented with the Ogre::TerrainGroup or similar parts? I just think that it is very annoying if I have to implement a whole terrain pager again just because I need one for the physics as well. I also think that it is not a good idea to load all physics terrains at the same time because my terrain is theoretically infinite. Furthmore how can I hide/detach the whole terrain from the world if it does not need to be shown (let us say if the hero enters a house)? Any hints on anything I have said?
I think you'll find it better to consider making your own wrapper for this.

I tend to believe it's important to put as much effort into the game build process as possible. This way when it comes run-time, as much data that can be baked and packed into easy to load and manage assets makes the overall process much easier. For example, our game editor upon exporting creates a series of baked paged files that contain height-map, vertices, normals, texture mappings, shadow mappings, model placements, holes, and anything else into a set of files that each represent a specific chunk of the game world.

When the game client is then running, the map streaming system simply uses the current world position of the player to determine what pages need to be loaded & rendered, which need to be loaded but not yet rendered because they're still outside the view distance, and those which need to be loaded to give the illusion of the mountainous silhouette. This system simply acts as a broker of data to a plethora of other systems. It informs physics of the terrain's size, vertices, etc. It passes that same data onto the render system to create the terrain page, etc.

I just prefer to keep Ogre-like stuff restricted to graphics only and supplement everything else with my own non-Ogre related code.
Post Reply