BSTerrainHeightmap.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyrightD
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework;
  32. using OpenSim.Region.PhysicsModules.SharedBase;
  33. using Nini.Config;
  34. using log4net;
  35. using OpenMetaverse;
  36. namespace OpenSim.Region.PhysicsModule.BulletS
  37. {
  38. public sealed class BSTerrainHeightmap : BSTerrainPhys
  39. {
  40. static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]";
  41. BulletHMapInfo m_mapInfo = null;
  42. // Constructor to build a default, flat heightmap terrain.
  43. public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize)
  44. : base(physicsScene, regionBase, id)
  45. {
  46. Vector3 minTerrainCoords = new Vector3(0f, 0f, BSTerrainManager.HEIGHT_INITIALIZATION - BSTerrainManager.HEIGHT_EQUAL_FUDGE);
  47. Vector3 maxTerrainCoords = new Vector3(regionSize.X, regionSize.Y, BSTerrainManager.HEIGHT_INITIALIZATION);
  48. int totalHeights = (int)maxTerrainCoords.X * (int)maxTerrainCoords.Y;
  49. float[] initialMap = new float[totalHeights];
  50. for (int ii = 0; ii < totalHeights; ii++)
  51. {
  52. initialMap[ii] = BSTerrainManager.HEIGHT_INITIALIZATION;
  53. }
  54. m_mapInfo = new BulletHMapInfo(id, initialMap, regionSize.X, regionSize.Y);
  55. m_mapInfo.minCoords = minTerrainCoords;
  56. m_mapInfo.maxCoords = maxTerrainCoords;
  57. m_mapInfo.terrainRegionBase = TerrainBase;
  58. // Don't have to free any previous since we just got here.
  59. BuildHeightmapTerrain();
  60. }
  61. // This minCoords and maxCoords passed in give the size of the terrain (min and max Z
  62. // are the high and low points of the heightmap).
  63. public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap,
  64. Vector3 minCoords, Vector3 maxCoords)
  65. : base(physicsScene, regionBase, id)
  66. {
  67. m_mapInfo = new BulletHMapInfo(id, initialMap, maxCoords.X - minCoords.X, maxCoords.Y - minCoords.Y);
  68. m_mapInfo.minCoords = minCoords;
  69. m_mapInfo.maxCoords = maxCoords;
  70. m_mapInfo.minZ = minCoords.Z;
  71. m_mapInfo.maxZ = maxCoords.Z;
  72. m_mapInfo.terrainRegionBase = TerrainBase;
  73. // Don't have to free any previous since we just got here.
  74. BuildHeightmapTerrain();
  75. }
  76. public override void Dispose()
  77. {
  78. ReleaseHeightMapTerrain();
  79. }
  80. // Using the information in m_mapInfo, create the physical representation of the heightmap.
  81. private void BuildHeightmapTerrain()
  82. {
  83. // Create the terrain shape from the mapInfo
  84. m_mapInfo.terrainShape = m_physicsScene.PE.CreateTerrainShape( m_mapInfo.ID,
  85. new Vector3(m_mapInfo.sizeX, m_mapInfo.sizeY, 0), m_mapInfo.minZ, m_mapInfo.maxZ,
  86. m_mapInfo.heightMap, 1f, BSParam.TerrainCollisionMargin);
  87. // The terrain object initial position is at the center of the object
  88. Vector3 centerPos;
  89. centerPos.X = m_mapInfo.minCoords.X + (m_mapInfo.sizeX / 2f);
  90. centerPos.Y = m_mapInfo.minCoords.Y + (m_mapInfo.sizeY / 2f);
  91. centerPos.Z = m_mapInfo.minZ + ((m_mapInfo.maxZ - m_mapInfo.minZ) / 2f);
  92. m_mapInfo.terrainBody = m_physicsScene.PE.CreateBodyWithDefaultMotionState(m_mapInfo.terrainShape,
  93. m_mapInfo.ID, centerPos, Quaternion.Identity);
  94. // Set current terrain attributes
  95. m_physicsScene.PE.SetFriction(m_mapInfo.terrainBody, BSParam.TerrainFriction);
  96. m_physicsScene.PE.SetHitFraction(m_mapInfo.terrainBody, BSParam.TerrainHitFraction);
  97. m_physicsScene.PE.SetRestitution(m_mapInfo.terrainBody, BSParam.TerrainRestitution);
  98. m_physicsScene.PE.SetCollisionFlags(m_mapInfo.terrainBody, CollisionFlags.CF_STATIC_OBJECT);
  99. m_mapInfo.terrainBody.collisionType = CollisionType.Terrain;
  100. // Return the new terrain to the world of physical objects
  101. m_physicsScene.PE.AddObjectToWorld(m_physicsScene.World, m_mapInfo.terrainBody);
  102. // redo its bounding box now that it is in the world
  103. m_physicsScene.PE.UpdateSingleAabb(m_physicsScene.World, m_mapInfo.terrainBody);
  104. // Make it so the terrain will not move or be considered for movement.
  105. m_physicsScene.PE.ForceActivationState(m_mapInfo.terrainBody, ActivationState.DISABLE_SIMULATION);
  106. return;
  107. }
  108. // If there is information in m_mapInfo pointing to physical structures, release same.
  109. private void ReleaseHeightMapTerrain()
  110. {
  111. if (m_mapInfo != null)
  112. {
  113. if (m_mapInfo.terrainBody.HasPhysicalBody)
  114. {
  115. m_physicsScene.PE.RemoveObjectFromWorld(m_physicsScene.World, m_mapInfo.terrainBody);
  116. // Frees both the body and the shape.
  117. m_physicsScene.PE.DestroyObject(m_physicsScene.World, m_mapInfo.terrainBody);
  118. }
  119. m_mapInfo.Release();
  120. }
  121. m_mapInfo = null;
  122. }
  123. // The passed position is relative to the base of the region.
  124. // There are many assumptions herein that the heightmap increment is 1.
  125. public override float GetTerrainHeightAtXYZ(Vector3 pos)
  126. {
  127. float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
  128. try {
  129. int baseX = (int)pos.X;
  130. int baseY = (int)pos.Y;
  131. int maxX = (int)m_mapInfo.sizeX;
  132. int maxY = (int)m_mapInfo.sizeY;
  133. float diffX = pos.X - (float)baseX;
  134. float diffY = pos.Y - (float)baseY;
  135. float mapHeight1 = m_mapInfo.heightMap[baseY * maxY + baseX];
  136. float mapHeight2 = m_mapInfo.heightMap[Math.Min(baseY + 1, maxY - 1) * maxY + baseX];
  137. float mapHeight3 = m_mapInfo.heightMap[baseY * maxY + Math.Min(baseX + 1, maxX - 1)];
  138. float mapHeight4 = m_mapInfo.heightMap[Math.Min(baseY + 1, maxY - 1) * maxY + Math.Min(baseX + 1, maxX - 1)];
  139. float Xrise = (mapHeight4 - mapHeight3) * diffX;
  140. float Yrise = (mapHeight2 - mapHeight1) * diffY;
  141. ret = mapHeight1 + ((Xrise + Yrise) / 2f);
  142. // m_physicsScene.DetailLog("{0},BSTerrainHeightMap,GetTerrainHeightAtXYZ,pos={1},{2}/{3}/{4}/{5},ret={6}",
  143. // BSScene.DetailLogZero, pos, mapHeight1, mapHeight2, mapHeight3, mapHeight4, ret);
  144. }
  145. catch
  146. {
  147. // Sometimes they give us wonky values of X and Y. Give a warning and return something.
  148. m_physicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}",
  149. LogHeader, m_mapInfo.terrainRegionBase, pos);
  150. ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
  151. }
  152. return ret;
  153. }
  154. // The passed position is relative to the base of the region.
  155. public override float GetWaterLevelAtXYZ(Vector3 pos)
  156. {
  157. return m_physicsScene.SimpleWaterLevel;
  158. }
  159. }
  160. }