BSTerrainHeightmap.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.CoreModules;
  33. using OpenSim.Region.Physics.Manager;
  34. using Nini.Config;
  35. using log4net;
  36. using OpenMetaverse;
  37. namespace OpenSim.Region.Physics.BulletSNPlugin
  38. {
  39. public sealed class BSTerrainHeightmap : BSTerrainPhys
  40. {
  41. static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]";
  42. BulletHeightMapInfo m_mapInfo = null;
  43. // Constructor to build a default, flat heightmap terrain.
  44. public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize)
  45. : base(physicsScene, regionBase, id)
  46. {
  47. Vector3 minTerrainCoords = new Vector3(0f, 0f, BSTerrainManager.HEIGHT_INITIALIZATION - BSTerrainManager.HEIGHT_EQUAL_FUDGE);
  48. Vector3 maxTerrainCoords = new Vector3(regionSize.X, regionSize.Y, BSTerrainManager.HEIGHT_INITIALIZATION);
  49. int totalHeights = (int)maxTerrainCoords.X * (int)maxTerrainCoords.Y;
  50. float[] initialMap = new float[totalHeights];
  51. for (int ii = 0; ii < totalHeights; ii++)
  52. {
  53. initialMap[ii] = BSTerrainManager.HEIGHT_INITIALIZATION;
  54. }
  55. m_mapInfo = new BulletHeightMapInfo(id, initialMap, null);
  56. m_mapInfo.minCoords = minTerrainCoords;
  57. m_mapInfo.maxCoords = maxTerrainCoords;
  58. m_mapInfo.terrainRegionBase = TerrainBase;
  59. // Don't have to free any previous since we just got here.
  60. BuildHeightmapTerrain();
  61. }
  62. // This minCoords and maxCoords passed in give the size of the terrain (min and max Z
  63. // are the high and low points of the heightmap).
  64. public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap,
  65. Vector3 minCoords, Vector3 maxCoords)
  66. : base(physicsScene, regionBase, id)
  67. {
  68. m_mapInfo = new BulletHeightMapInfo(id, initialMap, null);
  69. m_mapInfo.minCoords = minCoords;
  70. m_mapInfo.maxCoords = maxCoords;
  71. m_mapInfo.minZ = minCoords.Z;
  72. m_mapInfo.maxZ = maxCoords.Z;
  73. m_mapInfo.terrainRegionBase = TerrainBase;
  74. // Don't have to free any previous since we just got here.
  75. BuildHeightmapTerrain();
  76. }
  77. public override void Dispose()
  78. {
  79. ReleaseHeightMapTerrain();
  80. }
  81. // Using the information in m_mapInfo, create the physical representation of the heightmap.
  82. private void BuildHeightmapTerrain()
  83. {
  84. m_mapInfo.Ptr = BulletSimAPI.CreateHeightMapInfo2(PhysicsScene.World.ptr, m_mapInfo.ID,
  85. m_mapInfo.minCoords, m_mapInfo.maxCoords,
  86. m_mapInfo.heightMap, BSParam.TerrainCollisionMargin);
  87. // Create the terrain shape from the mapInfo
  88. m_mapInfo.terrainShape = new BulletShape(BulletSimAPI.CreateTerrainShape2(m_mapInfo.Ptr),
  89. BSPhysicsShapeType.SHAPE_TERRAIN);
  90. // The terrain object initial position is at the center of the object
  91. Vector3 centerPos;
  92. centerPos.X = m_mapInfo.minCoords.X + (m_mapInfo.sizeX / 2f);
  93. centerPos.Y = m_mapInfo.minCoords.Y + (m_mapInfo.sizeY / 2f);
  94. centerPos.Z = m_mapInfo.minZ + ((m_mapInfo.maxZ - m_mapInfo.minZ) / 2f - 0.5f);
  95. m_mapInfo.terrainBody = new BulletBody(m_mapInfo.ID,
  96. BulletSimAPI.CreateBodyWithDefaultMotionState2(m_mapInfo.terrainShape.ptr,
  97. m_mapInfo.ID, centerPos, Quaternion.Identity));
  98. // Set current terrain attributes
  99. BulletSimAPI.SetFriction2(m_mapInfo.terrainBody.ptr, BSParam.TerrainFriction);
  100. BulletSimAPI.SetHitFraction2(m_mapInfo.terrainBody.ptr, BSParam.TerrainHitFraction);
  101. BulletSimAPI.SetRestitution2(m_mapInfo.terrainBody.ptr, BSParam.TerrainRestitution);
  102. BulletSimAPI.SetCollisionFlags2(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);
  103. // Return the new terrain to the world of physical objects
  104. BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr, centerPos, Quaternion.Identity);
  105. // redo its bounding box now that it is in the world
  106. BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr);
  107. m_mapInfo.terrainBody.collisionType = CollisionType.Terrain;
  108. m_mapInfo.terrainBody.ApplyCollisionMask();
  109. // Make it so the terrain will not move or be considered for movement.
  110. BulletSimAPI.ForceActivationState2(m_mapInfo.terrainBody.ptr, ActivationState.DISABLE_SIMULATION);
  111. return;
  112. }
  113. // If there is information in m_mapInfo pointing to physical structures, release same.
  114. private void ReleaseHeightMapTerrain()
  115. {
  116. if (m_mapInfo != null)
  117. {
  118. if (m_mapInfo.terrainBody.HasPhysicalBody)
  119. {
  120. BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr);
  121. // Frees both the body and the shape.
  122. BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr);
  123. BulletSimAPI.ReleaseHeightMapInfo2(m_mapInfo.Ptr);
  124. }
  125. }
  126. m_mapInfo = null;
  127. }
  128. // The passed position is relative to the base of the region.
  129. public override float GetTerrainHeightAtXYZ(Vector3 pos)
  130. {
  131. float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
  132. int mapIndex = (int)pos.Y * (int)m_mapInfo.sizeY + (int)pos.X;
  133. try
  134. {
  135. ret = m_mapInfo.heightMap[mapIndex];
  136. }
  137. catch
  138. {
  139. // Sometimes they give us wonky values of X and Y. Give a warning and return something.
  140. PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}",
  141. LogHeader, m_mapInfo.terrainRegionBase, pos);
  142. ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
  143. }
  144. return ret;
  145. }
  146. // The passed position is relative to the base of the region.
  147. public override float GetWaterLevelAtXYZ(Vector3 pos)
  148. {
  149. return PhysicsScene.SimpleWaterLevel;
  150. }
  151. }
  152. }