diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSScene.cs')
-rw-r--r-- | OpenSim/Region/Physics/BulletSPlugin/BSScene.cs | 860 |
1 files changed, 860 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs b/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs new file mode 100644 index 0000000..7704002 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs | |||
@@ -0,0 +1,860 @@ | |||
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.Runtime.InteropServices; | ||
30 | using System.Text; | ||
31 | using System.Threading; | ||
32 | using Nini.Config; | ||
33 | using log4net; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Region.Physics.Manager; | ||
36 | using OpenMetaverse; | ||
37 | using OpenSim.Region.Framework; | ||
38 | |||
39 | // TODOs for BulletSim (for BSScene, BSPrim, BSCharacter and BulletSim) | ||
40 | // Adjust character capsule size when height is adjusted (ScenePresence.SetHeight) | ||
41 | // Test sculpties | ||
42 | // Compute physics FPS reasonably | ||
43 | // Based on material, set density and friction | ||
44 | // More efficient memory usage in passing hull information from BSPrim to BulletSim | ||
45 | // Four states of prim: Physical, regular, phantom and selected. Are we modeling these correctly? | ||
46 | // In SL one can set both physical and phantom (gravity, does not effect others, makes collisions with ground) | ||
47 | // At the moment, physical and phantom causes object to drop through the terrain | ||
48 | // Should prim.link() and prim.delink() membership checking happen at taint time? | ||
49 | // Mesh sharing. Use meshHash to tell if we already have a hull of that shape and only create once | ||
50 | // Do attachments need to be handled separately? Need collision events. Do not collide with VolumeDetect | ||
51 | // Implement the genCollisions feature in BulletSim::SetObjectProperties (don't pass up unneeded collisions) | ||
52 | // Implement LockAngularMotion | ||
53 | // Decide if clearing forces is the right thing to do when setting position (BulletSim::SetObjectTranslation) | ||
54 | // Does NeedsMeshing() really need to exclude all the different shapes? | ||
55 | // | ||
56 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
57 | { | ||
58 | public class BSScene : PhysicsScene, IPhysicsParameters | ||
59 | { | ||
60 | private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
61 | private static readonly string LogHeader = "[BULLETS SCENE]"; | ||
62 | |||
63 | public string BulletSimVersion = "?"; | ||
64 | |||
65 | private Dictionary<uint, BSCharacter> m_avatars = new Dictionary<uint, BSCharacter>(); | ||
66 | private Dictionary<uint, BSPrim> m_prims = new Dictionary<uint, BSPrim>(); | ||
67 | private List<BSPrim> m_vehicles = new List<BSPrim>(); | ||
68 | private float[] m_heightMap; | ||
69 | private float m_waterLevel; | ||
70 | private uint m_worldID; | ||
71 | public uint WorldID { get { return m_worldID; } } | ||
72 | |||
73 | private bool m_initialized = false; | ||
74 | |||
75 | public IMesher mesher; | ||
76 | private float m_meshLOD; | ||
77 | public float MeshLOD | ||
78 | { | ||
79 | get { return m_meshLOD; } | ||
80 | } | ||
81 | private float m_sculptLOD; | ||
82 | public float SculptLOD | ||
83 | { | ||
84 | get { return m_sculptLOD; } | ||
85 | } | ||
86 | |||
87 | private int m_maxSubSteps; | ||
88 | private float m_fixedTimeStep; | ||
89 | private long m_simulationStep = 0; | ||
90 | public long SimulationStep { get { return m_simulationStep; } } | ||
91 | |||
92 | // A value of the time now so all the collision and update routines do not have to get their own | ||
93 | // Set to 'now' just before all the prims and actors are called for collisions and updates | ||
94 | private int m_simulationNowTime; | ||
95 | public int SimulationNowTime { get { return m_simulationNowTime; } } | ||
96 | |||
97 | private int m_maxCollisionsPerFrame; | ||
98 | private CollisionDesc[] m_collisionArray; | ||
99 | private GCHandle m_collisionArrayPinnedHandle; | ||
100 | |||
101 | private int m_maxUpdatesPerFrame; | ||
102 | private EntityProperties[] m_updateArray; | ||
103 | private GCHandle m_updateArrayPinnedHandle; | ||
104 | |||
105 | private bool _meshSculptedPrim = true; // cause scuplted prims to get meshed | ||
106 | private bool _forceSimplePrimMeshing = false; // if a cube or sphere, let Bullet do internal shapes | ||
107 | |||
108 | public const uint TERRAIN_ID = 0; // OpenSim senses terrain with a localID of zero | ||
109 | public const uint GROUNDPLANE_ID = 1; | ||
110 | |||
111 | public ConfigurationParameters Params | ||
112 | { | ||
113 | get { return m_params[0]; } | ||
114 | } | ||
115 | public Vector3 DefaultGravity | ||
116 | { | ||
117 | get { return new Vector3(0f, 0f, Params.gravity); } | ||
118 | } | ||
119 | |||
120 | private float m_maximumObjectMass; | ||
121 | public float MaximumObjectMass | ||
122 | { | ||
123 | get { return m_maximumObjectMass; } | ||
124 | } | ||
125 | |||
126 | public delegate void TaintCallback(); | ||
127 | private List<TaintCallback> _taintedObjects; | ||
128 | private Object _taintLock = new Object(); | ||
129 | |||
130 | // A pointer to an instance if this structure is passed to the C++ code | ||
131 | ConfigurationParameters[] m_params; | ||
132 | GCHandle m_paramsHandle; | ||
133 | |||
134 | private BulletSimAPI.DebugLogCallback m_DebugLogCallbackHandle; | ||
135 | |||
136 | public BSScene(string identifier) | ||
137 | { | ||
138 | m_initialized = false; | ||
139 | } | ||
140 | |||
141 | public override void Initialise(IMesher meshmerizer, IConfigSource config) | ||
142 | { | ||
143 | // Allocate pinned memory to pass parameters. | ||
144 | m_params = new ConfigurationParameters[1]; | ||
145 | m_paramsHandle = GCHandle.Alloc(m_params, GCHandleType.Pinned); | ||
146 | |||
147 | // Set default values for physics parameters plus any overrides from the ini file | ||
148 | GetInitialParameterValues(config); | ||
149 | |||
150 | // allocate more pinned memory close to the above in an attempt to get the memory all together | ||
151 | m_collisionArray = new CollisionDesc[m_maxCollisionsPerFrame]; | ||
152 | m_collisionArrayPinnedHandle = GCHandle.Alloc(m_collisionArray, GCHandleType.Pinned); | ||
153 | m_updateArray = new EntityProperties[m_maxUpdatesPerFrame]; | ||
154 | m_updateArrayPinnedHandle = GCHandle.Alloc(m_updateArray, GCHandleType.Pinned); | ||
155 | |||
156 | // Get the version of the DLL | ||
157 | // TODO: this doesn't work yet. Something wrong with marshaling the returned string. | ||
158 | // BulletSimVersion = BulletSimAPI.GetVersion(); | ||
159 | // m_log.WarnFormat("{0}: BulletSim.dll version='{1}'", LogHeader, BulletSimVersion); | ||
160 | |||
161 | // if Debug, enable logging from the unmanaged code | ||
162 | if (m_log.IsDebugEnabled) | ||
163 | { | ||
164 | m_log.DebugFormat("{0}: Initialize: Setting debug callback for unmanaged code", LogHeader); | ||
165 | m_DebugLogCallbackHandle = new BulletSimAPI.DebugLogCallback(BulletLogger); | ||
166 | BulletSimAPI.SetDebugLogCallback(m_DebugLogCallbackHandle); | ||
167 | } | ||
168 | |||
169 | _taintedObjects = new List<TaintCallback>(); | ||
170 | |||
171 | mesher = meshmerizer; | ||
172 | // The bounding box for the simulated world | ||
173 | Vector3 worldExtent = new Vector3(Constants.RegionSize, Constants.RegionSize, 4096f); | ||
174 | |||
175 | // m_log.DebugFormat("{0}: Initialize: Calling BulletSimAPI.Initialize.", LogHeader); | ||
176 | m_worldID = BulletSimAPI.Initialize(worldExtent, m_paramsHandle.AddrOfPinnedObject(), | ||
177 | m_maxCollisionsPerFrame, m_collisionArrayPinnedHandle.AddrOfPinnedObject(), | ||
178 | m_maxUpdatesPerFrame, m_updateArrayPinnedHandle.AddrOfPinnedObject()); | ||
179 | |||
180 | m_initialized = true; | ||
181 | } | ||
182 | |||
183 | // All default parameter values are set here. There should be no values set in the | ||
184 | // variable definitions. | ||
185 | private void GetInitialParameterValues(IConfigSource config) | ||
186 | { | ||
187 | ConfigurationParameters parms = new ConfigurationParameters(); | ||
188 | |||
189 | _meshSculptedPrim = true; // mesh sculpted prims | ||
190 | _forceSimplePrimMeshing = false; // use complex meshing if called for | ||
191 | |||
192 | m_meshLOD = 8f; | ||
193 | m_sculptLOD = 32f; | ||
194 | |||
195 | m_maxSubSteps = 10; | ||
196 | m_fixedTimeStep = 1f / 60f; | ||
197 | m_maxCollisionsPerFrame = 2048; | ||
198 | m_maxUpdatesPerFrame = 2048; | ||
199 | m_maximumObjectMass = 10000.01f; | ||
200 | |||
201 | parms.defaultFriction = 0.5f; | ||
202 | parms.defaultDensity = 10.000006836f; // Aluminum g/cm3 | ||
203 | parms.defaultRestitution = 0f; | ||
204 | parms.collisionMargin = 0.0f; | ||
205 | parms.gravity = -9.80665f; | ||
206 | |||
207 | parms.linearDamping = 0.0f; | ||
208 | parms.angularDamping = 0.0f; | ||
209 | parms.deactivationTime = 0.2f; | ||
210 | parms.linearSleepingThreshold = 0.8f; | ||
211 | parms.angularSleepingThreshold = 1.0f; | ||
212 | parms.ccdMotionThreshold = 0.5f; // set to zero to disable | ||
213 | parms.ccdSweptSphereRadius = 0.2f; | ||
214 | |||
215 | parms.terrainFriction = 0.5f; | ||
216 | parms.terrainHitFraction = 0.8f; | ||
217 | parms.terrainRestitution = 0f; | ||
218 | parms.avatarFriction = 0.0f; | ||
219 | parms.avatarDensity = 60f; | ||
220 | parms.avatarCapsuleRadius = 0.37f; | ||
221 | parms.avatarCapsuleHeight = 1.5f; // 2.140599f | ||
222 | |||
223 | if (config != null) | ||
224 | { | ||
225 | // If there are specifications in the ini file, use those values | ||
226 | // WHEN ADDING OR UPDATING THIS SECTION, BE SURE TO UPDATE OpenSimDefaults.ini | ||
227 | // ALSO REMEMBER TO UPDATE THE RUNTIME SETTING OF THE PARAMETERS. | ||
228 | IConfig pConfig = config.Configs["BulletSim"]; | ||
229 | if (pConfig != null) | ||
230 | { | ||
231 | _meshSculptedPrim = pConfig.GetBoolean("MeshSculptedPrim", _meshSculptedPrim); | ||
232 | _forceSimplePrimMeshing = pConfig.GetBoolean("ForceSimplePrimMeshing", _forceSimplePrimMeshing); | ||
233 | |||
234 | m_meshLOD = pConfig.GetFloat("MeshLevelOfDetail", m_meshLOD); | ||
235 | m_sculptLOD = pConfig.GetFloat("SculptLevelOfDetail", m_sculptLOD); | ||
236 | |||
237 | m_maxSubSteps = pConfig.GetInt("MaxSubSteps", m_maxSubSteps); | ||
238 | m_fixedTimeStep = pConfig.GetFloat("FixedTimeStep", m_fixedTimeStep); | ||
239 | m_maxCollisionsPerFrame = pConfig.GetInt("MaxCollisionsPerFrame", m_maxCollisionsPerFrame); | ||
240 | m_maxUpdatesPerFrame = pConfig.GetInt("MaxUpdatesPerFrame", m_maxUpdatesPerFrame); | ||
241 | m_maximumObjectMass = pConfig.GetFloat("MaxObjectMass", m_maximumObjectMass); | ||
242 | |||
243 | parms.defaultFriction = pConfig.GetFloat("DefaultFriction", parms.defaultFriction); | ||
244 | parms.defaultDensity = pConfig.GetFloat("DefaultDensity", parms.defaultDensity); | ||
245 | parms.defaultRestitution = pConfig.GetFloat("DefaultRestitution", parms.defaultRestitution); | ||
246 | parms.collisionMargin = pConfig.GetFloat("CollisionMargin", parms.collisionMargin); | ||
247 | parms.gravity = pConfig.GetFloat("Gravity", parms.gravity); | ||
248 | |||
249 | parms.linearDamping = pConfig.GetFloat("LinearDamping", parms.linearDamping); | ||
250 | parms.angularDamping = pConfig.GetFloat("AngularDamping", parms.angularDamping); | ||
251 | parms.deactivationTime = pConfig.GetFloat("DeactivationTime", parms.deactivationTime); | ||
252 | parms.linearSleepingThreshold = pConfig.GetFloat("LinearSleepingThreshold", parms.linearSleepingThreshold); | ||
253 | parms.angularSleepingThreshold = pConfig.GetFloat("AngularSleepingThreshold", parms.angularSleepingThreshold); | ||
254 | parms.ccdMotionThreshold = pConfig.GetFloat("CcdMotionThreshold", parms.ccdMotionThreshold); | ||
255 | parms.ccdSweptSphereRadius = pConfig.GetFloat("CcdSweptSphereRadius", parms.ccdSweptSphereRadius); | ||
256 | |||
257 | parms.terrainFriction = pConfig.GetFloat("TerrainFriction", parms.terrainFriction); | ||
258 | parms.terrainHitFraction = pConfig.GetFloat("TerrainHitFraction", parms.terrainHitFraction); | ||
259 | parms.terrainRestitution = pConfig.GetFloat("TerrainRestitution", parms.terrainRestitution); | ||
260 | parms.avatarFriction = pConfig.GetFloat("AvatarFriction", parms.avatarFriction); | ||
261 | parms.avatarDensity = pConfig.GetFloat("AvatarDensity", parms.avatarDensity); | ||
262 | parms.avatarCapsuleRadius = pConfig.GetFloat("AvatarCapsuleRadius", parms.avatarCapsuleRadius); | ||
263 | parms.avatarCapsuleHeight = pConfig.GetFloat("AvatarCapsuleHeight", parms.avatarCapsuleHeight); | ||
264 | } | ||
265 | } | ||
266 | m_params[0] = parms; | ||
267 | } | ||
268 | |||
269 | // Called directly from unmanaged code so don't do much | ||
270 | private void BulletLogger(string msg) | ||
271 | { | ||
272 | m_log.Debug("[BULLETS UNMANAGED]:" + msg); | ||
273 | } | ||
274 | |||
275 | public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 size, bool isFlying) | ||
276 | { | ||
277 | m_log.ErrorFormat("{0}: CALL TO AddAvatar in BSScene. NOT IMPLEMENTED", LogHeader); | ||
278 | return null; | ||
279 | } | ||
280 | |||
281 | public override PhysicsActor AddAvatar(uint localID, string avName, Vector3 position, Vector3 size, bool isFlying) | ||
282 | { | ||
283 | // m_log.DebugFormat("{0}: AddAvatar: {1}", LogHeader, avName); | ||
284 | BSCharacter actor = new BSCharacter(localID, avName, this, position, size, isFlying); | ||
285 | lock (m_avatars) m_avatars.Add(localID, actor); | ||
286 | return actor; | ||
287 | } | ||
288 | |||
289 | public override void RemoveAvatar(PhysicsActor actor) | ||
290 | { | ||
291 | // m_log.DebugFormat("{0}: RemoveAvatar", LogHeader); | ||
292 | if (actor is BSCharacter) | ||
293 | { | ||
294 | ((BSCharacter)actor).Destroy(); | ||
295 | } | ||
296 | try | ||
297 | { | ||
298 | lock (m_avatars) m_avatars.Remove(actor.LocalID); | ||
299 | } | ||
300 | catch (Exception e) | ||
301 | { | ||
302 | m_log.WarnFormat("{0}: Attempt to remove avatar that is not in physics scene: {1}", LogHeader, e); | ||
303 | } | ||
304 | } | ||
305 | |||
306 | public override void RemovePrim(PhysicsActor prim) | ||
307 | { | ||
308 | // m_log.DebugFormat("{0}: RemovePrim", LogHeader); | ||
309 | if (prim is BSPrim) | ||
310 | { | ||
311 | ((BSPrim)prim).Destroy(); | ||
312 | } | ||
313 | try | ||
314 | { | ||
315 | lock (m_prims) m_prims.Remove(prim.LocalID); | ||
316 | } | ||
317 | catch (Exception e) | ||
318 | { | ||
319 | m_log.WarnFormat("{0}: Attempt to remove prim that is not in physics scene: {1}", LogHeader, e); | ||
320 | } | ||
321 | } | ||
322 | |||
323 | public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position, | ||
324 | Vector3 size, Quaternion rotation, bool isPhysical, uint localID) | ||
325 | { | ||
326 | // m_log.DebugFormat("{0}: AddPrimShape2: {1}", LogHeader, primName); | ||
327 | BSPrim prim = new BSPrim(localID, primName, this, position, size, rotation, pbs, isPhysical); | ||
328 | lock (m_prims) m_prims.Add(localID, prim); | ||
329 | return prim; | ||
330 | } | ||
331 | |||
332 | // This is a call from the simulator saying that some physical property has been updated. | ||
333 | // The BulletSim driver senses the changing of relevant properties so this taint | ||
334 | // information call is not needed. | ||
335 | public override void AddPhysicsActorTaint(PhysicsActor prim) { } | ||
336 | |||
337 | // Simulate one timestep | ||
338 | public override float Simulate(float timeStep) | ||
339 | { | ||
340 | int updatedEntityCount; | ||
341 | IntPtr updatedEntitiesPtr; | ||
342 | int collidersCount; | ||
343 | IntPtr collidersPtr; | ||
344 | |||
345 | // prevent simulation until we've been initialized | ||
346 | if (!m_initialized) return 10.0f; | ||
347 | |||
348 | // update the prim states while we know the physics engine is not busy | ||
349 | ProcessTaints(); | ||
350 | |||
351 | // Some of the prims operate with special vehicle properties | ||
352 | ProcessVehicles(timeStep); | ||
353 | ProcessTaints(); // the vehicles might have added taints | ||
354 | |||
355 | // step the physical world one interval | ||
356 | m_simulationStep++; | ||
357 | int numSubSteps = BulletSimAPI.PhysicsStep(m_worldID, timeStep, m_maxSubSteps, m_fixedTimeStep, | ||
358 | out updatedEntityCount, out updatedEntitiesPtr, out collidersCount, out collidersPtr); | ||
359 | |||
360 | // Don't have to use the pointers passed back since we know it is the same pinned memory we passed in | ||
361 | |||
362 | // Get a value for 'now' so all the collision and update routines don't have to get their own | ||
363 | m_simulationNowTime = Util.EnvironmentTickCount(); | ||
364 | |||
365 | // If there were collisions, process them by sending the event to the prim. | ||
366 | // Collisions must be processed before updates. | ||
367 | if (collidersCount > 0) | ||
368 | { | ||
369 | for (int ii = 0; ii < collidersCount; ii++) | ||
370 | { | ||
371 | uint cA = m_collisionArray[ii].aID; | ||
372 | uint cB = m_collisionArray[ii].bID; | ||
373 | Vector3 point = m_collisionArray[ii].point; | ||
374 | Vector3 normal = m_collisionArray[ii].normal; | ||
375 | SendCollision(cA, cB, point, normal, 0.01f); | ||
376 | SendCollision(cB, cA, point, -normal, 0.01f); | ||
377 | } | ||
378 | } | ||
379 | |||
380 | // If any of the objects had updated properties, tell the object it has been changed by the physics engine | ||
381 | if (updatedEntityCount > 0) | ||
382 | { | ||
383 | for (int ii = 0; ii < updatedEntityCount; ii++) | ||
384 | { | ||
385 | EntityProperties entprop = m_updateArray[ii]; | ||
386 | // m_log.DebugFormat("{0}: entprop[{1}]: id={2}, pos={3}", LogHeader, ii, entprop.ID, entprop.Position); | ||
387 | BSCharacter actor; | ||
388 | if (m_avatars.TryGetValue(entprop.ID, out actor)) | ||
389 | { | ||
390 | actor.UpdateProperties(entprop); | ||
391 | continue; | ||
392 | } | ||
393 | BSPrim prim; | ||
394 | if (m_prims.TryGetValue(entprop.ID, out prim)) | ||
395 | { | ||
396 | prim.UpdateProperties(entprop); | ||
397 | } | ||
398 | } | ||
399 | } | ||
400 | |||
401 | // TODO: FIX THIS: fps calculation wrong. This calculation always returns about 1 in normal operation. | ||
402 | return timeStep / (numSubSteps * m_fixedTimeStep) * 1000f; | ||
403 | } | ||
404 | |||
405 | // Something has collided | ||
406 | private void SendCollision(uint localID, uint collidingWith, Vector3 collidePoint, Vector3 collideNormal, float penitration) | ||
407 | { | ||
408 | if (localID == TERRAIN_ID || localID == GROUNDPLANE_ID) | ||
409 | { | ||
410 | return; // don't send collisions to the terrain | ||
411 | } | ||
412 | |||
413 | ActorTypes type = ActorTypes.Prim; | ||
414 | if (collidingWith == TERRAIN_ID || collidingWith == GROUNDPLANE_ID) | ||
415 | type = ActorTypes.Ground; | ||
416 | else if (m_avatars.ContainsKey(collidingWith)) | ||
417 | type = ActorTypes.Agent; | ||
418 | |||
419 | BSPrim prim; | ||
420 | if (m_prims.TryGetValue(localID, out prim)) { | ||
421 | prim.Collide(collidingWith, type, collidePoint, collideNormal, penitration); | ||
422 | return; | ||
423 | } | ||
424 | BSCharacter actor; | ||
425 | if (m_avatars.TryGetValue(localID, out actor)) { | ||
426 | actor.Collide(collidingWith, type, collidePoint, collideNormal, penitration); | ||
427 | return; | ||
428 | } | ||
429 | return; | ||
430 | } | ||
431 | |||
432 | public override void GetResults() { } | ||
433 | |||
434 | public override void SetTerrain(float[] heightMap) { | ||
435 | m_heightMap = heightMap; | ||
436 | this.TaintedObject(delegate() | ||
437 | { | ||
438 | BulletSimAPI.SetHeightmap(m_worldID, m_heightMap); | ||
439 | }); | ||
440 | } | ||
441 | |||
442 | public float GetTerrainHeightAtXY(float tX, float tY) | ||
443 | { | ||
444 | return m_heightMap[((int)tX) * Constants.RegionSize + ((int)tY)]; | ||
445 | } | ||
446 | |||
447 | public override void SetWaterLevel(float baseheight) | ||
448 | { | ||
449 | m_waterLevel = baseheight; | ||
450 | } | ||
451 | public float GetWaterLevel() | ||
452 | { | ||
453 | return m_waterLevel; | ||
454 | } | ||
455 | |||
456 | public override void DeleteTerrain() | ||
457 | { | ||
458 | m_log.DebugFormat("{0}: DeleteTerrain()", LogHeader); | ||
459 | } | ||
460 | |||
461 | public override void Dispose() | ||
462 | { | ||
463 | m_log.DebugFormat("{0}: Dispose()", LogHeader); | ||
464 | } | ||
465 | |||
466 | public override Dictionary<uint, float> GetTopColliders() | ||
467 | { | ||
468 | return new Dictionary<uint, float>(); | ||
469 | } | ||
470 | |||
471 | public override bool IsThreaded { get { return false; } } | ||
472 | |||
473 | /// <summary> | ||
474 | /// Routine to figure out if we need to mesh this prim with our mesher | ||
475 | /// </summary> | ||
476 | /// <param name="pbs"></param> | ||
477 | /// <returns>true if the prim needs meshing</returns> | ||
478 | public bool NeedsMeshing(PrimitiveBaseShape pbs) | ||
479 | { | ||
480 | // most of this is redundant now as the mesher will return null if it cant mesh a prim | ||
481 | // but we still need to check for sculptie meshing being enabled so this is the most | ||
482 | // convenient place to do it for now... | ||
483 | |||
484 | // int iPropertiesNotSupportedDefault = 0; | ||
485 | |||
486 | if (pbs.SculptEntry && !_meshSculptedPrim) | ||
487 | { | ||
488 | // Render sculpties as boxes | ||
489 | return false; | ||
490 | } | ||
491 | |||
492 | // if it's a standard box or sphere with no cuts, hollows, twist or top shear, return false since Bullet | ||
493 | // can use an internal representation for the prim | ||
494 | if (!_forceSimplePrimMeshing) | ||
495 | { | ||
496 | if ((pbs.ProfileShape == ProfileShape.Square && pbs.PathCurve == (byte)Extrusion.Straight) | ||
497 | || (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1 | ||
498 | && pbs.Scale.X == pbs.Scale.Y && pbs.Scale.Y == pbs.Scale.Z)) | ||
499 | { | ||
500 | |||
501 | if (pbs.ProfileBegin == 0 && pbs.ProfileEnd == 0 | ||
502 | && pbs.ProfileHollow == 0 | ||
503 | && pbs.PathTwist == 0 && pbs.PathTwistBegin == 0 | ||
504 | && pbs.PathBegin == 0 && pbs.PathEnd == 0 | ||
505 | && pbs.PathTaperX == 0 && pbs.PathTaperY == 0 | ||
506 | && pbs.PathScaleX == 100 && pbs.PathScaleY == 100 | ||
507 | && pbs.PathShearX == 0 && pbs.PathShearY == 0) | ||
508 | { | ||
509 | return false; | ||
510 | } | ||
511 | } | ||
512 | } | ||
513 | |||
514 | /* TODO: verify that the mesher will now do all these shapes | ||
515 | if (pbs.ProfileHollow != 0) | ||
516 | iPropertiesNotSupportedDefault++; | ||
517 | |||
518 | if ((pbs.PathBegin != 0) || pbs.PathEnd != 0) | ||
519 | iPropertiesNotSupportedDefault++; | ||
520 | |||
521 | if ((pbs.PathTwistBegin != 0) || (pbs.PathTwist != 0)) | ||
522 | iPropertiesNotSupportedDefault++; | ||
523 | |||
524 | if ((pbs.ProfileBegin != 0) || pbs.ProfileEnd != 0) | ||
525 | iPropertiesNotSupportedDefault++; | ||
526 | |||
527 | if ((pbs.PathScaleX != 100) || (pbs.PathScaleY != 100)) | ||
528 | iPropertiesNotSupportedDefault++; | ||
529 | |||
530 | if ((pbs.PathShearX != 0) || (pbs.PathShearY != 0)) | ||
531 | iPropertiesNotSupportedDefault++; | ||
532 | |||
533 | if (pbs.ProfileShape == ProfileShape.Circle && pbs.PathCurve == (byte)Extrusion.Straight) | ||
534 | iPropertiesNotSupportedDefault++; | ||
535 | |||
536 | if (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1 && (pbs.Scale.X != pbs.Scale.Y || pbs.Scale.Y != pbs.Scale.Z || pbs.Scale.Z != pbs.Scale.X)) | ||
537 | iPropertiesNotSupportedDefault++; | ||
538 | |||
539 | if (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte) Extrusion.Curve1) | ||
540 | iPropertiesNotSupportedDefault++; | ||
541 | |||
542 | // test for torus | ||
543 | if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.Square) | ||
544 | { | ||
545 | if (pbs.PathCurve == (byte)Extrusion.Curve1) | ||
546 | { | ||
547 | iPropertiesNotSupportedDefault++; | ||
548 | } | ||
549 | } | ||
550 | else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.Circle) | ||
551 | { | ||
552 | if (pbs.PathCurve == (byte)Extrusion.Straight) | ||
553 | { | ||
554 | iPropertiesNotSupportedDefault++; | ||
555 | } | ||
556 | // ProfileCurve seems to combine hole shape and profile curve so we need to only compare against the lower 3 bits | ||
557 | else if (pbs.PathCurve == (byte)Extrusion.Curve1) | ||
558 | { | ||
559 | iPropertiesNotSupportedDefault++; | ||
560 | } | ||
561 | } | ||
562 | else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle) | ||
563 | { | ||
564 | if (pbs.PathCurve == (byte)Extrusion.Curve1 || pbs.PathCurve == (byte)Extrusion.Curve2) | ||
565 | { | ||
566 | iPropertiesNotSupportedDefault++; | ||
567 | } | ||
568 | } | ||
569 | else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle) | ||
570 | { | ||
571 | if (pbs.PathCurve == (byte)Extrusion.Straight) | ||
572 | { | ||
573 | iPropertiesNotSupportedDefault++; | ||
574 | } | ||
575 | else if (pbs.PathCurve == (byte)Extrusion.Curve1) | ||
576 | { | ||
577 | iPropertiesNotSupportedDefault++; | ||
578 | } | ||
579 | } | ||
580 | if (iPropertiesNotSupportedDefault == 0) | ||
581 | { | ||
582 | return false; | ||
583 | } | ||
584 | */ | ||
585 | return true; | ||
586 | } | ||
587 | |||
588 | // The calls to the PhysicsActors can't directly call into the physics engine | ||
589 | // because it might be busy. We we delay changes to a known time. | ||
590 | // We rely on C#'s closure to save and restore the context for the delegate. | ||
591 | public void TaintedObject(TaintCallback callback) | ||
592 | { | ||
593 | lock (_taintLock) | ||
594 | _taintedObjects.Add(callback); | ||
595 | return; | ||
596 | } | ||
597 | |||
598 | // When someone tries to change a property on a BSPrim or BSCharacter, the object queues | ||
599 | // a callback into itself to do the actual property change. That callback is called | ||
600 | // here just before the physics engine is called to step the simulation. | ||
601 | public void ProcessTaints() | ||
602 | { | ||
603 | if (_taintedObjects.Count > 0) // save allocating new list if there is nothing to process | ||
604 | { | ||
605 | // swizzle a new list into the list location so we can process what's there | ||
606 | List<TaintCallback> oldList; | ||
607 | lock (_taintLock) | ||
608 | { | ||
609 | oldList = _taintedObjects; | ||
610 | _taintedObjects = new List<TaintCallback>(); | ||
611 | } | ||
612 | |||
613 | foreach (TaintCallback callback in oldList) | ||
614 | { | ||
615 | try | ||
616 | { | ||
617 | callback(); | ||
618 | } | ||
619 | catch (Exception e) | ||
620 | { | ||
621 | m_log.ErrorFormat("{0}: ProcessTaints: Exception: {1}", LogHeader, e); | ||
622 | } | ||
623 | } | ||
624 | oldList.Clear(); | ||
625 | } | ||
626 | } | ||
627 | |||
628 | #region Vehicles | ||
629 | // Make so the scene will call this prim for vehicle actions each tick. | ||
630 | // Safe to call if prim is already in the vehicle list. | ||
631 | public void AddVehiclePrim(BSPrim vehicle) | ||
632 | { | ||
633 | lock (m_vehicles) | ||
634 | { | ||
635 | if (!m_vehicles.Contains(vehicle)) | ||
636 | { | ||
637 | m_vehicles.Add(vehicle); | ||
638 | } | ||
639 | } | ||
640 | } | ||
641 | |||
642 | // Remove a prim from our list of vehicles. | ||
643 | // Safe to call if the prim is not in the vehicle list. | ||
644 | public void RemoveVehiclePrim(BSPrim vehicle) | ||
645 | { | ||
646 | lock (m_vehicles) | ||
647 | { | ||
648 | if (m_vehicles.Contains(vehicle)) | ||
649 | { | ||
650 | m_vehicles.Remove(vehicle); | ||
651 | } | ||
652 | } | ||
653 | } | ||
654 | |||
655 | // Some prims have extra vehicle actions | ||
656 | // no locking because only called when physics engine is not busy | ||
657 | private void ProcessVehicles(float timeStep) | ||
658 | { | ||
659 | foreach (BSPrim prim in m_vehicles) | ||
660 | { | ||
661 | prim.StepVehicle(timeStep); | ||
662 | } | ||
663 | } | ||
664 | #endregion Vehicles | ||
665 | |||
666 | #region Runtime settable parameters | ||
667 | public static PhysParameterEntry[] SettableParameters = new PhysParameterEntry[] | ||
668 | { | ||
669 | new PhysParameterEntry("MeshLOD", "Level of detail to render meshes (32, 16, 8 or 4. 32=most detailed)"), | ||
670 | new PhysParameterEntry("SculptLOD", "Level of detail to render sculpties (32, 16, 8 or 4. 32=most detailed)"), | ||
671 | new PhysParameterEntry("MaxSubStep", "In simulation step, maximum number of substeps"), | ||
672 | new PhysParameterEntry("FixedTimeStep", "In simulation step, seconds of one substep (1/60)"), | ||
673 | new PhysParameterEntry("MaxObjectMass", "Maximum object mass (10000.01)"), | ||
674 | |||
675 | new PhysParameterEntry("DefaultFriction", "Friction factor used on new objects"), | ||
676 | new PhysParameterEntry("DefaultDensity", "Density for new objects" ), | ||
677 | new PhysParameterEntry("DefaultRestitution", "Bouncyness of an object" ), | ||
678 | // new PhysParameterEntry("CollisionMargin", "Margin around objects before collisions are calculated (must be zero!!)" ), | ||
679 | new PhysParameterEntry("Gravity", "Vertical force of gravity (negative means down)" ), | ||
680 | |||
681 | new PhysParameterEntry("LinearDamping", "Factor to damp linear movement per second (0.0 - 1.0)" ), | ||
682 | new PhysParameterEntry("AngularDamping", "Factor to damp angular movement per second (0.0 - 1.0)" ), | ||
683 | new PhysParameterEntry("DeactivationTime", "Seconds before considering an object potentially static" ), | ||
684 | new PhysParameterEntry("LinearSleepingThreshold", "Seconds to measure linear movement before considering static" ), | ||
685 | new PhysParameterEntry("AngularSleepingThreshold", "Seconds to measure angular movement before considering static" ), | ||
686 | // new PhysParameterEntry("CcdMotionThreshold", "" ), | ||
687 | // new PhysParameterEntry("CcdSweptSphereRadius", "" ), | ||
688 | |||
689 | new PhysParameterEntry("TerrainFriction", "Factor to reduce movement against terrain surface" ), | ||
690 | new PhysParameterEntry("TerrainHitFraction", "Distance to measure hit collisions" ), | ||
691 | new PhysParameterEntry("TerrainRestitution", "Bouncyness" ), | ||
692 | new PhysParameterEntry("AvatarFriction", "Factor to reduce movement against an avatar. Changed on avatar recreation." ), | ||
693 | new PhysParameterEntry("AvatarDensity", "Density of an avatar. Changed on avatar recreation." ), | ||
694 | new PhysParameterEntry("AvatarRestitution", "Bouncyness. Changed on avatar recreation." ), | ||
695 | new PhysParameterEntry("AvatarCapsuleRadius", "Radius of space around an avatar" ), | ||
696 | new PhysParameterEntry("AvatarCapsuleHeight", "Default height of space around avatar" ) | ||
697 | }; | ||
698 | |||
699 | #region IPhysicsParameters | ||
700 | // Get the list of parameters this physics engine supports | ||
701 | public PhysParameterEntry[] GetParameterList() | ||
702 | { | ||
703 | return SettableParameters; | ||
704 | } | ||
705 | |||
706 | // Set parameter on a specific or all instances. | ||
707 | // Return 'false' if not able to set the parameter. | ||
708 | // Setting the value in the m_params block will change the value the physics engine | ||
709 | // will use the next time since it's pinned and shared memory. | ||
710 | // Some of the values require calling into the physics engine to get the new | ||
711 | // value activated ('terrainFriction' for instance). | ||
712 | public bool SetPhysicsParameter(string parm, float val, uint localID) | ||
713 | { | ||
714 | bool ret = true; | ||
715 | string lparm = parm.ToLower(); | ||
716 | switch (lparm) | ||
717 | { | ||
718 | case "meshlod": m_meshLOD = (int)val; break; | ||
719 | case "sculptlod": m_sculptLOD = (int)val; break; | ||
720 | case "maxsubstep": m_maxSubSteps = (int)val; break; | ||
721 | case "fixedtimestep": m_fixedTimeStep = val; break; | ||
722 | case "maxobjectmass": m_maximumObjectMass = val; break; | ||
723 | |||
724 | case "defaultfriction": m_params[0].defaultFriction = val; break; | ||
725 | case "defaultdensity": m_params[0].defaultDensity = val; break; | ||
726 | case "defaultrestitution": m_params[0].defaultRestitution = val; break; | ||
727 | case "collisionmargin": m_params[0].collisionMargin = val; break; | ||
728 | case "gravity": m_params[0].gravity = val; TaintedUpdateParameter(lparm, PhysParameterEntry.APPLY_TO_NONE, val); break; | ||
729 | |||
730 | case "lineardamping": UpdateParameterPrims(ref m_params[0].linearDamping, lparm, localID, val); break; | ||
731 | case "angulardamping": UpdateParameterPrims(ref m_params[0].angularDamping, lparm, localID, val); break; | ||
732 | case "deactivationtime": UpdateParameterPrims(ref m_params[0].deactivationTime, lparm, localID, val); break; | ||
733 | case "linearsleepingthreshold": UpdateParameterPrims(ref m_params[0].linearSleepingThreshold, lparm, localID, val); break; | ||
734 | case "angularsleepingthreshold": UpdateParameterPrims(ref m_params[0].angularDamping, lparm, localID, val); break; | ||
735 | case "ccdmotionthreshold": UpdateParameterPrims(ref m_params[0].ccdMotionThreshold, lparm, localID, val); break; | ||
736 | case "ccdsweptsphereradius": UpdateParameterPrims(ref m_params[0].ccdSweptSphereRadius, lparm, localID, val); break; | ||
737 | |||
738 | // set a terrain physical feature and cause terrain to be recalculated | ||
739 | case "terrainfriction": m_params[0].terrainFriction = val; TaintedUpdateParameter("terrain", 0, val); break; | ||
740 | case "terrainhitfraction": m_params[0].terrainHitFraction = val; TaintedUpdateParameter("terrain", 0, val); break; | ||
741 | case "terrainrestitution": m_params[0].terrainRestitution = val; TaintedUpdateParameter("terrain", 0, val); break; | ||
742 | // set an avatar physical feature and cause avatar(s) to be recalculated | ||
743 | case "avatarfriction": UpdateParameterAvatars(ref m_params[0].avatarFriction, "avatar", localID, val); break; | ||
744 | case "avatardensity": UpdateParameterAvatars(ref m_params[0].avatarDensity, "avatar", localID, val); break; | ||
745 | case "avatarrestitution": UpdateParameterAvatars(ref m_params[0].avatarRestitution, "avatar", localID, val); break; | ||
746 | case "avatarcapsuleradius": UpdateParameterAvatars(ref m_params[0].avatarCapsuleRadius, "avatar", localID, val); break; | ||
747 | case "avatarcapsuleheight": UpdateParameterAvatars(ref m_params[0].avatarCapsuleHeight, "avatar", localID, val); break; | ||
748 | |||
749 | default: ret = false; break; | ||
750 | } | ||
751 | return ret; | ||
752 | } | ||
753 | |||
754 | // check to see if we are updating a parameter for a particular or all of the prims | ||
755 | private void UpdateParameterPrims(ref float loc, string parm, uint localID, float val) | ||
756 | { | ||
757 | List<uint> operateOn; | ||
758 | lock (m_prims) operateOn = new List<uint>(m_prims.Keys); | ||
759 | UpdateParameterSet(operateOn, ref loc, parm, localID, val); | ||
760 | } | ||
761 | |||
762 | // check to see if we are updating a parameter for a particular or all of the avatars | ||
763 | private void UpdateParameterAvatars(ref float loc, string parm, uint localID, float val) | ||
764 | { | ||
765 | List<uint> operateOn; | ||
766 | lock (m_avatars) operateOn = new List<uint>(m_avatars.Keys); | ||
767 | UpdateParameterSet(operateOn, ref loc, parm, localID, val); | ||
768 | } | ||
769 | |||
770 | // update all the localIDs specified | ||
771 | // If the local ID is APPLY_TO_NONE, just change the default value | ||
772 | // If the localID is APPLY_TO_ALL change the default value and apply the new value to all the lIDs | ||
773 | // If the localID is a specific object, apply the parameter change to only that object | ||
774 | private void UpdateParameterSet(List<uint> lIDs, ref float defaultLoc, string parm, uint localID, float val) | ||
775 | { | ||
776 | switch (localID) | ||
777 | { | ||
778 | case PhysParameterEntry.APPLY_TO_NONE: | ||
779 | defaultLoc = val; // setting only the default value | ||
780 | break; | ||
781 | case PhysParameterEntry.APPLY_TO_ALL: | ||
782 | defaultLoc = val; // setting ALL also sets the default value | ||
783 | List<uint> objectIDs = lIDs; | ||
784 | string xparm = parm.ToLower(); | ||
785 | float xval = val; | ||
786 | TaintedObject(delegate() { | ||
787 | foreach (uint lID in objectIDs) | ||
788 | { | ||
789 | BulletSimAPI.UpdateParameter(m_worldID, lID, xparm, xval); | ||
790 | } | ||
791 | }); | ||
792 | break; | ||
793 | default: | ||
794 | // setting only one localID | ||
795 | TaintedUpdateParameter(parm, localID, val); | ||
796 | break; | ||
797 | } | ||
798 | } | ||
799 | |||
800 | // schedule the actual updating of the paramter to when the phys engine is not busy | ||
801 | private void TaintedUpdateParameter(string parm, uint localID, float val) | ||
802 | { | ||
803 | uint xlocalID = localID; | ||
804 | string xparm = parm.ToLower(); | ||
805 | float xval = val; | ||
806 | TaintedObject(delegate() { | ||
807 | BulletSimAPI.UpdateParameter(m_worldID, xlocalID, xparm, xval); | ||
808 | }); | ||
809 | } | ||
810 | |||
811 | // Get parameter. | ||
812 | // Return 'false' if not able to get the parameter. | ||
813 | public bool GetPhysicsParameter(string parm, out float value) | ||
814 | { | ||
815 | float val = 0f; | ||
816 | bool ret = true; | ||
817 | switch (parm.ToLower()) | ||
818 | { | ||
819 | case "meshlod": val = (float)m_meshLOD; break; | ||
820 | case "sculptlod": val = (float)m_sculptLOD; break; | ||
821 | case "maxsubstep": val = (float)m_maxSubSteps; break; | ||
822 | case "fixedtimestep": val = m_fixedTimeStep; break; | ||
823 | case "maxobjectmass": val = m_maximumObjectMass; break; | ||
824 | |||
825 | case "defaultfriction": val = m_params[0].defaultFriction; break; | ||
826 | case "defaultdensity": val = m_params[0].defaultDensity; break; | ||
827 | case "defaultrestitution": val = m_params[0].defaultRestitution; break; | ||
828 | case "collisionmargin": val = m_params[0].collisionMargin; break; | ||
829 | case "gravity": val = m_params[0].gravity; break; | ||
830 | |||
831 | case "lineardamping": val = m_params[0].linearDamping; break; | ||
832 | case "angulardamping": val = m_params[0].angularDamping; break; | ||
833 | case "deactivationtime": val = m_params[0].deactivationTime; break; | ||
834 | case "linearsleepingthreshold": val = m_params[0].linearSleepingThreshold; break; | ||
835 | case "angularsleepingthreshold": val = m_params[0].angularDamping; break; | ||
836 | case "ccdmotionthreshold": val = m_params[0].ccdMotionThreshold; break; | ||
837 | case "ccdsweptsphereradius": val = m_params[0].ccdSweptSphereRadius; break; | ||
838 | |||
839 | case "terrainfriction": val = m_params[0].terrainFriction; break; | ||
840 | case "terrainhitfraction": val = m_params[0].terrainHitFraction; break; | ||
841 | case "terrainrestitution": val = m_params[0].terrainRestitution; break; | ||
842 | |||
843 | case "avatarfriction": val = m_params[0].avatarFriction; break; | ||
844 | case "avatardensity": val = m_params[0].avatarDensity; break; | ||
845 | case "avatarrestitution": val = m_params[0].avatarRestitution; break; | ||
846 | case "avatarcapsuleradius": val = m_params[0].avatarCapsuleRadius; break; | ||
847 | case "avatarcapsuleheight": val = m_params[0].avatarCapsuleHeight; break; | ||
848 | default: ret = false; break; | ||
849 | |||
850 | } | ||
851 | value = val; | ||
852 | return ret; | ||
853 | } | ||
854 | |||
855 | #endregion IPhysicsParameters | ||
856 | |||
857 | #endregion Runtime settable parameters | ||
858 | |||
859 | } | ||
860 | } | ||