diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs | 464 |
1 files changed, 464 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs new file mode 100755 index 0000000..ab45f8f --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs | |||
@@ -0,0 +1,464 @@ | |||
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.CoreModules; | ||
34 | using OpenSim.Region.Physics.Manager; | ||
35 | |||
36 | using Nini.Config; | ||
37 | using log4net; | ||
38 | |||
39 | using OpenMetaverse; | ||
40 | |||
41 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
42 | { | ||
43 | public class BSTerrainManager | ||
44 | { | ||
45 | static string LogHeader = "[BULLETSIM TERRAIN MANAGER]"; | ||
46 | |||
47 | // These height values are fractional so the odd values will be | ||
48 | // noticable when debugging. | ||
49 | public const float HEIGHT_INITIALIZATION = 24.987f; | ||
50 | public const float HEIGHT_INITIAL_LASTHEIGHT = 24.876f; | ||
51 | public const float HEIGHT_GETHEIGHT_RET = 24.765f; | ||
52 | |||
53 | // If the min and max height are equal, we reduce the min by this | ||
54 | // amount to make sure that a bounding box is built for the terrain. | ||
55 | public const float HEIGHT_EQUAL_FUDGE = 0.2f; | ||
56 | |||
57 | public const float TERRAIN_COLLISION_MARGIN = 0.0f; | ||
58 | |||
59 | // Until the whole simulator is changed to pass us the region size, we rely on constants. | ||
60 | public Vector3 DefaultRegionSize = new Vector3(Constants.RegionSize, Constants.RegionSize, 0f); | ||
61 | |||
62 | // The scene that I am part of | ||
63 | private BSScene m_physicsScene; | ||
64 | |||
65 | // The ground plane created to keep thing from falling to infinity. | ||
66 | private BulletBody m_groundPlane; | ||
67 | |||
68 | // If doing mega-regions, if we're region zero we will be managing multiple | ||
69 | // region terrains since region zero does the physics for the whole mega-region. | ||
70 | private Dictionary<Vector2, BulletHeightMapInfo> m_heightMaps; | ||
71 | |||
72 | // True of the terrain has been modified. | ||
73 | // Used to force recalculation of terrain height after terrain has been modified | ||
74 | private bool m_terrainModified; | ||
75 | |||
76 | // If we are doing mega-regions, terrains are added from TERRAIN_ID to m_terrainCount. | ||
77 | // This is incremented before assigning to new region so it is the last ID allocated. | ||
78 | private uint m_terrainCount = BSScene.CHILDTERRAIN_ID - 1; | ||
79 | public uint HighestTerrainID { get {return m_terrainCount; } } | ||
80 | |||
81 | // If doing mega-regions, this holds our offset from region zero of | ||
82 | // the mega-regions. "parentScene" points to the PhysicsScene of region zero. | ||
83 | private Vector3 m_worldOffset; | ||
84 | // If the parent region (region 0), this is the extent of the combined regions | ||
85 | // relative to the origin of region zero | ||
86 | private Vector3 m_worldMax; | ||
87 | private PhysicsScene m_parentScene; | ||
88 | |||
89 | public BSTerrainManager(BSScene physicsScene) | ||
90 | { | ||
91 | m_physicsScene = physicsScene; | ||
92 | m_heightMaps = new Dictionary<Vector2,BulletHeightMapInfo>(); | ||
93 | m_terrainModified = false; | ||
94 | |||
95 | // Assume one region of default size | ||
96 | m_worldOffset = Vector3.Zero; | ||
97 | m_worldMax = new Vector3(DefaultRegionSize.X, DefaultRegionSize.Y, 4096f); | ||
98 | m_parentScene = null; | ||
99 | } | ||
100 | |||
101 | // Create the initial instance of terrain and the underlying ground plane. | ||
102 | // The objects are allocated in the unmanaged space and the pointers are tracked | ||
103 | // by the managed code. | ||
104 | // The terrains and the groundPlane are not added to the list of PhysObjects. | ||
105 | // This is called from the initialization routine so we presume it is | ||
106 | // safe to call Bullet in real time. We hope no one is moving prims around yet. | ||
107 | public void CreateInitialGroundPlaneAndTerrain() | ||
108 | { | ||
109 | // The ground plane is here to catch things that are trying to drop to negative infinity | ||
110 | BulletShape groundPlaneShape = new BulletShape(BulletSimAPI.CreateGroundPlaneShape2(BSScene.GROUNDPLANE_ID, 1f, TERRAIN_COLLISION_MARGIN)); | ||
111 | m_groundPlane = new BulletBody(BSScene.GROUNDPLANE_ID, | ||
112 | BulletSimAPI.CreateBodyWithDefaultMotionState2(groundPlaneShape.Ptr, Vector3.Zero, Quaternion.Identity)); | ||
113 | BulletSimAPI.AddObjectToWorld2(m_physicsScene.World.Ptr, m_groundPlane.Ptr); | ||
114 | |||
115 | Vector3 minTerrainCoords = new Vector3(0f, 0f, HEIGHT_INITIALIZATION - HEIGHT_EQUAL_FUDGE); | ||
116 | Vector3 maxTerrainCoords = new Vector3(DefaultRegionSize.X, DefaultRegionSize.Y, HEIGHT_INITIALIZATION); | ||
117 | int totalHeights = (int)maxTerrainCoords.X * (int)maxTerrainCoords.Y; | ||
118 | float[] initialMap = new float[totalHeights]; | ||
119 | for (int ii = 0; ii < totalHeights; ii++) | ||
120 | { | ||
121 | initialMap[ii] = HEIGHT_INITIALIZATION; | ||
122 | } | ||
123 | UpdateOrCreateTerrain(BSScene.TERRAIN_ID, initialMap, minTerrainCoords, maxTerrainCoords, true); | ||
124 | } | ||
125 | |||
126 | // Release all the terrain structures we might have allocated | ||
127 | public void ReleaseGroundPlaneAndTerrain() | ||
128 | { | ||
129 | if (m_groundPlane.Ptr != IntPtr.Zero) | ||
130 | { | ||
131 | if (BulletSimAPI.RemoveObjectFromWorld2(m_physicsScene.World.Ptr, m_groundPlane.Ptr)) | ||
132 | { | ||
133 | BulletSimAPI.DestroyObject2(m_physicsScene.World.Ptr, m_groundPlane.Ptr); | ||
134 | } | ||
135 | m_groundPlane.Ptr = IntPtr.Zero; | ||
136 | } | ||
137 | |||
138 | ReleaseTerrain(); | ||
139 | } | ||
140 | |||
141 | // Release all the terrain we have allocated | ||
142 | public void ReleaseTerrain() | ||
143 | { | ||
144 | foreach (KeyValuePair<Vector2, BulletHeightMapInfo> kvp in m_heightMaps) | ||
145 | { | ||
146 | if (BulletSimAPI.RemoveObjectFromWorld2(m_physicsScene.World.Ptr, kvp.Value.terrainBody.Ptr)) | ||
147 | { | ||
148 | BulletSimAPI.DestroyObject2(m_physicsScene.World.Ptr, kvp.Value.terrainBody.Ptr); | ||
149 | BulletSimAPI.ReleaseHeightMapInfo2(kvp.Value.Ptr); | ||
150 | } | ||
151 | } | ||
152 | m_heightMaps.Clear(); | ||
153 | } | ||
154 | |||
155 | // The simulator wants to set a new heightmap for the terrain. | ||
156 | public void SetTerrain(float[] heightMap) { | ||
157 | if (m_worldOffset != Vector3.Zero && m_parentScene != null) | ||
158 | { | ||
159 | // If a child of a mega-region, we shouldn't have any terrain allocated for us | ||
160 | ReleaseGroundPlaneAndTerrain(); | ||
161 | // If doing the mega-prim stuff and we are the child of the zero region, | ||
162 | // the terrain is added to our parent | ||
163 | if (m_parentScene is BSScene) | ||
164 | { | ||
165 | DetailLog("{0},SetTerrain.ToParent,offset={1},worldMax={2}", | ||
166 | BSScene.DetailLogZero, m_worldOffset, m_worldMax); | ||
167 | ((BSScene)m_parentScene).TerrainManager.UpdateOrCreateTerrain(BSScene.CHILDTERRAIN_ID, | ||
168 | heightMap, m_worldOffset, m_worldOffset+DefaultRegionSize, false); | ||
169 | } | ||
170 | } | ||
171 | else | ||
172 | { | ||
173 | // If not doing the mega-prim thing, just change the terrain | ||
174 | DetailLog("{0},SetTerrain.Existing", BSScene.DetailLogZero); | ||
175 | |||
176 | UpdateOrCreateTerrain(BSScene.TERRAIN_ID, heightMap, m_worldOffset, m_worldOffset+DefaultRegionSize, false); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | // If called with no mapInfo for the terrain, this will create a new mapInfo and terrain | ||
181 | // based on the passed information. The 'id' should be either the terrain id or | ||
182 | // BSScene.CHILDTERRAIN_ID. If the latter, a new child terrain ID will be allocated and used. | ||
183 | // The latter feature is for creating child terrains for mega-regions. | ||
184 | // If called with a mapInfo in m_heightMaps but the terrain has no body yet (mapInfo.terrainBody.Ptr == 0) | ||
185 | // then a new body and shape is created and the mapInfo is filled. | ||
186 | // This call is used for doing the initial terrain creation. | ||
187 | // If called with a mapInfo in m_heightMaps and there is an existing terrain body, a new | ||
188 | // terrain shape is created and added to the body. | ||
189 | // This call is most often used to update the heightMap and parameters of the terrain. | ||
190 | // The 'doNow' boolean says whether to do all the unmanaged activities right now (like when | ||
191 | // calling this routine from initialization or taint-time routines) or whether to delay | ||
192 | // all the unmanaged activities to taint-time. | ||
193 | private void UpdateOrCreateTerrain(uint id, float[] heightMap, Vector3 minCoords, Vector3 maxCoords, bool doNow) | ||
194 | { | ||
195 | DetailLog("{0},BSTerrainManager.UpdateOrCreateTerrain,call,minC={1},maxC={2},doNow={3}", | ||
196 | BSScene.DetailLogZero, minCoords, maxCoords, doNow); | ||
197 | |||
198 | float minZ = float.MaxValue; | ||
199 | float maxZ = float.MinValue; | ||
200 | Vector2 terrainRegionBase = new Vector2(minCoords.X, minCoords.Y); | ||
201 | |||
202 | int heightMapSize = heightMap.Length; | ||
203 | for (int ii = 0; ii < heightMapSize; ii++) | ||
204 | { | ||
205 | float height = heightMap[ii]; | ||
206 | if (height < minZ) minZ = height; | ||
207 | if (height > maxZ) maxZ = height; | ||
208 | } | ||
209 | |||
210 | // The shape of the terrain is from its base to its extents. | ||
211 | minCoords.Z = minZ; | ||
212 | maxCoords.Z = maxZ; | ||
213 | |||
214 | BulletHeightMapInfo mapInfo; | ||
215 | if (m_heightMaps.TryGetValue(terrainRegionBase, out mapInfo)) | ||
216 | { | ||
217 | // If this is terrain we know about, it's easy to update | ||
218 | |||
219 | mapInfo.heightMap = heightMap; | ||
220 | mapInfo.minCoords = minCoords; | ||
221 | mapInfo.maxCoords = maxCoords; | ||
222 | mapInfo.minZ = minZ; | ||
223 | mapInfo.maxZ = maxZ; | ||
224 | mapInfo.sizeX = maxCoords.X - minCoords.X; | ||
225 | mapInfo.sizeY = maxCoords.Y - minCoords.Y; | ||
226 | DetailLog("{0},UpdateOrCreateTerrain:UpdateExisting,call,terrainBase={1},minC={2}, maxC={3}, szX={4}, szY={5}", | ||
227 | BSScene.DetailLogZero, terrainRegionBase, mapInfo.minCoords, mapInfo.maxCoords, mapInfo.sizeX, mapInfo.sizeY); | ||
228 | |||
229 | BSScene.TaintCallback rebuildOperation = delegate() | ||
230 | { | ||
231 | if (m_parentScene != null) | ||
232 | { | ||
233 | // It's possible that Combine() was called after this code was queued. | ||
234 | // If we are a child of combined regions, we don't create any terrain for us. | ||
235 | DetailLog("{0},UpdateOrCreateTerrain:AmACombineChild,taint", BSScene.DetailLogZero); | ||
236 | |||
237 | // Get rid of any terrain that may have been allocated for us. | ||
238 | ReleaseGroundPlaneAndTerrain(); | ||
239 | |||
240 | // I hate doing this, but just bail | ||
241 | return; | ||
242 | } | ||
243 | |||
244 | if (mapInfo.terrainBody.Ptr != IntPtr.Zero) | ||
245 | { | ||
246 | // Updating an existing terrain. | ||
247 | DetailLog("{0},UpdateOrCreateTerrain:UpdateExisting,taint,terrainBase={1},minC={2}, maxC={3}, szX={4}, szY={5}", | ||
248 | BSScene.DetailLogZero, terrainRegionBase, mapInfo.minCoords, mapInfo.maxCoords, mapInfo.sizeX, mapInfo.sizeY); | ||
249 | |||
250 | // Remove from the dynamics world because we're going to mangle this object | ||
251 | BulletSimAPI.RemoveObjectFromWorld2(m_physicsScene.World.Ptr, mapInfo.terrainBody.Ptr); | ||
252 | |||
253 | // Get rid of the old terrain | ||
254 | BulletSimAPI.DestroyObject2(m_physicsScene.World.Ptr, mapInfo.terrainBody.Ptr); | ||
255 | BulletSimAPI.ReleaseHeightMapInfo2(mapInfo.Ptr); | ||
256 | mapInfo.Ptr = IntPtr.Zero; | ||
257 | |||
258 | /* | ||
259 | // NOTE: This routine is half here because I can't get the terrain shape replacement | ||
260 | // to work. In the short term, the above three lines completely delete the old | ||
261 | // terrain and the code below recreates one from scratch. | ||
262 | // Hopefully the Bullet community will help me out on this one. | ||
263 | |||
264 | // First, release the old collision shape (there is only one terrain) | ||
265 | BulletSimAPI.DeleteCollisionShape2(m_physicsScene.World.Ptr, mapInfo.terrainShape.Ptr); | ||
266 | |||
267 | // Fill the existing height map info with the new location and size information | ||
268 | BulletSimAPI.FillHeightMapInfo2(m_physicsScene.World.Ptr, mapInfo.Ptr, mapInfo.ID, | ||
269 | mapInfo.minCoords, mapInfo.maxCoords, mapInfo.heightMap, TERRAIN_COLLISION_MARGIN); | ||
270 | |||
271 | // Create a terrain shape based on the new info | ||
272 | mapInfo.terrainShape = new BulletShape(BulletSimAPI.CreateTerrainShape2(mapInfo.Ptr)); | ||
273 | |||
274 | // Stuff the shape into the existing terrain body | ||
275 | BulletSimAPI.SetBodyShape2(m_physicsScene.World.Ptr, mapInfo.terrainBody.Ptr, mapInfo.terrainShape.Ptr); | ||
276 | */ | ||
277 | } | ||
278 | // else | ||
279 | { | ||
280 | // Creating a new terrain. | ||
281 | DetailLog("{0},UpdateOrCreateTerrain:CreateNewTerrain,taint,baseX={1},baseY={2},minZ={3},maxZ={4}", | ||
282 | BSScene.DetailLogZero, mapInfo.minCoords.X, mapInfo.minCoords.Y, minZ, maxZ); | ||
283 | |||
284 | mapInfo.ID = id; | ||
285 | mapInfo.Ptr = BulletSimAPI.CreateHeightMapInfo2(m_physicsScene.World.Ptr, mapInfo.ID, | ||
286 | mapInfo.minCoords, mapInfo.maxCoords, mapInfo.heightMap, TERRAIN_COLLISION_MARGIN); | ||
287 | |||
288 | // The terrain object initial position is at the center of the object | ||
289 | Vector3 centerPos; | ||
290 | centerPos.X = minCoords.X + (mapInfo.sizeX / 2f); | ||
291 | centerPos.Y = minCoords.Y + (mapInfo.sizeY / 2f); | ||
292 | centerPos.Z = minZ + ((maxZ - minZ) / 2f); | ||
293 | |||
294 | // Create the terrain shape from the mapInfo | ||
295 | mapInfo.terrainShape = new BulletShape(BulletSimAPI.CreateTerrainShape2(mapInfo.Ptr)); | ||
296 | |||
297 | mapInfo.terrainBody = new BulletBody(mapInfo.ID, | ||
298 | BulletSimAPI.CreateBodyWithDefaultMotionState2(mapInfo.terrainShape.Ptr, | ||
299 | centerPos, Quaternion.Identity)); | ||
300 | } | ||
301 | |||
302 | // Make sure the entry is in the heightmap table | ||
303 | m_heightMaps[terrainRegionBase] = mapInfo; | ||
304 | |||
305 | // Set current terrain attributes | ||
306 | BulletSimAPI.SetFriction2(mapInfo.terrainBody.Ptr, m_physicsScene.Params.terrainFriction); | ||
307 | BulletSimAPI.SetHitFraction2(mapInfo.terrainBody.Ptr, m_physicsScene.Params.terrainHitFraction); | ||
308 | BulletSimAPI.SetRestitution2(mapInfo.terrainBody.Ptr, m_physicsScene.Params.terrainRestitution); | ||
309 | BulletSimAPI.SetCollisionFlags2(mapInfo.terrainBody.Ptr, CollisionFlags.CF_STATIC_OBJECT); | ||
310 | |||
311 | BulletSimAPI.SetMassProps2(mapInfo.terrainBody.Ptr, 0f, Vector3.Zero); | ||
312 | BulletSimAPI.UpdateInertiaTensor2(mapInfo.terrainBody.Ptr); | ||
313 | |||
314 | // Return the new terrain to the world of physical objects | ||
315 | BulletSimAPI.AddObjectToWorld2(m_physicsScene.World.Ptr, mapInfo.terrainBody.Ptr); | ||
316 | |||
317 | // redo its bounding box now that it is in the world | ||
318 | BulletSimAPI.UpdateSingleAabb2(m_physicsScene.World.Ptr, mapInfo.terrainBody.Ptr); | ||
319 | |||
320 | // Make sure the new shape is processed. | ||
321 | BulletSimAPI.Activate2(mapInfo.terrainBody.Ptr, true); | ||
322 | }; | ||
323 | |||
324 | // There is the option to do the changes now (we're already in 'taint time'), or | ||
325 | // to do the Bullet operations later. | ||
326 | if (doNow) | ||
327 | rebuildOperation(); | ||
328 | else | ||
329 | m_physicsScene.TaintedObject("BSScene.UpdateOrCreateTerrain:UpdateExisting", rebuildOperation); | ||
330 | } | ||
331 | else | ||
332 | { | ||
333 | // We don't know about this terrain so either we are creating a new terrain or | ||
334 | // our mega-prim child is giving us a new terrain to add to the phys world | ||
335 | |||
336 | // if this is a child terrain, calculate a unique terrain id | ||
337 | uint newTerrainID = id; | ||
338 | if (newTerrainID >= BSScene.CHILDTERRAIN_ID) | ||
339 | newTerrainID = ++m_terrainCount; | ||
340 | |||
341 | float[] heightMapX = heightMap; | ||
342 | Vector3 minCoordsX = minCoords; | ||
343 | Vector3 maxCoordsX = maxCoords; | ||
344 | |||
345 | DetailLog("{0},UpdateOrCreateTerrain:NewTerrain,call,id={1}, minC={2}, maxC={3}", | ||
346 | BSScene.DetailLogZero, newTerrainID, minCoords, minCoords); | ||
347 | |||
348 | // Code that must happen at taint-time | ||
349 | BSScene.TaintCallback createOperation = delegate() | ||
350 | { | ||
351 | DetailLog("{0},UpdateOrCreateTerrain:NewTerrain,taint,baseX={1},baseY={2}", BSScene.DetailLogZero, minCoords.X, minCoords.Y); | ||
352 | // Create a new mapInfo that will be filled with the new info | ||
353 | mapInfo = new BulletHeightMapInfo(id, heightMapX, | ||
354 | BulletSimAPI.CreateHeightMapInfo2(m_physicsScene.World.Ptr, newTerrainID, | ||
355 | minCoordsX, maxCoordsX, heightMapX, TERRAIN_COLLISION_MARGIN)); | ||
356 | // Put the unfilled heightmap info into the collection of same | ||
357 | m_heightMaps.Add(terrainRegionBase, mapInfo); | ||
358 | // Build the terrain | ||
359 | UpdateOrCreateTerrain(newTerrainID, heightMap, minCoords, maxCoords, true); | ||
360 | }; | ||
361 | |||
362 | // If already in taint-time, just call Bullet. Otherwise queue the operations for the safe time. | ||
363 | if (doNow) | ||
364 | createOperation(); | ||
365 | else | ||
366 | m_physicsScene.TaintedObject("BSScene.UpdateOrCreateTerrain:NewTerrain", createOperation); | ||
367 | } | ||
368 | } | ||
369 | |||
370 | // Someday we will have complex terrain with caves and tunnels | ||
371 | public float GetTerrainHeightAtXYZ(Vector3 loc) | ||
372 | { | ||
373 | // For the moment, it's flat and convex | ||
374 | return GetTerrainHeightAtXY(loc.X, loc.Y); | ||
375 | } | ||
376 | |||
377 | // Given an X and Y, find the height of the terrain. | ||
378 | // Since we could be handling multiple terrains for a mega-region, | ||
379 | // the base of the region is calcuated assuming all regions are | ||
380 | // the same size and that is the default. | ||
381 | // Once the heightMapInfo is found, we have all the information to | ||
382 | // compute the offset into the array. | ||
383 | private float lastHeightTX = 999999f; | ||
384 | private float lastHeightTY = 999999f; | ||
385 | private float lastHeight = HEIGHT_INITIAL_LASTHEIGHT; | ||
386 | public float GetTerrainHeightAtXY(float tX, float tY) | ||
387 | { | ||
388 | // You'd be surprized at the number of times this routine is called | ||
389 | // with the same parameters as last time. | ||
390 | if (!m_terrainModified && lastHeightTX == tX && lastHeightTY == tY) | ||
391 | return lastHeight; | ||
392 | |||
393 | lastHeightTX = tX; | ||
394 | lastHeightTY = tY; | ||
395 | float ret = HEIGHT_GETHEIGHT_RET; | ||
396 | |||
397 | int offsetX = ((int)(tX / (int)DefaultRegionSize.X)) * (int)DefaultRegionSize.X; | ||
398 | int offsetY = ((int)(tY / (int)DefaultRegionSize.Y)) * (int)DefaultRegionSize.Y; | ||
399 | Vector2 terrainBaseXY = new Vector2(offsetX, offsetY); | ||
400 | |||
401 | BulletHeightMapInfo mapInfo; | ||
402 | if (m_heightMaps.TryGetValue(terrainBaseXY, out mapInfo)) | ||
403 | { | ||
404 | float regionX = tX - offsetX; | ||
405 | float regionY = tY - offsetY; | ||
406 | if (regionX > mapInfo.sizeX) regionX = 0; | ||
407 | if (regionY > mapInfo.sizeY) regionY = 0; | ||
408 | int mapIndex = (int)regionY * (int)mapInfo.sizeY + (int)regionX; | ||
409 | ret = mapInfo.heightMap[mapIndex]; | ||
410 | m_terrainModified = false; | ||
411 | DetailLog("{0},BSTerrainManager.GetTerrainHeightAtXY,bX={1},baseY={2},szX={3},szY={4},regX={5},regY={6},index={7},ht={8}", | ||
412 | BSScene.DetailLogZero, offsetX, offsetY, mapInfo.sizeX, mapInfo.sizeY, regionX, regionY, mapIndex, ret); | ||
413 | } | ||
414 | else | ||
415 | { | ||
416 | m_physicsScene.Logger.ErrorFormat("{0} GetTerrainHeightAtXY: terrain not found: region={1}, x={2}, y={3}", | ||
417 | LogHeader, m_physicsScene.RegionName, tX, tY); | ||
418 | } | ||
419 | lastHeight = ret; | ||
420 | return ret; | ||
421 | } | ||
422 | |||
423 | // Although no one seems to check this, I do support combining. | ||
424 | public bool SupportsCombining() | ||
425 | { | ||
426 | return true; | ||
427 | } | ||
428 | |||
429 | // This routine is called two ways: | ||
430 | // One with 'offset' and 'pScene' zero and null but 'extents' giving the maximum | ||
431 | // extent of the combined regions. This is to inform the parent of the size | ||
432 | // of the combined regions. | ||
433 | // and one with 'offset' as the offset of the child region to the base region, | ||
434 | // 'pScene' pointing to the parent and 'extents' of zero. This informs the | ||
435 | // child of its relative base and new parent. | ||
436 | public void Combine(PhysicsScene pScene, Vector3 offset, Vector3 extents) | ||
437 | { | ||
438 | m_worldOffset = offset; | ||
439 | m_worldMax = extents; | ||
440 | m_parentScene = pScene; | ||
441 | if (pScene != null) | ||
442 | { | ||
443 | // We are a child. | ||
444 | // We want m_worldMax to be the highest coordinate of our piece of terrain. | ||
445 | m_worldMax = offset + DefaultRegionSize; | ||
446 | } | ||
447 | DetailLog("{0},BSTerrainManager.Combine,offset={1},extents={2},wOffset={3},wMax={4}", | ||
448 | BSScene.DetailLogZero, offset, extents, m_worldOffset, m_worldMax); | ||
449 | } | ||
450 | |||
451 | // Unhook all the combining that I know about. | ||
452 | public void UnCombine(PhysicsScene pScene) | ||
453 | { | ||
454 | // Just like ODE, for the moment a NOP | ||
455 | DetailLog("{0},BSTerrainManager.UnCombine", BSScene.DetailLogZero); | ||
456 | } | ||
457 | |||
458 | |||
459 | private void DetailLog(string msg, params Object[] args) | ||
460 | { | ||
461 | m_physicsScene.PhysicsLogging.Write(msg, args); | ||
462 | } | ||
463 | } | ||
464 | } | ||