Irr1.8c++| How can i project a useable grid on to my Terrain

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.
Ultraporing
Posts: 34
Joined: Tue Nov 06, 2007 7:22 pm
Location: Würzburg, Germany

Irr1.8c++| How can i project a useable grid on to my Terrain

Post by Ultraporing »

Hello forum,
I like to put a enable/disable able grid on top of my terrain and let the player put objects centered in the cells.
But I don't know how to do this. Can you please give me some tips where and how to start?

Thank you :D

Image to Illustrate what I mean:
Image
STTrife
Posts: 33
Joined: Sun Jan 13, 2013 3:43 pm

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by STTrife »

it's very hard to understand what you need help with exactly.

If you want to know for each cell if players can or cannot place something, you could for example create a second 2darray of booleans, the same size as your heightfield. Then set the values where players can create something to true. Then if players want to place something at (x,y), check the boolean-2darray at (x,y) to see if they can.

btw the size of your boolean-area should be 1 less in both with and height. The values in a heightfield represent the 'corners' of your cells. So if you have a 25x25 heightfield, you have actually 24x24 cells, so you need a 24x24 area of bools.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by CuteAlien »

And if it's about he display of a grid then check-out this thread: http://irrlicht.sourceforge.net/forum/v ... 8&start=60
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Ultraporing
Posts: 34
Joined: Tue Nov 06, 2007 7:22 pm
Location: Würzburg, Germany

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by Ultraporing »

Thank you for the answers but i need help with creating the grid and putting it directly ontop of my terrain, so the grid follows the terrain up & down like in sims or rts games. And the player is then able to put objects in the cells.
@CuteAlien: I tried this grid but its only a flat one and dosnt follow the terrain. I already asked in this thread but noone seems to understand my question or just can't answer it.
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by chronologicaldot »

I think I get what you want.

Here's the simple solution: make two two textures - the one you normally display and one that has horizontal and vertical lines crossing it (or at whatever angle you need). Now granted, the resolution of the line will change when you zoom in and out of the terrain, but at least the lines will be there. And heck, you could always make another texture with thicker lines. You could even draw the lines on the texture when it loads (so you don't have to keep making duplicates of your files).
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by CuteAlien »

@Ultraporing: You are right, that one is only flat. But it also comes with source... adapting just the y-values of the points is still easier than writing one from scratch. So it might be a start - there's rarely the perfect code out there which you can just use for your project.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
STTrife
Posts: 33
Joined: Sun Jan 13, 2013 3:43 pm

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by STTrife »

I already asked in this thread but noone seems to understand my question or just can't answer it.
Well to be honest, I'm pretty sure that is caused by how you ask your question. It's very vague, and very big and you don't say what exactly you are are tying to do, what you already tried and where you fail.

I think the first part is answered, but for the second part, how to let users place objects on the grid... there are so many ways to do it and many 'problems' you have to solve, so you need to be more specific on how you want to the user to place objects, and what parts of the implementation is giving you a hard time (via mouse? see http://irrlicht.sourceforge.net/forum/v ... hp?t=38725)
Ultraporing
Posts: 34
Joined: Tue Nov 06, 2007 7:22 pm
Location: Würzburg, Germany

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by Ultraporing »

@STTrife thanks for the raycasting link thats the way i like to place the objects. The only hard time I have is with the grid, because I have no idea how to put it at the correct Y value corrosponding to the terrain height. A flat grid can i probably manage somehow but i dont know how to get the height of the terrain at the corners of the cells and place them right. Just as a Texture isnt good either because I like to show the grid only on certain areas of the map.

@CuteAlien: My main problem is I dont know how to adapt the y-values of the points, and I have no clue how to change CGridSceneNode so it does what i want.

Well it looks like i have to write my own grid class, thank you for your time and answers. The Raycasting will be most helpfull.
Last question: Dosn't Raycasting for every point of the Line take a lot of resources? I'll try now to create the grid by constructing the cells with 4 lines and repeate this for the with and height of the grid.
Last edited by Ultraporing on Fri Feb 01, 2013 12:02 pm, edited 1 time in total.
STTrife
Posts: 33
Joined: Sun Jan 13, 2013 3:43 pm

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by STTrife »

A way to do it is to copy/paste the code from CGridSceneNode into your own class. then change the RegenerateGrid() function to take the height into account. You need to pass your heightgrid to the scenenode in the constructor, so it can read from it. And you need to tell the CGridSceneNode (or however you are going to call it) at which place in your heightmap it should read the heights, and set the y-coordinate to that height.

Also I see that the CGridSceneNode really assumes a flat terrain, because it draws line from the start of the grid to the end. What you need is to make linepieces for each cell in your heightmap. So you do need to do a significant amount of work yourself. Don't know if it will be much use reusing the CGridSceneNode literally. At least you can look at the things they do in that class to see how to make your own gridscene node.

A completely different way of doing it is to make a second heightfield, with a square as repeating textures. The inside of that square should be transparant. Then put that second heightfield just a little bit heigher than your original heightfield and you can see raster lines over your heightmap (this was already suggested before too).
Last edited by STTrife on Fri Feb 01, 2013 12:04 pm, edited 1 time in total.
Ultraporing
Posts: 34
Joined: Tue Nov 06, 2007 7:22 pm
Location: Würzburg, Germany

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by Ultraporing »

STTrife wrote:Probably the easiest way is to copy/paste the code from CGridSceneNode into your own class. then change the RegenerateGrid() function to take the height into account. You need to pass your heightgrid to the scenenode in the constructor, so it can read from it. And you need to tell the CGridSceneNode (or however you are going to call it) at which place in your heightmap it should read the heights, and set the y-coordinate to that height.
Thanks i'll try this, and sorry for not being able to communicate my problem in a understandable way.
STTrife
Posts: 33
Joined: Sun Jan 13, 2013 3:43 pm

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by STTrife »

I edited my post, CSceneNode needs more work then I thought...
Ultraporing
Posts: 34
Joined: Tue Nov 06, 2007 7:22 pm
Location: Würzburg, Germany

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by Ultraporing »

Can I somehow get the height of a point on the terrain directly or do i have to use Raycasts?
STTrife
Posts: 33
Joined: Sun Jan 13, 2013 3:43 pm

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by STTrife »

There shouldn't be a need for a raycast. You can use the data you created the TerrainSceneNode with, or you can use the getHeight (f32 x, f32 y) function of the TerrainSceneNode.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by hendu »

Bumping in to confuse even more ;)

I'd use a shader. In your vert shader for the terrain, calculate the world space position, and check if its modulo is ok. Then pass this ok as a varying.

In the frag shader, just draw black if the varying is 1, otherwise the terrain normally.

Or in pseudocode:

if (pos.xz % 32 < 1)
draw line here
else
draw terrain normally
Ultraporing
Posts: 34
Joined: Tue Nov 06, 2007 7:22 pm
Location: Würzburg, Germany

Re: Irr1.8c++| How can i project a useable grid on to my Ter

Post by Ultraporing »

I just want to give a fast status update.
Im drawing the cells now fine :), I also removed the terrain for testing purposes.

Image

next up the grid :)
Post Reply