Alter Terrain via Script
by Trigger · in Torque 3D Professional · 01/15/2010 (10:30 am) · 24 replies
Hi all
Would someone be able to point me in the right direction in order to modify the terrain via Script...
I read in a previous post that it was possible to deform the terrain (I.E lower the height) via a script, maybe using similar code to how the Terrain Editor works?
After looking over the scripts for a couple of days I haven't really managed to get much closer, Is this something I have miss read, is it even possible with the binary only version?
I found some things inside of terrainEditor.ed.cs and I have tried to setup breakpoints within Torsion to try get it to break when I click the terrain to, try and follow through what is going on, with no luck..
So far I just found the code to setup the Brush via code - ETerrainEditor.switchAction( raiseHeight );
What I am looking for is a method that I may pass a set of coordinates, some deformation properties (height width etc..) and probably the terrain block to modify?
being new to Torque I may be way over simplifying this...
Thanks in advance!
Would someone be able to point me in the right direction in order to modify the terrain via Script...
I read in a previous post that it was possible to deform the terrain (I.E lower the height) via a script, maybe using similar code to how the Terrain Editor works?
After looking over the scripts for a couple of days I haven't really managed to get much closer, Is this something I have miss read, is it even possible with the binary only version?
I found some things inside of terrainEditor.ed.cs and I have tried to setup breakpoints within Torsion to try get it to break when I click the terrain to, try and follow through what is going on, with no luck..
So far I just found the code to setup the Brush via code - ETerrainEditor.switchAction( raiseHeight );
What I am looking for is a method that I may pass a set of coordinates, some deformation properties (height width etc..) and probably the terrain block to modify?
being new to Torque I may be way over simplifying this...
Thanks in advance!
About the author
Ťō Бĕťťēŕ Ũňđĕŕśŧāńđ Ŝŏmėŧħĩńĝ, Ŧřŷ Ŧŏ Čħāńģě Ĭŧ
#2
it was this thread I had read: http://www.torquepowered.com/community/forums/viewthread/108019
Tom did mention it could require some C++ changes (for a network multiplayer?) but it may also be possible via Torque Script alone.
It's not a multiplayer game so I am not so bothered about that side of it, but up to now I haven't seen any code that seems relevant to do this with.
Thank you for the reply :)
01/15/2010 (3:16 pm)
Not at the moment no, but if that is going to be the only way, then I am going to have to look to upgrade.it was this thread I had read: http://www.torquepowered.com/community/forums/viewthread/108019
Tom did mention it could require some C++ changes (for a network multiplayer?) but it may also be possible via Torque Script alone.
It's not a multiplayer game so I am not so bothered about that side of it, but up to now I haven't seen any code that seems relevant to do this with.
Thank you for the reply :)
#3
01/18/2010 (12:34 pm)
You might shop around the community for someone who has the source to make the changes for you and send you an updated binary. Although I am not sure how the licensing affects this. Maybe someone from GG can answer that.
#4
It's ok, I am going to purchase the source tonight or tomorrow, it's probably going to be a whole lot more useful in the future.
I have been playing with some TS to try and get the editor version working in my own script ( new TerrainEditor(ETerrainDeformer) ) and I made a little progress, but I hit a problem when I tried to setBrushPos(), torque would just crash, I need to see what is happening in the Source...
Am I right in saying there is a separate forums just for the Source?
I will try and port the old terrain Deformer resource and see if I can get that working, it may be a nice little project to get my head round it all.
01/18/2010 (2:53 pm)
Hi again, It's ok, I am going to purchase the source tonight or tomorrow, it's probably going to be a whole lot more useful in the future.
I have been playing with some TS to try and get the editor version working in my own script ( new TerrainEditor(ETerrainDeformer) ) and I made a little progress, but I hit a problem when I tried to setBrushPos(), torque would just crash, I need to see what is happening in the Source...
Am I right in saying there is a separate forums just for the Source?
I will try and port the old terrain Deformer resource and see if I can get that working, it may be a nice little project to get my head round it all.
#5
01/18/2010 (3:00 pm)
Purchased! A couple of Stella's made that somewhat easier ;)
#6
I have ported some of the code over, used some of the updated editor functions where Ron originally took them from, unfortunately right now I had to leave out all the decal updates etc... but I am going to look and try get all these back on over the weekend.
Still got a couple of the original problems I.E "Cannot change namespace parent linkage of myDeformer from ItemData to myDeformer" and the weird error about lifetime has to be between 1 and 4096 or whatever, debugged the code from start to finish and can't seem to track this down, I will get it though!
Don't know what the namespace error is about yet, only been working with C++ a week so I will have to look into this some more, unless anyone has any ideas?
Anyhow, here is a screen shot of it deforming at a depth, width and height of 1, before I updated all the worldToGrid, gridToWorld methods etc... setting all the values made no difference, and instead I just got a 200ft deep hole in the terrain!
After I fix the other errors I will try and integrate Daniel's "Sphere" deformer ;)
01/22/2010 (3:53 pm)
managed to make a little progress, using this resource:www.torquepowered.com/community/resources/view/13884I have ported some of the code over, used some of the updated editor functions where Ron originally took them from, unfortunately right now I had to leave out all the decal updates etc... but I am going to look and try get all these back on over the weekend.
Still got a couple of the original problems I.E "Cannot change namespace parent linkage of myDeformer from ItemData to myDeformer" and the weird error about lifetime has to be between 1 and 4096 or whatever, debugged the code from start to finish and can't seem to track this down, I will get it though!
Don't know what the namespace error is about yet, only been working with C++ a week so I will have to look into this some more, unless anyone has any ideas?
Anyhow, here is a screen shot of it deforming at a depth, width and height of 1, before I updated all the worldToGrid, gridToWorld methods etc... setting all the values made no difference, and instead I just got a 200ft deep hole in the terrain!
After I fix the other errors I will try and integrate Daniel's "Sphere" deformer ;)
#7
So I managed to fix a couple of issues I was having, but then when I came to deform the terrain it was just deforming in some really weird ways.
So Sunday I decided to just start a new deformer from scratch, and I have managed to get it all working ok, I can now set the width, height, length and depth of the deformer and call a "doDeform(%pos)" method, all from Torque Script and it works pretty nice.
But I have a question, the C++ code for my "LowerGridHeight" goes a little something like this:
then once I finish setting the height for the size of deform I call "gridUpdateComplete()" that does the following:
Now every time I deform a position I get this very small pause, if I were to deform let's say by dragging an item over the terrain it would be "Jerky" ...
My Question is how does the TerrainEditor manage to do this, and do it smoothly?
The only thing I can see is that MAY be a clue is:
Can anyone help?
Thanks in advance...
01/25/2010 (1:45 pm)
Back again!So I managed to fix a couple of issues I was having, but then when I came to deform the terrain it was just deforming in some really weird ways.
So Sunday I decided to just start a new deformer from scratch, and I have managed to get it all working ok, I can now set the width, height, length and depth of the deformer and call a "doDeform(%pos)" method, all from Torque Script and it works pretty nice.
But I have a question, the C++ code for my "LowerGridHeight" goes a little something like this:
for (updateWidth = 0; updateWidth <= mWidth; updateWidth++)
{
// Set at the beggining of the loop so we dont set a bad value at the end
mGridPoint.gridPos.x = mOriginalX;
// Get the current grid height
mCurrentGridPointHeight = getGridHeight(mGridPoint);
// Set our new deph for this grid point
setGridHeight(mGridPoint, mCurrentGridPointHeight - mDepth);
for (updateLength = 0; updateLength <= mLength; updateLength++)
{
// Now move to the next grid X position
mGridPoint.gridPos.x++;
// Get the height of the grid point
mCurrentGridPointHeight = getGridHeight(mGridPoint);
// And set out new depth
setGridHeight(mGridPoint, mCurrentGridPointHeight - mDepth);
}
// Move to the next Y position to deform
mGridPoint.gridPos.y++;
}then once I finish setting the height for the size of deform I call "gridUpdateComplete()" that does the following:
mTerrainBlock->updateGrid(mGridUpdateMin, mGridUpdateMax, true);
Now every time I deform a position I get this very small pause, if I were to deform let's say by dragging an item over the terrain it would be "Jerky" ...
My Question is how does the TerrainEditor manage to do this, and do it smoothly?
The only thing I can see is that MAY be a clue is:
// process on ticks - every 30th of a second. Sim::postEvent(this, new TerrainProcessActionEvent(mMouseDownSeq), Sim::getCurrentTime() + 30);
Can anyone help?
Thanks in advance...
#8
I am really excited about your progress (I am the original guy whose post you read about terraforming). I haven't purchased the source yet. I would love to incorporate your method when I do, if you don't mind. Thanks for exploring this!
Regards!
01/28/2010 (2:18 pm)
Hey Leon,I am really excited about your progress (I am the original guy whose post you read about terraforming). I haven't purchased the source yet. I would love to incorporate your method when I do, if you don't mind. Thanks for exploring this!
Regards!
#9
It like if you wanted to count 100 times, you would loop 0 through 99 or 1 through 100.
01/28/2010 (4:33 pm)
Not sure if this is affecting what you are seeing, but in your loops, you are starting at zero and incremementing up to mWidth and mLength since you have the <= "less than or equal to" operator. What this is doing is processing each loop one extra time. To fix that, you just need change it to a straight < "less than" operator.It like if you wanted to count 100 times, you would loop 0 through 99 or 1 through 100.
#10
@Saunder Studios - yes, I just realised that tonight haha, I have changed that now.
As an update to where I am at, I have also now added a "setUnderGroundTexture" so when you deform, it will replace a new texture, so say you have sand originally, you could set soil once its deformed, it's working O.K but I just need to get rid of this little jerkiness it has.
Am a software developer during the day so it's not been so bad to pickup C++ so far, still ALOT to learn though, but I am enjoying it a lot so far, and torque is great!
One thing I haven't played with much is threads, and I know a little about thread locks and things being "Thread safe", do any of you guys know how I could make the whole thing a little smoother, possibly executing gridUpdateComplete() and materialUpdateComplete() on a different thread? I think it's unlikely I will be able to but it's worth asking I guess..
To be honest, my previous post was a little incorrect, when I finally got it attached to an object it was pretty smooth, but now I have added the material update in there, it has started to jerk around a little, but the fact I put the terrain tile size down to 0.5 hasn't helped much... (at a tile size of 2 its fine)
The way I have done this so far is to allow you to set any object inside torque as a deform object, so the onTick method will check if the object you selected is colliding with the terrain using a rayCast, it works pretty well and it's on a delay of 5 ticks at the min, I will make it so that can be adjusted through Torque Script as well, I want to make it as generic as possible so it can be used for a wide variety of things, as an example (not for my project) but you could attach it to a grenade, and then have it deform the terrain on explosion or such...
I just can't get my head round what the terrain editor is doing to make it so smooth, you can edit with a brush size of say 100 and its still perfectly smooth, if anyone could suggest how I could get the same performance that would be great...
thank you both for your replies :)
01/28/2010 (4:57 pm)
@Eyesgood - I would love to release this as a resource once I have finished with it!@Saunder Studios - yes, I just realised that tonight haha, I have changed that now.
As an update to where I am at, I have also now added a "setUnderGroundTexture" so when you deform, it will replace a new texture, so say you have sand originally, you could set soil once its deformed, it's working O.K but I just need to get rid of this little jerkiness it has.
Am a software developer during the day so it's not been so bad to pickup C++ so far, still ALOT to learn though, but I am enjoying it a lot so far, and torque is great!
One thing I haven't played with much is threads, and I know a little about thread locks and things being "Thread safe", do any of you guys know how I could make the whole thing a little smoother, possibly executing gridUpdateComplete() and materialUpdateComplete() on a different thread? I think it's unlikely I will be able to but it's worth asking I guess..
To be honest, my previous post was a little incorrect, when I finally got it attached to an object it was pretty smooth, but now I have added the material update in there, it has started to jerk around a little, but the fact I put the terrain tile size down to 0.5 hasn't helped much... (at a tile size of 2 its fine)
The way I have done this so far is to allow you to set any object inside torque as a deform object, so the onTick method will check if the object you selected is colliding with the terrain using a rayCast, it works pretty well and it's on a delay of 5 ticks at the min, I will make it so that can be adjusted through Torque Script as well, I want to make it as generic as possible so it can be used for a wide variety of things, as an example (not for my project) but you could attach it to a grenade, and then have it deform the terrain on explosion or such...
I just can't get my head round what the terrain editor is doing to make it so smooth, you can edit with a brush size of say 100 and its still perfectly smooth, if anyone could suggest how I could get the same performance that would be great...
thank you both for your replies :)
#11
I am a .NET developer by day. I have some c++ experience from the mid 1990's, but I have to get back into it again once I upgrade to the Torque source (I currently work primarily with VB.NET). I am currently working on a concept for a terraforming MMO, so I am really interested in anything you are able to accomplish. I also will be happy to contribute to your efforts once I upgrade from the binary copy I recently purchased. I'll let you know once I am able to get inside the code. Good luck with your efforts. I'm watching intently! :)
Eyesgood
01/28/2010 (8:48 pm)
Leon,I am a .NET developer by day. I have some c++ experience from the mid 1990's, but I have to get back into it again once I upgrade to the Torque source (I currently work primarily with VB.NET). I am currently working on a concept for a terraforming MMO, so I am really interested in anything you are able to accomplish. I also will be happy to contribute to your efforts once I upgrade from the binary copy I recently purchased. I'll let you know once I am able to get inside the code. Good luck with your efforts. I'm watching intently! :)
Eyesgood
#12
Same here, I work with VB.Net mainly!
In the original resource I was working on, that was “network” enabled, as I said previously I had to start again, unfortunately that’s meant I had to drop the network stuff, it was just going to be too much to for me to try and get my head round as a first thing to work on.
If when I have finished you where able to add the required bits in to make it compatible over the net that would be pretty awesome…
I don’t know about you, but when I get home and begin working on some code, hours seem like minutes and the night is over before I make any “real” progress!
Anyhow, it’s nearly weekend *cheers*
I will let you know where I am up to Sunday afternoon; I am going to try work on making it a bit smoother (somehow) and finish up the material change.
I thought if a deform takes place that is rather steep, then a “side projection” texture should be used on the steep tiles, so I will try and get that done also.
The one other thing I want to add in just because I think it would be useful for others is, at the min if you place the object on the terrain to deform, it will deform until that object stops colliding, if you where to use it for a grenade, then it would not work so well, so I am going to try put a lifetime or something similar; 0 = infinite, > 0 = ticks before it stops.
If there is anything else you can think would be useful give me a shout!
Trig.
01/29/2010 (4:00 am)
Hey Eyesgood, Same here, I work with VB.Net mainly!
In the original resource I was working on, that was “network” enabled, as I said previously I had to start again, unfortunately that’s meant I had to drop the network stuff, it was just going to be too much to for me to try and get my head round as a first thing to work on.
If when I have finished you where able to add the required bits in to make it compatible over the net that would be pretty awesome…
I don’t know about you, but when I get home and begin working on some code, hours seem like minutes and the night is over before I make any “real” progress!
Anyhow, it’s nearly weekend *cheers*
I will let you know where I am up to Sunday afternoon; I am going to try work on making it a bit smoother (somehow) and finish up the material change.
I thought if a deform takes place that is rather steep, then a “side projection” texture should be used on the steep tiles, so I will try and get that done also.
The one other thing I want to add in just because I think it would be useful for others is, at the min if you place the object on the terrain to deform, it will deform until that object stops colliding, if you where to use it for a grenade, then it would not work so well, so I am going to try put a lifetime or something similar; 0 = infinite, > 0 = ticks before it stops.
If there is anything else you can think would be useful give me a shout!
Trig.
#13
Didn't get as much as I wanted to over the weekend but hey, what can you do...
Anyway below is a shot of the deformer so far, you can see the health pack is my Deform Object, and the trench it made when I moved it!

You can see from the Pic the deformer is in-between 2 tiles more or less, it looks kinda odd so I am not sure what to do about that yet, I may just make its width 2 in those cases, and maybe put it on an option on or off, we will see.
Although it's not a great texture I used to show it, the texture has changed at the bottom of the hole, its changed from the default grass texture to the included "rocktest" texture.
Still need to sort out the Side Projection texture on the outer tile, but shouldn't be so hard...
As for the performance, on a tile size of 1 or 2 it seems alright, but at 0.5 it's just not great so I will carry on trying to get it a little smoother this week and see how I get on.
So far in Torque you can do;
setTerrainBlock("yourTerrain")
setDeformSize(int_yourHeight, int_yourWidth, int_yourDepth)
setDeformType(int_DeformType) // 1 = Lower height 2 = Raise Height
setDeformObject("anyObjectInTorque")
setUndergroundTexture("yourTextureName")
So I have about 3 maybe 4 more methods to add in then it's pretty much there.
02/02/2010 (2:40 pm)
Hi guys, thought I would do a real quick update...Didn't get as much as I wanted to over the weekend but hey, what can you do...
Anyway below is a shot of the deformer so far, you can see the health pack is my Deform Object, and the trench it made when I moved it!

You can see from the Pic the deformer is in-between 2 tiles more or less, it looks kinda odd so I am not sure what to do about that yet, I may just make its width 2 in those cases, and maybe put it on an option on or off, we will see.
Although it's not a great texture I used to show it, the texture has changed at the bottom of the hole, its changed from the default grass texture to the included "rocktest" texture.
Still need to sort out the Side Projection texture on the outer tile, but shouldn't be so hard...
As for the performance, on a tile size of 1 or 2 it seems alright, but at 0.5 it's just not great so I will carry on trying to get it a little smoother this week and see how I get on.
So far in Torque you can do;
setTerrainBlock("yourTerrain")
setDeformSize(int_yourHeight, int_yourWidth, int_yourDepth)
setDeformType(int_DeformType) // 1 = Lower height 2 = Raise Height
setDeformObject("anyObjectInTorque")
setUndergroundTexture("yourTextureName")
So I have about 3 maybe 4 more methods to add in then it's pretty much there.
#14
It would be very exciting to see terrain drop and rivers fill it up etc.
I would like to see some river enhancements, don't get me wrong already its a super time/life saver but a bit more, .... reality, you know flow control side and bottom textures, more polyhedral control of its body as opposed to blocks, including fine control over all those points.
But I am getting off topic and apologize!!!
02/04/2010 (7:52 am)
Excellent I had the same question (Terrain alteration), can this work, with changes for The Rivers? And or Roads?It would be very exciting to see terrain drop and rivers fill it up etc.
I would like to see some river enhancements, don't get me wrong already its a super time/life saver but a bit more, .... reality, you know flow control side and bottom textures, more polyhedral control of its body as opposed to blocks, including fine control over all those points.
But I am getting off topic and apologize!!!
#15
I got to the point of thinking well if it was filled with water, then you dig some more, you’re going to need to empty it some as well, that’s when I went to sleep :P
The side textures will be in there, I just haven’t played with the material code yet.
“more polyhedral control of its body as opposed to blocks” – If I understand what you mean correctly, that’s what I talked about earlier about the terrain square size, you could implement brush types similar to how the terrain editor works, but that’s not going to solve the “blocky” issue, I think only lowering the Tile size would help looks wise…
I have changed some of the code as I realised a bit more of how the terrain editor works, there isn’t a problem updating a grid, I can update hundreds at a time, the slight pause comes in when you call the gridUpdateComplete() method, so I changed the code round to alter tiles on every tick, and you can now set a deform speed that will set how many ticks occur before update() is called (then reset back to 0).
I got the other methods in so its pretty much there, I just need to tidy the code up and do some error checking so it don’t cause crashes when objects are not set and such..
The lifetime is in there now too, but I haven’t had chance to test it yet.
I have some work to do with the rayCast for the object collision, it’s doing some weird things when the deformer is moved in all directions but once I have that sorted I will get everything tidied up and look into the water thing, like you say it would be cool to;
Throw a grenade, make a hole in the terrain > rain starts to fill the hole with water…
Maybe I could create a water block and set it’s length and width to the deformers X and Y position, could get messy I guess though.. I will have a think.
02/04/2010 (8:45 am)
Yeah I thought a little about water, if it rains, to have it slowly fill up with water. I got to the point of thinking well if it was filled with water, then you dig some more, you’re going to need to empty it some as well, that’s when I went to sleep :P
The side textures will be in there, I just haven’t played with the material code yet.
“more polyhedral control of its body as opposed to blocks” – If I understand what you mean correctly, that’s what I talked about earlier about the terrain square size, you could implement brush types similar to how the terrain editor works, but that’s not going to solve the “blocky” issue, I think only lowering the Tile size would help looks wise…
I have changed some of the code as I realised a bit more of how the terrain editor works, there isn’t a problem updating a grid, I can update hundreds at a time, the slight pause comes in when you call the gridUpdateComplete() method, so I changed the code round to alter tiles on every tick, and you can now set a deform speed that will set how many ticks occur before update() is called (then reset back to 0).
I got the other methods in so its pretty much there, I just need to tidy the code up and do some error checking so it don’t cause crashes when objects are not set and such..
The lifetime is in there now too, but I haven’t had chance to test it yet.
I have some work to do with the rayCast for the object collision, it’s doing some weird things when the deformer is moved in all directions but once I have that sorted I will get everything tidied up and look into the water thing, like you say it would be cool to;
Throw a grenade, make a hole in the terrain > rain starts to fill the hole with water…
Maybe I could create a water block and set it’s length and width to the deformers X and Y position, could get messy I guess though.. I will have a think.
#16
From what I gathered (maybe wrong) its a simple here is a river now leave it alone! tool once the game starts?
And if so how complicated does it appear to make the changes engine wise?
02/05/2010 (8:42 pm)
Polyhedral River control, I know we could lower the square size but I was wondering more about things like say octagonal flow of water, so you could position 8 points per segment instead of 4, (or can you already and I just missed it) that way you could hump water for (well I( don't know what for but you could! ) in the middle of a river or create magma flow easier because it would be less uniform on the surface. And you could make it follow terrain better when its not a 100pct fluid, for things like snow or avalance flow.From what I gathered (maybe wrong) its a simple here is a river now leave it alone! tool once the game starts?
And if so how complicated does it appear to make the changes engine wise?
#17
04/02/2010 (3:51 pm)
anymore updates on this? Really looking forward to in game terraforming with network support.
#18
There will be a large update soon, I am working on a “show off” video at the moment, I will update this thread once it’s finished.
As for the networking side, that was part of the old deformer and due to the slight pauses it was introducing during deformation I decided to drop it.
Since my last update I have been working on converting the whole terrain editor into a deformer, it’s actually pretty much done and it has some really cool features (think everything you have in the terrain editor, plus another 8 or 9 new features)
I need to do some work on the deformer over the next few days to add a couple of things (Why set the terrain via TS when it can just work out what terrain block it hit in code?)
So I will have a look at the old networking code and see what it would take to make it work over a network, but no promises. Worst case the first version won’t support networking but let’s see what happens.
04/03/2010 (2:26 am)
Hi Mark, There will be a large update soon, I am working on a “show off” video at the moment, I will update this thread once it’s finished.
As for the networking side, that was part of the old deformer and due to the slight pauses it was introducing during deformation I decided to drop it.
Since my last update I have been working on converting the whole terrain editor into a deformer, it’s actually pretty much done and it has some really cool features (think everything you have in the terrain editor, plus another 8 or 9 new features)
I need to do some work on the deformer over the next few days to add a couple of things (Why set the terrain via TS when it can just work out what terrain block it hit in code?)
So I will have a look at the old networking code and see what it would take to make it work over a network, but no promises. Worst case the first version won’t support networking but let’s see what happens.
#19
04/06/2010 (1:24 am)
Wow ! Great news ! Hope to see your resource soon :D
#20
10/18/2012 (3:48 pm)
Hate to revive an old thread, but Trigger, I would like to obtain a copy of this from you if possible or even the old resource code. You can hit me up at thevisad @ gmail (dot) com. Thanks!
Torque 3D Owner Sunder Studios
My guess is that you need to expose TerrData::setHeight() as a console function in the engine and then call it from script somehow. Do you have the engine source?