OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
BSTerrainHeightmap.cs
Go to the documentation of this file.
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 
31 using OpenSim.Framework;
32 using OpenSim.Region.Framework;
33 using OpenSim.Region.PhysicsModules.SharedBase;
34 
35 using Nini.Config;
36 using log4net;
37 
38 using OpenMetaverse;
39 
40 namespace OpenSim.Region.PhysicsModule.BulletS
41 {
42 public sealed class BSTerrainHeightmap : BSTerrainPhys
43 {
44  static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]";
45 
46  BulletHMapInfo m_mapInfo = null;
47 
48  // Constructor to build a default, flat heightmap terrain.
49  public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize)
50  : base(physicsScene, regionBase, id)
51  {
52  Vector3 minTerrainCoords = new Vector3(0f, 0f, BSTerrainManager.HEIGHT_INITIALIZATION - BSTerrainManager.HEIGHT_EQUAL_FUDGE);
53  Vector3 maxTerrainCoords = new Vector3(regionSize.X, regionSize.Y, BSTerrainManager.HEIGHT_INITIALIZATION);
54  int totalHeights = (int)maxTerrainCoords.X * (int)maxTerrainCoords.Y;
55  float[] initialMap = new float[totalHeights];
56  for (int ii = 0; ii < totalHeights; ii++)
57  {
58  initialMap[ii] = BSTerrainManager.HEIGHT_INITIALIZATION;
59  }
60  m_mapInfo = new BulletHMapInfo(id, initialMap, regionSize.X, regionSize.Y);
61  m_mapInfo.minCoords = minTerrainCoords;
62  m_mapInfo.maxCoords = maxTerrainCoords;
63  m_mapInfo.terrainRegionBase = TerrainBase;
64  // Don't have to free any previous since we just got here.
65  BuildHeightmapTerrain();
66  }
67 
68  // This minCoords and maxCoords passed in give the size of the terrain (min and max Z
69  // are the high and low points of the heightmap).
70  public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap,
71  Vector3 minCoords, Vector3 maxCoords)
72  : base(physicsScene, regionBase, id)
73  {
74  m_mapInfo = new BulletHMapInfo(id, initialMap, maxCoords.X - minCoords.X, maxCoords.Y - minCoords.Y);
75  m_mapInfo.minCoords = minCoords;
76  m_mapInfo.maxCoords = maxCoords;
77  m_mapInfo.minZ = minCoords.Z;
78  m_mapInfo.maxZ = maxCoords.Z;
79  m_mapInfo.terrainRegionBase = TerrainBase;
80 
81  // Don't have to free any previous since we just got here.
82  BuildHeightmapTerrain();
83  }
84 
85  public override void Dispose()
86  {
87  ReleaseHeightMapTerrain();
88  }
89 
90  // Using the information in m_mapInfo, create the physical representation of the heightmap.
91  private void BuildHeightmapTerrain()
92  {
93  // Create the terrain shape from the mapInfo
94  m_mapInfo.terrainShape = m_physicsScene.PE.CreateTerrainShape( m_mapInfo.ID,
95  new Vector3(m_mapInfo.sizeX, m_mapInfo.sizeY, 0), m_mapInfo.minZ, m_mapInfo.maxZ,
96  m_mapInfo.heightMap, 1f, BSParam.TerrainCollisionMargin);
97 
98 
99  // The terrain object initial position is at the center of the object
100  Vector3 centerPos;
101  centerPos.X = m_mapInfo.minCoords.X + (m_mapInfo.sizeX / 2f);
102  centerPos.Y = m_mapInfo.minCoords.Y + (m_mapInfo.sizeY / 2f);
103  centerPos.Z = m_mapInfo.minZ + ((m_mapInfo.maxZ - m_mapInfo.minZ) / 2f);
104 
105  m_mapInfo.terrainBody = m_physicsScene.PE.CreateBodyWithDefaultMotionState(m_mapInfo.terrainShape,
106  m_mapInfo.ID, centerPos, Quaternion.Identity);
107 
108  // Set current terrain attributes
109  m_physicsScene.PE.SetFriction(m_mapInfo.terrainBody, BSParam.TerrainFriction);
110  m_physicsScene.PE.SetHitFraction(m_mapInfo.terrainBody, BSParam.TerrainHitFraction);
111  m_physicsScene.PE.SetRestitution(m_mapInfo.terrainBody, BSParam.TerrainRestitution);
112  m_physicsScene.PE.SetCollisionFlags(m_mapInfo.terrainBody, CollisionFlags.CF_STATIC_OBJECT);
113 
114  m_mapInfo.terrainBody.collisionType = CollisionType.Terrain;
115 
116  // Return the new terrain to the world of physical objects
117  m_physicsScene.PE.AddObjectToWorld(m_physicsScene.World, m_mapInfo.terrainBody);
118 
119  // redo its bounding box now that it is in the world
120  m_physicsScene.PE.UpdateSingleAabb(m_physicsScene.World, m_mapInfo.terrainBody);
121 
122  // Make it so the terrain will not move or be considered for movement.
123  m_physicsScene.PE.ForceActivationState(m_mapInfo.terrainBody, ActivationState.DISABLE_SIMULATION);
124 
125  return;
126  }
127 
128  // If there is information in m_mapInfo pointing to physical structures, release same.
129  private void ReleaseHeightMapTerrain()
130  {
131  if (m_mapInfo != null)
132  {
133  if (m_mapInfo.terrainBody.HasPhysicalBody)
134  {
135  m_physicsScene.PE.RemoveObjectFromWorld(m_physicsScene.World, m_mapInfo.terrainBody);
136  // Frees both the body and the shape.
137  m_physicsScene.PE.DestroyObject(m_physicsScene.World, m_mapInfo.terrainBody);
138  }
139  }
140  m_mapInfo = null;
141  }
142 
143  // The passed position is relative to the base of the region.
144  public override float GetTerrainHeightAtXYZ(Vector3 pos)
145  {
146  float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
147 
148  int mapIndex = (int)pos.Y * (int)m_mapInfo.sizeY + (int)pos.X;
149  try
150  {
151  ret = m_mapInfo.heightMap[mapIndex];
152  }
153  catch
154  {
155  // Sometimes they give us wonky values of X and Y. Give a warning and return something.
156  m_physicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}",
157  LogHeader, m_mapInfo.terrainRegionBase, pos);
158  ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
159  }
160  return ret;
161  }
162 
163  // The passed position is relative to the base of the region.
164  public override float GetWaterLevelAtXYZ(Vector3 pos)
165  {
166  return m_physicsScene.SimpleWaterLevel;
167  }
168 }
169 }
BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize)
BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap, Vector3 minCoords, Vector3 maxCoords)