diff options
Diffstat (limited to '')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs | 340 |
1 files changed, 170 insertions, 170 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs index e9fd0cb..8fc36d1 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs | |||
@@ -1,170 +1,170 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ | 2 | * Copyright (c) Contributors, http://opensimulator.org/ |
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | 3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
4 | * | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: | 6 | * modification, are permitted provided that the following conditions are met: |
7 | * * Redistributions of source code must retain the above copyright | 7 | * * Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. | 8 | * notice, this list of conditions and the following disclaimer. |
9 | * * Redistributions in binary form must reproduce the above copyrightD | 9 | * * Redistributions in binary form must reproduce the above copyrightD |
10 | * notice, this list of conditions and the following disclaimer in the | 10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. | 11 | * documentation and/or other materials provided with the distribution. |
12 | * * Neither the name of the OpenSimulator Project nor the | 12 | * * Neither the name of the OpenSimulator Project nor the |
13 | * names of its contributors may be used to endorse or promote products | 13 | * names of its contributors may be used to endorse or promote products |
14 | * derived from this software without specific prior written permission. | 14 | * derived from this software without specific prior written permission. |
15 | * | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | 16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | 19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ | 26 | */ |
27 | using System; | 27 | using System; |
28 | using System.Collections.Generic; | 28 | using System.Collections.Generic; |
29 | using System.Text; | 29 | using System.Text; |
30 | 30 | ||
31 | using OpenSim.Framework; | 31 | using OpenSim.Framework; |
32 | using OpenSim.Region.Framework; | 32 | using OpenSim.Region.Framework; |
33 | using OpenSim.Region.CoreModules; | 33 | using OpenSim.Region.CoreModules; |
34 | using OpenSim.Region.Physics.Manager; | 34 | using OpenSim.Region.Physics.Manager; |
35 | 35 | ||
36 | using Nini.Config; | 36 | using Nini.Config; |
37 | using log4net; | 37 | using log4net; |
38 | 38 | ||
39 | using OpenMetaverse; | 39 | using OpenMetaverse; |
40 | 40 | ||
41 | namespace OpenSim.Region.Physics.BulletSPlugin | 41 | namespace OpenSim.Region.Physics.BulletSPlugin |
42 | { | 42 | { |
43 | public sealed class BSTerrainHeightmap : BSTerrainPhys | 43 | public sealed class BSTerrainHeightmap : BSTerrainPhys |
44 | { | 44 | { |
45 | static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]"; | 45 | static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]"; |
46 | 46 | ||
47 | BulletHeightMapInfo m_mapInfo = null; | 47 | BulletHeightMapInfo m_mapInfo = null; |
48 | 48 | ||
49 | // Constructor to build a default, flat heightmap terrain. | 49 | // Constructor to build a default, flat heightmap terrain. |
50 | public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize) | 50 | public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize) |
51 | : base(physicsScene, regionBase, id) | 51 | : base(physicsScene, regionBase, id) |
52 | { | 52 | { |
53 | Vector3 minTerrainCoords = new Vector3(0f, 0f, BSTerrainManager.HEIGHT_INITIALIZATION - BSTerrainManager.HEIGHT_EQUAL_FUDGE); | 53 | Vector3 minTerrainCoords = new Vector3(0f, 0f, BSTerrainManager.HEIGHT_INITIALIZATION - BSTerrainManager.HEIGHT_EQUAL_FUDGE); |
54 | Vector3 maxTerrainCoords = new Vector3(regionSize.X, regionSize.Y, BSTerrainManager.HEIGHT_INITIALIZATION); | 54 | Vector3 maxTerrainCoords = new Vector3(regionSize.X, regionSize.Y, BSTerrainManager.HEIGHT_INITIALIZATION); |
55 | int totalHeights = (int)maxTerrainCoords.X * (int)maxTerrainCoords.Y; | 55 | int totalHeights = (int)maxTerrainCoords.X * (int)maxTerrainCoords.Y; |
56 | float[] initialMap = new float[totalHeights]; | 56 | float[] initialMap = new float[totalHeights]; |
57 | for (int ii = 0; ii < totalHeights; ii++) | 57 | for (int ii = 0; ii < totalHeights; ii++) |
58 | { | 58 | { |
59 | initialMap[ii] = BSTerrainManager.HEIGHT_INITIALIZATION; | 59 | initialMap[ii] = BSTerrainManager.HEIGHT_INITIALIZATION; |
60 | } | 60 | } |
61 | m_mapInfo = new BulletHeightMapInfo(id, initialMap, IntPtr.Zero); | 61 | m_mapInfo = new BulletHeightMapInfo(id, initialMap, IntPtr.Zero); |
62 | m_mapInfo.minCoords = minTerrainCoords; | 62 | m_mapInfo.minCoords = minTerrainCoords; |
63 | m_mapInfo.maxCoords = maxTerrainCoords; | 63 | m_mapInfo.maxCoords = maxTerrainCoords; |
64 | m_mapInfo.terrainRegionBase = TerrainBase; | 64 | m_mapInfo.terrainRegionBase = TerrainBase; |
65 | // Don't have to free any previous since we just got here. | 65 | // Don't have to free any previous since we just got here. |
66 | BuildHeightmapTerrain(); | 66 | BuildHeightmapTerrain(); |
67 | } | 67 | } |
68 | 68 | ||
69 | // This minCoords and maxCoords passed in give the size of the terrain (min and max Z | 69 | // This minCoords and maxCoords passed in give the size of the terrain (min and max Z |
70 | // are the high and low points of the heightmap). | 70 | // are the high and low points of the heightmap). |
71 | public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap, | 71 | public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap, |
72 | Vector3 minCoords, Vector3 maxCoords) | 72 | Vector3 minCoords, Vector3 maxCoords) |
73 | : base(physicsScene, regionBase, id) | 73 | : base(physicsScene, regionBase, id) |
74 | { | 74 | { |
75 | m_mapInfo = new BulletHeightMapInfo(id, initialMap, IntPtr.Zero); | 75 | m_mapInfo = new BulletHeightMapInfo(id, initialMap, IntPtr.Zero); |
76 | m_mapInfo.minCoords = minCoords; | 76 | m_mapInfo.minCoords = minCoords; |
77 | m_mapInfo.maxCoords = maxCoords; | 77 | m_mapInfo.maxCoords = maxCoords; |
78 | m_mapInfo.minZ = minCoords.Z; | 78 | m_mapInfo.minZ = minCoords.Z; |
79 | m_mapInfo.maxZ = maxCoords.Z; | 79 | m_mapInfo.maxZ = maxCoords.Z; |
80 | m_mapInfo.terrainRegionBase = TerrainBase; | 80 | m_mapInfo.terrainRegionBase = TerrainBase; |
81 | 81 | ||
82 | // Don't have to free any previous since we just got here. | 82 | // Don't have to free any previous since we just got here. |
83 | BuildHeightmapTerrain(); | 83 | BuildHeightmapTerrain(); |
84 | } | 84 | } |
85 | 85 | ||
86 | public override void Dispose() | 86 | public override void Dispose() |
87 | { | 87 | { |
88 | ReleaseHeightMapTerrain(); | 88 | ReleaseHeightMapTerrain(); |
89 | } | 89 | } |
90 | 90 | ||
91 | // Using the information in m_mapInfo, create the physical representation of the heightmap. | 91 | // Using the information in m_mapInfo, create the physical representation of the heightmap. |
92 | private void BuildHeightmapTerrain() | 92 | private void BuildHeightmapTerrain() |
93 | { | 93 | { |
94 | m_mapInfo.Ptr = BulletSimAPI.CreateHeightMapInfo2(PhysicsScene.World.ptr, m_mapInfo.ID, | 94 | m_mapInfo.Ptr = BulletSimAPI.CreateHeightMapInfo2(PhysicsScene.World.ptr, m_mapInfo.ID, |
95 | m_mapInfo.minCoords, m_mapInfo.maxCoords, | 95 | m_mapInfo.minCoords, m_mapInfo.maxCoords, |
96 | m_mapInfo.heightMap, BSTerrainManager.TERRAIN_COLLISION_MARGIN); | 96 | m_mapInfo.heightMap, BSTerrainManager.TERRAIN_COLLISION_MARGIN); |
97 | 97 | ||
98 | // Create the terrain shape from the mapInfo | 98 | // Create the terrain shape from the mapInfo |
99 | m_mapInfo.terrainShape = new BulletShape(BulletSimAPI.CreateTerrainShape2(m_mapInfo.Ptr), | 99 | m_mapInfo.terrainShape = new BulletShape(BulletSimAPI.CreateTerrainShape2(m_mapInfo.Ptr), |
100 | PhysicsShapeType.SHAPE_TERRAIN); | 100 | PhysicsShapeType.SHAPE_TERRAIN); |
101 | 101 | ||
102 | // The terrain object initial position is at the center of the object | 102 | // The terrain object initial position is at the center of the object |
103 | Vector3 centerPos; | 103 | Vector3 centerPos; |
104 | centerPos.X = m_mapInfo.minCoords.X + (m_mapInfo.sizeX / 2f); | 104 | centerPos.X = m_mapInfo.minCoords.X + (m_mapInfo.sizeX / 2f); |
105 | centerPos.Y = m_mapInfo.minCoords.Y + (m_mapInfo.sizeY / 2f); | 105 | centerPos.Y = m_mapInfo.minCoords.Y + (m_mapInfo.sizeY / 2f); |
106 | centerPos.Z = m_mapInfo.minZ + ((m_mapInfo.maxZ - m_mapInfo.minZ) / 2f); | 106 | centerPos.Z = m_mapInfo.minZ + ((m_mapInfo.maxZ - m_mapInfo.minZ) / 2f); |
107 | 107 | ||
108 | m_mapInfo.terrainBody = new BulletBody(m_mapInfo.ID, | 108 | m_mapInfo.terrainBody = new BulletBody(m_mapInfo.ID, |
109 | BulletSimAPI.CreateBodyWithDefaultMotionState2(m_mapInfo.terrainShape.ptr, | 109 | BulletSimAPI.CreateBodyWithDefaultMotionState2(m_mapInfo.terrainShape.ptr, |
110 | m_mapInfo.ID, centerPos, Quaternion.Identity)); | 110 | m_mapInfo.ID, centerPos, Quaternion.Identity)); |
111 | 111 | ||
112 | // Set current terrain attributes | 112 | // Set current terrain attributes |
113 | BulletSimAPI.SetFriction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainFriction); | 113 | BulletSimAPI.SetFriction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainFriction); |
114 | BulletSimAPI.SetHitFraction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); | 114 | BulletSimAPI.SetHitFraction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); |
115 | BulletSimAPI.SetRestitution2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainRestitution); | 115 | BulletSimAPI.SetRestitution2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainRestitution); |
116 | BulletSimAPI.SetCollisionFlags2(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); | 116 | BulletSimAPI.SetCollisionFlags2(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); |
117 | 117 | ||
118 | // Return the new terrain to the world of physical objects | 118 | // Return the new terrain to the world of physical objects |
119 | BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); | 119 | BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); |
120 | 120 | ||
121 | // redo its bounding box now that it is in the world | 121 | // redo its bounding box now that it is in the world |
122 | BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); | 122 | BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); |
123 | 123 | ||
124 | BulletSimAPI.SetCollisionFilterMask2(m_mapInfo.terrainBody.ptr, | 124 | BulletSimAPI.SetCollisionFilterMask2(m_mapInfo.terrainBody.ptr, |
125 | (uint)CollisionFilterGroups.TerrainFilter, | 125 | (uint)CollisionFilterGroups.TerrainFilter, |
126 | (uint)CollisionFilterGroups.TerrainMask); | 126 | (uint)CollisionFilterGroups.TerrainMask); |
127 | 127 | ||
128 | // Make it so the terrain will not move or be considered for movement. | 128 | // Make it so the terrain will not move or be considered for movement. |
129 | BulletSimAPI.ForceActivationState2(m_mapInfo.terrainBody.ptr, ActivationState.DISABLE_SIMULATION); | 129 | BulletSimAPI.ForceActivationState2(m_mapInfo.terrainBody.ptr, ActivationState.DISABLE_SIMULATION); |
130 | 130 | ||
131 | return; | 131 | return; |
132 | } | 132 | } |
133 | 133 | ||
134 | // If there is information in m_mapInfo pointing to physical structures, release same. | 134 | // If there is information in m_mapInfo pointing to physical structures, release same. |
135 | private void ReleaseHeightMapTerrain() | 135 | private void ReleaseHeightMapTerrain() |
136 | { | 136 | { |
137 | if (m_mapInfo != null) | 137 | if (m_mapInfo != null) |
138 | { | 138 | { |
139 | if (m_mapInfo.terrainBody.ptr != IntPtr.Zero) | 139 | if (m_mapInfo.terrainBody.ptr != IntPtr.Zero) |
140 | { | 140 | { |
141 | BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); | 141 | BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); |
142 | // Frees both the body and the shape. | 142 | // Frees both the body and the shape. |
143 | BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); | 143 | BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); |
144 | BulletSimAPI.ReleaseHeightMapInfo2(m_mapInfo.Ptr); | 144 | BulletSimAPI.ReleaseHeightMapInfo2(m_mapInfo.Ptr); |
145 | } | 145 | } |
146 | } | 146 | } |
147 | m_mapInfo = null; | 147 | m_mapInfo = null; |
148 | } | 148 | } |
149 | 149 | ||
150 | // The passed position is relative to the base of the region. | 150 | // The passed position is relative to the base of the region. |
151 | public override float GetHeightAtXYZ(Vector3 pos) | 151 | public override float GetHeightAtXYZ(Vector3 pos) |
152 | { | 152 | { |
153 | float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; | 153 | float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; |
154 | 154 | ||
155 | int mapIndex = (int)pos.Y * (int)m_mapInfo.sizeY + (int)pos.X; | 155 | int mapIndex = (int)pos.Y * (int)m_mapInfo.sizeY + (int)pos.X; |
156 | try | 156 | try |
157 | { | 157 | { |
158 | ret = m_mapInfo.heightMap[mapIndex]; | 158 | ret = m_mapInfo.heightMap[mapIndex]; |
159 | } | 159 | } |
160 | catch | 160 | catch |
161 | { | 161 | { |
162 | // Sometimes they give us wonky values of X and Y. Give a warning and return something. | 162 | // Sometimes they give us wonky values of X and Y. Give a warning and return something. |
163 | PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}", | 163 | PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}", |
164 | LogHeader, m_mapInfo.terrainRegionBase, pos); | 164 | LogHeader, m_mapInfo.terrainRegionBase, pos); |
165 | ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; | 165 | ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; |
166 | } | 166 | } |
167 | return ret; | 167 | return ret; |
168 | } | 168 | } |
169 | } | 169 | } |
170 | } | 170 | } |