Height Calculator For Sunburn Terrain

rated by 0 users
This post has 26 Replies | 5 Followers

Top 150 Contributor
Posts 22
SunBurn_Community_Licensee

cemalsensivas:
thanks a lot it works fairly well now I am trying to adopt the lerp method
Just to expand on the alternative approach I metioned of smoothing out normal changes, this is what I was essentially doing in my game class:

if (newPosition != lastPosition)
{
  normal = sunburnTerrainHeightCalc.GetNormal(newPosition));
  normal = new Vector3(MathHelper.Lerp(lastNormal.X, normal.X, 0.1f),
  MathHelper.Lerp(lastNormal.Y, normal.Y, 0.1f),
  MathHelper.Lerp(lastNormal.Z, normal.Z, 0.1f));
  normal.Normalize();
  lastNormal = normal;
}

Its pretty basic but it looked visually ok to me. The 0.1f value may need to be adjusted depending on your terrain scaling and object movement speed though. Also its much simplier than creating an array of normals for each square, locating the four surrounding square normals and using Lerp to factor in each one according to the position required.

Top 25 Contributor
Posts 184
SunBurn_Pro_Licensee

Thanks for this great Class.  I am new to Sunburn, but understand pretty well how the Tank Height Map example works.  I currently am just plugging the code into XNA and testing as a starting point.  I've followed:

In the fields section i added:

   SunburnTerrainHeightCalc sunburnTerrainHeightCalc;

In the LoadContent section i added:

Load<Texture2D>("terrain2");
   sunburnTerrainHeightCalc = new SunburnTerrainHeightCalc();
   sunburnTerrainHeightCalc.LoadHeightMap(terrainTexture, 0.18f, 1.0f, 1000f);

Top 25 Contributor
Posts 184
SunBurn_Pro_Licensee

I am currently just trying to add the class to the XNA example...I've followed:

In the fields section i added:

   SunburnTerrainHeightCalc sunburnTerrainHeightCalc;

In the LoadContent section i added:

   Texture2D terrainTexture = Content.Load<Texture2D>("terrain2");
   sunburnTerrainHeightCalc = new SunburnTerrainHeightCalc();
   sunburnTerrainHeightCalc.LoadHeightMap(terrainTexture, 0.18f, 1.0f, 1000f);

I am excited to use this class in Sunburn as I need to place things along the terrain programmatically and am hoping this will get me half way there.  The above works with no errors, so I think I have that implementation correct.

I go in and add the line:

   float heightAtPosition = SunburnTerrainHeightCalc.GetHeight(position); 

to the HandleInput method of the Tank.cs and I get...

Error 1 An object reference is required for the non-static field, method, or property 'TanksOnAHeightmap.SunburnTerrainHeightCalc.GetHeight(Microsoft.Xna.Framework.Vector3)' C:\Users\*******\Desktop\dev\SB\Day 1\Terrain\TankOnAHeightMapSample_4_0\TankOnAHeightmap\TankOnAHeightmap\Tank.cs 218 42 TankOnAHeightmapWindows

Top 150 Contributor
Posts 22
SunBurn_Community_Licensee

It sounds like your passing the SunburnTerrainHeightCalc object into your method wrong or referencing it wrong.

First have you created the SunburnTerrainHeightCalc object within your Tank class or within the main game class?

If you have created it in the game class you will need to modify your Tank HandleInput method to include in the SunburnTerrainHeightCalc object like this:

 public void HandleInput( ...  , SunburnTerrainHeightCalc sunburnTerrainHeightCalc)
{
   ...
}

If you have created it in the Tank class you will be able to make reference to the object directly in your HandleInput method. As you are probably just testing this at the moment it will be fine here, but you might want to move this to the game class eventually as you will probably require height data for other objects in your game.

Once you are sure your HandleInput method can access the object then also make sure your method is referencing the object itself and not the class type. I noticed you posted SunburnTerrainHeightCalc on your height calculation line but earlier you called your object sunburnTerrainHeightCalc instead. Basically you can name the object as you wish when you declare it in your fields and when you list it as an argument for your method so make sure you use the correct name when using the GetHeight method.

Hope that helps.

Top 25 Contributor
Posts 184
SunBurn_Pro_Licensee

Ok figured it out...I had to modify the code and make some things static i.e. these lines:

       static float[,] heights;
        static float maxHeight;
        static float tilingScale;
        static float worldScale;
        static Vector3 heightmapStartingPosition;
        static float heightmapWidth;
        static float heightmapHeight;
        static float terrainCentreOffset;

 and...

         public static float GetHeight(Vector3 position) 

Seems to be outputting correcctly.  Now to play with it in SunBurn!

Cheers!

Top 150 Contributor
Posts 22
SunBurn_Community_Licensee

Making it static would work as well but I'm sure you just needed to change some simple object references to the names you used (it might have worked by changing the object reference to the lower case version you declared). I use this class in my Sunburn games and I haven't changed anything to static so I know it works without those modifications.

Top 25 Contributor
Posts 184
SunBurn_Pro_Licensee

 

I actually didn't get to see your post until after I posted my fix.  Is there any downside/cost to using the static option?  I would prefer to use it as you designed it so I will go back and look at some of the things you suggested and post any solution I find here.

You were correct to assume my first concern was just to see it working, so I could then strip back the Tank Colliding on Terrain example and play with everything in SunBurn.
I should clarify that me being new to SunBurn my first concern is to get my character walking/colliding on terrain and for that terrain to be a SunBurn terrain.  I had found your post here http://www.synapsegaming.com/forums/p/2834/16971.aspx and was happy to discover this Class as I will use it at some point as my character interacts with the terrain.
I suppose I wanted to clarify/ask if the tank example is still the best example on a character walking on a terrain?
Also does anyone know of a tutorial/package from the community that walks a person through using the SB character controller and restructuring the Tank Terrain example to work as a SB terrain?
Thank you for your help!

 

Top 150 Contributor
Posts 22
SunBurn_Community_Licensee

I don't think there would be an issue using it as a static method. I just wanted to make it clear for others that the class works and the modifications weren't required.

When I wrote the class it was for an earlier version of Sunburn. I believe that there are some changes to the Sunburn terrain in the latest versions but it appeared to work correctly still when I tried it the other week. Before the terrain tiled infinitely whereas the number of repeats can be set now. However a correct height will still be given for visible areas.

I also believe Sunburn now has some built-in collision detection for terrain that didn't exist before. It should be possible to place objects on the terrain using this although I'm not sure if there is any sample code demonstrating this and it isn't something I have tried before.

Top 25 Contributor
Posts 184
SunBurn_Pro_Licensee

Thanks for the reply, I've started 2 forum topics on searching for help on finding out how Terrain collision works.  Hopefully I will get somewhere there.  I don't want to ask here as I know off topic.

http://www.synapsegaming.com/forums/t/3119.aspx

http://www.synapsegaming.com/forums/t/3118.aspx

Hopefully it will help pave the way for some other new users if I ask some noob questions.  I've searched and can't find anything on it.

Top 25 Contributor
Posts 186
SunBurn_Community_Licensee

Hello ! 

To get the height of a specific point at a terrain:

1. Should I "RayCast" a ray Vector3.Down to the terrain and get the distance ?

2. How could I possibly get the normal data out of the terrain to "align" my object to the terrain surface manually (without physics - at least, I will just modify orientation to look like following)... ? (rather than manually doing it - like squarebananas)...

EDIT: Maybe one could do the RayCast at current point, and another 2 points of an triangle, and then using the heights, calculate the normal...

Mmmm or simply we could keep track of the last 2 points and using that triangle -> plane -> normal :)

BR

CS

Top 150 Contributor
Posts 22
SunBurn_Community_Licensee

I'm not sure I entirely understand the questions but i will try to respond to them.

1) If you want the vertical distance from a position above the terrain to the terrain itself couldn't you just use GetHeight to get the terrain height then subtract this from the position's Y value to get the vertical distance.

2) I'm not sure what you want to do different to using the GetNormal method. Perhaps it would be easier to describe why the GetNormal method isn't appropriate to understand the issue you have.

Top 25 Contributor
Posts 186
SunBurn_Community_Licensee

I just was not aware of it until yet :) But it feels great now.. Thanks :)

Page 2 of 2 (27 items) < Previous 1 2 | RSS