aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs
diff options
context:
space:
mode:
authorRobert Adams2012-11-20 14:51:50 -0800
committerRobert Adams2012-11-21 16:43:14 -0800
commit71b9640dfa67e830769aad64ef208d767e102c92 (patch)
treea4929a0626d4d5c2f738e5457931581dd6bdd64c /OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs
parentBulletSim: rename SHAPE_AVATAR to SHAPE_CAPSULE with the eye to eventually ha... (diff)
downloadopensim-SC_OLD-71b9640dfa67e830769aad64ef208d767e102c92.zip
opensim-SC_OLD-71b9640dfa67e830769aad64ef208d767e102c92.tar.gz
opensim-SC_OLD-71b9640dfa67e830769aad64ef208d767e102c92.tar.bz2
opensim-SC_OLD-71b9640dfa67e830769aad64ef208d767e102c92.tar.xz
BulletSim: pull heightmap implementation out of the terrain manager so a mesh terrain can be implemented.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs174
1 files changed, 174 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs
new file mode 100755
index 0000000..3bb63cd
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs
@@ -0,0 +1,174 @@
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 */
27using System;
28using System.Collections.Generic;
29using System.Text;
30
31using OpenSim.Framework;
32using OpenSim.Region.Framework;
33using OpenSim.Region.CoreModules;
34using OpenSim.Region.Physics.Manager;
35
36using Nini.Config;
37using log4net;
38
39using OpenMetaverse;
40
41namespace OpenSim.Region.Physics.BulletSPlugin
42{
43public sealed class BSTerrainHeightmap : BSTerrainPhys
44{
45 static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]";
46
47 BulletHeightMapInfo m_mapInfo;
48
49 public BSTerrainHeightmap(BSScene physicsScene, uint id, Vector3 regionSize)
50 : base(physicsScene)
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 BulletHeightMapInfo(id, initialMap, IntPtr.Zero);
61 m_mapInfo.minCoords = minTerrainCoords;
62 m_mapInfo.maxCoords = maxTerrainCoords;
63 // Don't have to free any previous since we just got here.
64 BuildHeightmapTerrain();
65 }
66
67 // This minCoords and maxCoords passed in give the size of the terrain (min and max Z
68 // are the high and low points of the heightmap).
69 public BSTerrainHeightmap(BSScene physicsScene, uint id, float[] initialMap,
70 Vector3 minCoords, Vector3 maxCoords)
71 : base(physicsScene)
72 {
73 m_mapInfo = new BulletHeightMapInfo(id, initialMap, IntPtr.Zero);
74 m_mapInfo.minCoords = minCoords;
75 m_mapInfo.maxCoords = maxCoords;
76 m_mapInfo.minZ = minCoords.Z;
77 m_mapInfo.maxZ = maxCoords.Z;
78
79 // Don't have to free any previous since we just got here.
80 BuildHeightmapTerrain();
81 }
82
83 public override void Dispose()
84 {
85 ReleaseHeightMapTerrain();
86 }
87
88 // Using the information in m_mapInfo, create the physical representation of the heightmap.
89 private void BuildHeightmapTerrain()
90 {
91 m_mapInfo.Ptr = BulletSimAPI.CreateHeightMapInfo2(PhysicsScene.World.ptr, m_mapInfo.ID,
92 m_mapInfo.minCoords, m_mapInfo.maxCoords,
93 m_mapInfo.heightMap, BSTerrainManager.TERRAIN_COLLISION_MARGIN);
94
95 // Create the terrain shape from the mapInfo
96 m_mapInfo.terrainShape = new BulletShape(BulletSimAPI.CreateTerrainShape2(m_mapInfo.Ptr),
97 PhysicsShapeType.SHAPE_TERRAIN);
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 = new BulletBody(m_mapInfo.ID,
106 BulletSimAPI.CreateBodyWithDefaultMotionState2(m_mapInfo.terrainShape.ptr,
107 m_mapInfo.ID, centerPos, Quaternion.Identity));
108
109 // Set current terrain attributes
110 BulletSimAPI.SetFriction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainFriction);
111 BulletSimAPI.SetHitFraction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction);
112 BulletSimAPI.SetRestitution2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainRestitution);
113 BulletSimAPI.SetCollisionFlags2(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);
114
115 // Return the new terrain to the world of physical objects
116 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr);
117
118 // redo its bounding box now that it is in the world
119 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr);
120
121 BulletSimAPI.SetCollisionFilterMask2(m_mapInfo.terrainBody.ptr,
122 (uint)CollisionFilterGroups.TerrainFilter,
123 (uint)CollisionFilterGroups.TerrainMask);
124
125 // Make it so the terrain will not move or be considered for movement.
126 BulletSimAPI.ForceActivationState2(m_mapInfo.terrainBody.ptr, ActivationState.DISABLE_SIMULATION);
127
128 return;
129 }
130
131 // If there is information in m_mapInfo pointing to physical structures, release same.
132 private void ReleaseHeightMapTerrain()
133 {
134 if (m_mapInfo != null)
135 {
136 if (m_mapInfo.terrainBody.ptr != IntPtr.Zero)
137 {
138 if (BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr))
139 {
140 // Frees both the body and the shape.
141 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr);
142 BulletSimAPI.ReleaseHeightMapInfo2(m_mapInfo.Ptr);
143 }
144 }
145 }
146 m_mapInfo = null;
147 }
148
149 // The passed position is relative to the base of the region.
150 public override float GetHeightAtXYZ(Vector3 pos)
151 {
152 float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
153
154 int mapIndex = (int)pos.Y * (int)m_mapInfo.sizeY + (int)pos.X;
155 try
156 {
157 ret = m_mapInfo.heightMap[mapIndex];
158 }
159 catch
160 {
161 // Sometimes they give us wonky values of X and Y. Give a warning and return something.
162 PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}",
163 LogHeader, m_mapInfo.terrainRegionBase, pos);
164 ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
165 }
166 return ret;
167 }
168
169 public override Vector3 TerrainBase
170 {
171 get { return m_mapInfo.terrainRegionBase; }
172 }
173}
174}