BSScene.cs 37 KB

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