Terrain-Grid, best way

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Terrain-Grid, best way

Post by thetemplar »

Hello,
i am new to all of this, so this might be a dumb question, but i found no answer so far.

I want a map out of squares, each contain two triangles, so that every corner can have its own height.

I did something like this:

Code: Select all

 
while (device->run() && driver)
{
 [..]
    for (double x = 0; x < 100; x++)
        {
            for (double y = 0; y < 100; y++)
            {
                int a = 10;
                video::S3DVertex v[16];
                u16 i[6] = { 0, 1, 2, 1, 2, 3 };
 
                v[0].Pos.set(x + 20, 0, y + 20);
                v[1].Pos.set(x + 20, 0, y);
                v[2].Pos.set(x, 0, y + 20);
                v[3].Pos.set(x, 0, y);
 
                driver->drawVertexPrimitiveList(v, 4, i, 2, video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
            }
        }
     [..]
}
for a small map, which actually works, but is very slow (i drop to about 40fps with only 100*100 squares).
Image

How do i implement this right? I found something with a scene::ISceneManager, but I dont know what makes the difference.

In the end, the map should be a simple map to put big cubes fur buildings on, or small for worker etc, for a simple rts-demo.

Can anyone help me with this? Thanks
horvatha4
Posts: 12
Joined: Sun Feb 23, 2014 6:03 am
Location: Rheiland-Pfalz, Germany
Contact:

Re: Terrain-Grid, best way

Post by horvatha4 »

Hi!
I would just recommend two thing:
-The irr::scene::ITerrainSceneNode Class may a good choose for you, if you want a heighfield or terrain like scene-node. It has a build-in LOD system witch can save your computer a lot of count.
-If you want big and small cubes then create and use them ( I know everything is realive! ;-) ). You can use the build in collision callbacks with these cubes.

Good Luck!
Arpi
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Terrain-Grid, best way

Post by hendu »

1. You are making 10k draw calls per frame. Ouch.
2. You are doing repeated pointless cpu work every frame. Ouch.

The mentioned terrain mesh/node can create such in a much more efficient way, as can the hillplane mesh.
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Re: Terrain-Grid, best way

Post by thetemplar »

Hey,
first of all, thanks for the help!
@hendu: Yeah, "ouchy" code, was more to show what i want to do.

I so created a TerrainSceneNode*

Code: Select all

scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("../images/gray.bmp", 0, 777);
I looks nice and runs as fast as i expected it.
But can I access the "fields" or something? Or do I need a seperate way to handle information.

My idea were those tiles, containing information like "isOccupied" and so on, so that i can check placements of new items and later do pathfinding.

To ask straight forward:
I want to add a house aka cube now to the surface. How do I do that with checking the heights and if something is already there? Sadly, i neither found a sourcecode nor a tutorial for such thing.
polylux
Posts: 267
Joined: Thu Aug 27, 2009 12:39 pm
Location: EU

Re: Terrain-Grid, best way

Post by polylux »

thetemplar wrote:Hey,
first of all, thanks for the help!
@hendu: Yeah, "ouchy" code, was more to show what i want to do.

I so created a TerrainSceneNode*

Code: Select all

scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("../images/gray.bmp", 0, 777);
I looks nice and runs as fast as i expected it.
But can I access the "fields" or something? Or do I need a seperate way to handle information.

My idea were those tiles, containing information like "isOccupied" and so on, so that i can check placements of new items and later do pathfinding.

To ask straight forward:
I want to add a house aka cube now to the surface. How do I do that with checking the heights and if something is already there? Sadly, i neither found a sourcecode nor a tutorial for such thing.
I get the impression here that you kinda 'blend' graphics and game-logic here. Dunno about others but I like to keep things separated (as far as possible): The game logic keeping states, checking conditions, 'game steering' and the graphics part that's rather the component that visualizes what's going on in the "logic machine".
Following that I don't think it's a good idea to blindly hit-test in the visualization and if smth's there, then let's see. Keep a representation of all your fields in your game logic, Make a data structure representing such a field, with members to store a unit occupying it, a house that's built on it, etc. Instantiate them in some 100x100 container and then use this information to visualize the game state and render it.
If the user then clicks in a field, the only thing your render component does, is notifying the game logic what field was clicked. Handle accordingly. :)

Hope you get the idea and that it's useful,
p.
beer->setMotivationCallback(this);
horvatha4
Posts: 12
Joined: Sun Feb 23, 2014 6:03 am
Location: Rheiland-Pfalz, Germany
Contact:

Re: Terrain-Grid, best way

Post by horvatha4 »

Hi again!
This site is may interesting for you: http://irrrpgbuilder.sourceforge.net/
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Re: Terrain-Grid, best way

Post by thetemplar »

@polylus: Thanks. After reading this, i sort of hit myself for not thinking of this before. Indeed, I wanted to mix rendering and logic, but sure, it is better to have it

@horvatha4: Thanks, I will check it out!

Thanks for the help
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Re: Terrain-Grid, best way

Post by thetemplar »

One last thing:

I now have a array for my game logic and my drawn terrain.

How do i get the point where the mouse is on the surface of the terrain? I have a RTS-like look-around camera, so the "click" can be anywhere, how can I determin the point where the mouse is?
horvatha4
Posts: 12
Joined: Sun Feb 23, 2014 6:03 am
Location: Rheiland-Pfalz, Germany
Contact:

Re: Terrain-Grid, best way

Post by horvatha4 »

This Class is know that: irr::scene::ISceneCollisionManager
Before you ask one more thing - please - read the official Docs, test the demos and take a look at here : Board index ‹ Irrlicht Programming ‹ Code Snippets
The site what I recommented is a complet RTS-game-builder program! A lot of thing is ready to use. ;-)
Good Luck!

Arpi
polylux
Posts: 267
Joined: Thu Aug 27, 2009 12:39 pm
Location: EU

Re: Terrain-Grid, best way

Post by polylux »

Yeah, you gotta use the ISceneCollisionManager, more specificly this method. That creates a ray from your camera over the mouse position which you can then collide with your world.
I'd lay out eg. a plane at the level of your actual game port. You can then collide the plane with the "aim ray" and calculate back what field it has hit.

Cheers,
p.
beer->setMotivationCallback(this);
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Re: Terrain-Grid, best way

Post by thetemplar »

Yes, thanks, I only wanted that keyword so I know what to looking for.

Thanks, great community!
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Re: Terrain-Grid, best way

Post by thetemplar »

Ok, sorry to ask again...

I got my terrain with a selector

Code: Select all

 
scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("../images/gray.bmp");
ITriangleSelector* selector = smgr->createTriangleSelector(((IMeshSceneNode*)terrain)->getMesh(), terrain);
terrain->setTriangleSelector(selector);
 
while (device->run() && driver)
{
[...]
 
and my function to check collisions

Code: Select all

 
void GuiHandler::ClickInWorld(irr::SEvent::SMouseInput mouse)
{
    core::line3d<f32> line = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(core::vector2di(mouse.X, mouse.Y));
 
    rays->push_back(line.start); //for drawing the line in the main-loop
    rays->push_back(line.end);
 
    irr::core::vector3df outVec;
    irr::core::triangle3df outTri;
    smgr->getSceneCollisionManager()->getSceneNodeAndCollisionPointFromRay(line, outVec, outTri, 0);
}
but i get no collision. The "line"-ray runs straigt through the terrain.

I saw this problem also in http://irrlicht.sourceforge.net/forum/v ... on+terrain but tbh I do not understand the solution.
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Re: Terrain-Grid, best way

Post by thetemplar »

HA!
I got it after just 5 hours :D

Code: Select all

 
ITriangleSelector* terrainSelector = smgr->createTerrainTriangleSelector(terrain, 0);
 
This does the trick.

Thanks to the site i finally got the solution from: http://irrlichtirc.g0dsoft.com/kat104/3 ... index.html

@Whoever wrote the tutorials: Add a part with the simple terrain creation and collisions to it, i found more people who have this problem.
polylux
Posts: 267
Joined: Thu Aug 27, 2009 12:39 pm
Location: EU

Re: Terrain-Grid, best way

Post by polylux »

Congrats! :)

But as your terrain is completely flat, it'd be computationally faster to use e.g. the "plane approach" I described earlier. Collide this with the ray and convert the coll pos to a field index.

p.
beer->setMotivationCallback(this);
thetemplar
Posts: 8
Joined: Wed Mar 05, 2014 1:26 am

Re: Terrain-Grid, best way

Post by thetemplar »

Now, the terrain was just flat for the first try, i want to have different heigth and mountains and stuff, so this works pretty well.

But thanks for the tipp :-)

Image
Post Reply