1
0

BSPrim.cs 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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.Reflection;
  29. using System.Collections.Generic;
  30. using System.Xml;
  31. using log4net;
  32. using OMV = OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.PhysicsModules.SharedBase;
  35. using OpenSim.Region.PhysicsModules.ConvexDecompositionDotNet;
  36. namespace OpenSim.Region.PhysicsModule.BulletS
  37. {
  38. [Serializable]
  39. public class BSPrim : BSPhysObject
  40. {
  41. protected static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private static readonly string LogHeader = "[BULLETS PRIM]";
  43. // _size is what the user passed. Scale is what we pass to the physics engine with the mesh.
  44. private OMV.Vector3 _size; // the multiplier for each mesh dimension as passed by the user
  45. private bool _grabbed;
  46. private bool _isSelected;
  47. private bool _isVolumeDetect;
  48. private float _mass; // the mass of this object
  49. private OMV.Vector3 _acceleration;
  50. private int _physicsActorType;
  51. private bool _isPhysical;
  52. private bool _flying;
  53. private bool _setAlwaysRun;
  54. private bool _throttleUpdates;
  55. private bool _floatOnWater;
  56. private bool _kinematic;
  57. private float _buoyancy;
  58. private int CrossingFailures { get; set; }
  59. // Keep a handle to the vehicle actor so it is easy to set parameters on same.
  60. public const string VehicleActorName = "BasicVehicle";
  61. // Parameters for the hover actor
  62. public const string HoverActorName = "BSPrim.HoverActor";
  63. // Parameters for the axis lock actor
  64. public const String LockedAxisActorName = "BSPrim.LockedAxis";
  65. // Parameters for the move to target actor
  66. public const string MoveToTargetActorName = "BSPrim.MoveToTargetActor";
  67. // Parameters for the setForce and setTorque actors
  68. public const string SetForceActorName = "BSPrim.SetForceActor";
  69. public const string SetTorqueActorName = "BSPrim.SetTorqueActor";
  70. public BSPrim(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
  71. OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
  72. : base(parent_scene, localID, primName, "BSPrim")
  73. {
  74. // m_log.DebugFormat("{0}: BSPrim creation of {1}, id={2}", LogHeader, primName, localID);
  75. _physicsActorType = (int)ActorTypes.Prim;
  76. RawPosition = pos;
  77. _size = size;
  78. Scale = size; // prims are the size the user wants them to be (different for BSCharactes).
  79. RawOrientation = rotation;
  80. _buoyancy = 0f;
  81. RawVelocity = OMV.Vector3.Zero;
  82. RawRotationalVelocity = OMV.Vector3.Zero;
  83. BaseShape = pbs;
  84. _isPhysical = pisPhysical;
  85. _isVolumeDetect = false;
  86. _mass = CalculateMass();
  87. DetailLog("{0},BSPrim.constructor,pbs={1}", LocalID, BSScene.PrimitiveBaseShapeToString(pbs));
  88. // DetailLog("{0},BSPrim.constructor,call", LocalID);
  89. // do the actual object creation at taint time
  90. PhysScene.TaintedObject(LocalID, "BSPrim.create", delegate()
  91. {
  92. // Make sure the object is being created with some sanity.
  93. ExtremeSanityCheck(true /* inTaintTime */);
  94. CreateGeomAndObject(true);
  95. CurrentCollisionFlags = PhysScene.PE.GetCollisionFlags(PhysBody);
  96. IsInitialized = true;
  97. });
  98. }
  99. // called when this prim is being destroyed and we should free all the resources
  100. public override void Destroy()
  101. {
  102. // m_log.DebugFormat("{0}: Destroy, id={1}", LogHeader, LocalID);
  103. IsInitialized = false;
  104. base.Destroy();
  105. // Undo any vehicle properties
  106. this.VehicleType = (int)Vehicle.TYPE_NONE;
  107. PhysScene.TaintedObject(LocalID, "BSPrim.Destroy", delegate()
  108. {
  109. DetailLog("{0},BSPrim.Destroy,taint,", LocalID);
  110. // If there are physical body and shape, release my use of same.
  111. PhysScene.Shapes.DereferenceBody(PhysBody, null);
  112. PhysBody.Clear();
  113. PhysShape.Dereference(PhysScene);
  114. PhysShape = new BSShapeNull();
  115. });
  116. }
  117. // No one uses this property.
  118. public override bool Stopped {
  119. get { return false; }
  120. }
  121. public override bool IsIncomplete {
  122. get {
  123. return ShapeRebuildScheduled;
  124. }
  125. }
  126. // 'true' if this object's shape is in need of a rebuild and a rebuild has been queued.
  127. // The prim is still available but its underlying shape will change soon.
  128. // This is protected by a 'lock(this)'.
  129. public bool ShapeRebuildScheduled { get; protected set; }
  130. public override OMV.Vector3 Size {
  131. get { return _size; }
  132. set {
  133. // We presume the scale and size are the same. If scale must be changed for
  134. // the physical shape, that is done when the geometry is built.
  135. _size = value;
  136. Scale = _size;
  137. ForceBodyShapeRebuild(false);
  138. }
  139. }
  140. public override PrimitiveBaseShape Shape {
  141. set {
  142. BaseShape = value;
  143. DetailLog("{0},BSPrim.changeShape,pbs={1}", LocalID, BSScene.PrimitiveBaseShapeToString(BaseShape));
  144. PrimAssetState = PrimAssetCondition.Unknown;
  145. ForceBodyShapeRebuild(false);
  146. }
  147. }
  148. // Cause the body and shape of the prim to be rebuilt if necessary.
  149. // If there are no changes required, this is quick and does not make changes to the prim.
  150. // If rebuilding is necessary (like changing from static to physical), that will happen.
  151. // The 'ShapeRebuildScheduled' tells any checker that the body/shape may change shortly.
  152. // The return parameter is not used by anyone.
  153. public override bool ForceBodyShapeRebuild(bool inTaintTime)
  154. {
  155. if (inTaintTime)
  156. {
  157. // If called in taint time, do the operation immediately
  158. _mass = CalculateMass(); // changing the shape changes the mass
  159. CreateGeomAndObject(true);
  160. }
  161. else
  162. {
  163. lock (this)
  164. {
  165. // If a rebuild is not already in the queue
  166. if (!ShapeRebuildScheduled)
  167. {
  168. // Remember that a rebuild is queued -- this is used to flag an incomplete object
  169. ShapeRebuildScheduled = true;
  170. PhysScene.TaintedObject(LocalID, "BSPrim.ForceBodyShapeRebuild", delegate()
  171. {
  172. _mass = CalculateMass(); // changing the shape changes the mass
  173. CreateGeomAndObject(true);
  174. ShapeRebuildScheduled = false;
  175. });
  176. }
  177. }
  178. }
  179. return true;
  180. }
  181. public override bool Grabbed {
  182. set { _grabbed = value;
  183. }
  184. }
  185. public override bool Selected {
  186. set
  187. {
  188. if (value != _isSelected)
  189. {
  190. _isSelected = value;
  191. PhysScene.TaintedObject(LocalID, "BSPrim.setSelected", delegate()
  192. {
  193. DetailLog("{0},BSPrim.selected,taint,selected={1}", LocalID, _isSelected);
  194. SetObjectDynamic(false);
  195. });
  196. }
  197. }
  198. }
  199. public override bool IsSelected
  200. {
  201. get { return _isSelected; }
  202. }
  203. public override void CrossingFailure()
  204. {
  205. CrossingFailures++;
  206. if (CrossingFailures > BSParam.CrossingFailuresBeforeOutOfBounds)
  207. {
  208. base.RaiseOutOfBounds(RawPosition);
  209. }
  210. else if (CrossingFailures == BSParam.CrossingFailuresBeforeOutOfBounds)
  211. {
  212. m_log.WarnFormat("{0} Too many crossing failures for {1}", LogHeader, Name);
  213. }
  214. return;
  215. }
  216. // link me to the specified parent
  217. public override void link(PhysicsActor obj) {
  218. }
  219. // delink me from my linkset
  220. public override void delink() {
  221. }
  222. // Set motion values to zero.
  223. // Do it to the properties so the values get set in the physics engine.
  224. // Push the setting of the values to the viewer.
  225. // Called at taint time!
  226. public override void ZeroMotion(bool inTaintTime)
  227. {
  228. RawVelocity = OMV.Vector3.Zero;
  229. _acceleration = OMV.Vector3.Zero;
  230. RawRotationalVelocity = OMV.Vector3.Zero;
  231. // Zero some other properties in the physics engine
  232. PhysScene.TaintedObject(inTaintTime, LocalID, "BSPrim.ZeroMotion", delegate()
  233. {
  234. if (PhysBody.HasPhysicalBody)
  235. PhysScene.PE.ClearAllForces(PhysBody);
  236. });
  237. }
  238. public override void ZeroAngularMotion(bool inTaintTime)
  239. {
  240. RawRotationalVelocity = OMV.Vector3.Zero;
  241. // Zero some other properties in the physics engine
  242. PhysScene.TaintedObject(inTaintTime, LocalID, "BSPrim.ZeroMotion", delegate()
  243. {
  244. // DetailLog("{0},BSPrim.ZeroAngularMotion,call,rotVel={1}", LocalID, _rotationalVelocity);
  245. if (PhysBody.HasPhysicalBody)
  246. {
  247. PhysScene.PE.SetInterpolationAngularVelocity(PhysBody, RawRotationalVelocity);
  248. PhysScene.PE.SetAngularVelocity(PhysBody, RawRotationalVelocity);
  249. }
  250. });
  251. }
  252. public override void LockAngularMotion(byte axislocks)
  253. {
  254. DetailLog("{0},BSPrim.LockAngularMotion,call,axis={1}", LocalID, axislocks);
  255. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR, 0f, 0f);
  256. if ((axislocks & 0x02) != 0)
  257. {
  258. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_X, 0f, 0f);
  259. }
  260. if ((axislocks & 0x04) != 0)
  261. {
  262. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Y, 0f, 0f);
  263. }
  264. if ((axislocks & 0x08) != 0)
  265. {
  266. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Z, 0f, 0f);
  267. }
  268. InitializeAxisActor();
  269. return;
  270. }
  271. public override OMV.Vector3 Position {
  272. get {
  273. // don't do the GetObjectPosition for root elements because this function is called a zillion times.
  274. // RawPosition = ForcePosition;
  275. return RawPosition;
  276. }
  277. set {
  278. // If the position must be forced into the physics engine, use ForcePosition.
  279. // All positions are given in world positions.
  280. if (RawPosition == value)
  281. {
  282. DetailLog("{0},BSPrim.setPosition,call,positionNotChanging,pos={1},orient={2}", LocalID, RawPosition, RawOrientation);
  283. return;
  284. }
  285. RawPosition = value;
  286. PositionSanityCheck(false);
  287. PhysScene.TaintedObject(LocalID, "BSPrim.setPosition", delegate()
  288. {
  289. DetailLog("{0},BSPrim.SetPosition,taint,pos={1},orient={2}", LocalID, RawPosition, RawOrientation);
  290. ForcePosition = RawPosition;
  291. });
  292. }
  293. }
  294. // NOTE: overloaded by BSPrimDisplaced to handle offset for center-of-gravity.
  295. public override OMV.Vector3 ForcePosition {
  296. get {
  297. RawPosition = PhysScene.PE.GetPosition(PhysBody);
  298. return RawPosition;
  299. }
  300. set {
  301. RawPosition = value;
  302. if (PhysBody.HasPhysicalBody)
  303. {
  304. PhysScene.PE.SetTranslation(PhysBody, RawPosition, RawOrientation);
  305. ActivateIfPhysical(false);
  306. }
  307. }
  308. }
  309. // Check that the current position is sane and, if not, modify the position to make it so.
  310. // Check for being below terrain and being out of bounds.
  311. // Returns 'true' of the position was made sane by some action.
  312. private bool PositionSanityCheck(bool inTaintTime)
  313. {
  314. bool ret = false;
  315. // We don't care where non-physical items are placed
  316. if (!IsPhysicallyActive)
  317. return ret;
  318. if (!PhysScene.TerrainManager.IsWithinKnownTerrain(RawPosition))
  319. {
  320. // The physical object is out of the known/simulated area.
  321. // Upper levels of code will handle the transition to other areas so, for
  322. // the time, we just ignore the position.
  323. return ret;
  324. }
  325. float terrainHeight = PhysScene.TerrainManager.GetTerrainHeightAtXYZ(RawPosition);
  326. OMV.Vector3 upForce = OMV.Vector3.Zero;
  327. float approxSize = Math.Max(Size.X, Math.Max(Size.Y, Size.Z));
  328. if ((RawPosition.Z + approxSize / 2f) < terrainHeight)
  329. {
  330. DetailLog("{0},BSPrim.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, RawPosition, terrainHeight);
  331. float targetHeight = terrainHeight + (Size.Z / 2f);
  332. // If the object is below ground it just has to be moved up because pushing will
  333. // not get it through the terrain
  334. RawPosition = new OMV.Vector3(RawPosition.X, RawPosition.Y, targetHeight);
  335. if (inTaintTime)
  336. {
  337. ForcePosition = RawPosition;
  338. }
  339. // If we are throwing the object around, zero its other forces
  340. ZeroMotion(inTaintTime);
  341. ret = true;
  342. }
  343. if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0)
  344. {
  345. float waterHeight = PhysScene.TerrainManager.GetWaterLevelAtXYZ(RawPosition);
  346. // TODO: a floating motor so object will bob in the water
  347. if (Math.Abs(RawPosition.Z - waterHeight) > 0.1f)
  348. {
  349. // Upforce proportional to the distance away from the water. Correct the error in 1 sec.
  350. upForce.Z = (waterHeight - RawPosition.Z) * 1f;
  351. // Apply upforce and overcome gravity.
  352. OMV.Vector3 correctionForce = upForce - PhysScene.DefaultGravity;
  353. DetailLog("{0},BSPrim.PositionSanityCheck,applyForce,pos={1},upForce={2},correctionForce={3}", LocalID, RawPosition, upForce, correctionForce);
  354. AddForce(inTaintTime, correctionForce);
  355. ret = true;
  356. }
  357. }
  358. return ret;
  359. }
  360. // Occasionally things will fly off and really get lost.
  361. // Find the wanderers and bring them back.
  362. // Return 'true' if some parameter need some sanity.
  363. private bool ExtremeSanityCheck(bool inTaintTime)
  364. {
  365. bool ret = false;
  366. int wayOverThere = -1000;
  367. int wayOutThere = 10000;
  368. // There have been instances of objects getting thrown way out of bounds and crashing
  369. // the border crossing code.
  370. if ( RawPosition.X < wayOverThere || RawPosition.X > wayOutThere
  371. || RawPosition.Y < wayOverThere || RawPosition.X > wayOutThere
  372. || RawPosition.Z < wayOverThere || RawPosition.X > wayOutThere)
  373. {
  374. RawPosition = new OMV.Vector3(10, 10, 50);
  375. ZeroMotion(inTaintTime);
  376. ret = true;
  377. }
  378. if (RawVelocity.LengthSquared() > BSParam.MaxLinearVelocitySquared)
  379. {
  380. RawVelocity = Util.ClampV(RawVelocity, BSParam.MaxLinearVelocity);
  381. ret = true;
  382. }
  383. if (RawRotationalVelocity.LengthSquared() > BSParam.MaxAngularVelocitySquared)
  384. {
  385. RawRotationalVelocity = Util.ClampV(RawRotationalVelocity, BSParam.MaxAngularVelocity);
  386. ret = true;
  387. }
  388. return ret;
  389. }
  390. // Return the effective mass of the object.
  391. // The definition of this call is to return the mass of the prim.
  392. // If the simulator cares about the mass of the linkset, it will sum it itself.
  393. public override float Mass
  394. {
  395. get { return _mass; }
  396. }
  397. // TotalMass returns the mass of the large object the prim may be in (overridden by linkset code)
  398. public virtual float TotalMass
  399. {
  400. get { return _mass; }
  401. }
  402. // used when we only want this prim's mass and not the linkset thing
  403. public override float RawMass {
  404. get { return _mass; }
  405. }
  406. // Set the physical mass to the passed mass.
  407. // Note that this does not change _mass!
  408. public override void UpdatePhysicalMassProperties(float physMass, bool inWorld)
  409. {
  410. if (PhysBody.HasPhysicalBody && PhysShape.HasPhysicalShape)
  411. {
  412. if (IsStatic)
  413. {
  414. PhysScene.PE.SetGravity(PhysBody, PhysScene.DefaultGravity);
  415. Inertia = OMV.Vector3.Zero;
  416. PhysScene.PE.SetMassProps(PhysBody, 0f, Inertia);
  417. PhysScene.PE.UpdateInertiaTensor(PhysBody);
  418. }
  419. else
  420. {
  421. if (inWorld)
  422. {
  423. // Changing interesting properties doesn't change proxy and collision cache
  424. // information. The Bullet solution is to re-add the object to the world
  425. // after parameters are changed.
  426. PhysScene.PE.RemoveObjectFromWorld(PhysScene.World, PhysBody);
  427. }
  428. // The computation of mass props requires gravity to be set on the object.
  429. Gravity = ComputeGravity(Buoyancy);
  430. PhysScene.PE.SetGravity(PhysBody, Gravity);
  431. // OMV.Vector3 currentScale = PhysScene.PE.GetLocalScaling(PhysShape.physShapeInfo); // DEBUG DEBUG
  432. // DetailLog("{0},BSPrim.UpdateMassProperties,currentScale{1},shape={2}", LocalID, currentScale, PhysShape.physShapeInfo); // DEBUG DEBUG
  433. Inertia = PhysScene.PE.CalculateLocalInertia(PhysShape.physShapeInfo, physMass);
  434. PhysScene.PE.SetMassProps(PhysBody, physMass, Inertia);
  435. PhysScene.PE.UpdateInertiaTensor(PhysBody);
  436. DetailLog("{0},BSPrim.UpdateMassProperties,mass={1},localInertia={2},grav={3},inWorld={4}",
  437. LocalID, physMass, Inertia, Gravity, inWorld);
  438. if (inWorld)
  439. {
  440. AddObjectToPhysicalWorld();
  441. }
  442. }
  443. }
  444. }
  445. // Return what gravity should be set to this very moment
  446. public OMV.Vector3 ComputeGravity(float buoyancy)
  447. {
  448. OMV.Vector3 ret = PhysScene.DefaultGravity;
  449. if (!IsStatic)
  450. {
  451. ret *= (1f - buoyancy);
  452. ret *= GravModifier;
  453. }
  454. return ret;
  455. }
  456. // Is this used?
  457. public override OMV.Vector3 CenterOfMass
  458. {
  459. get { return RawPosition; }
  460. }
  461. // Is this used?
  462. public override OMV.Vector3 GeometricCenter
  463. {
  464. get { return RawPosition; }
  465. }
  466. public override OMV.Vector3 Force {
  467. get { return RawForce; }
  468. set {
  469. RawForce = value;
  470. EnableActor(RawForce != OMV.Vector3.Zero, SetForceActorName, delegate()
  471. {
  472. return new BSActorSetForce(PhysScene, this, SetForceActorName);
  473. });
  474. // Call update so actor Refresh() is called to start things off
  475. PhysScene.TaintedObject(LocalID, "BSPrim.setForce", delegate()
  476. {
  477. UpdatePhysicalParameters();
  478. });
  479. }
  480. }
  481. // Find and return a handle to the current vehicle actor.
  482. // Return 'null' if there is no vehicle actor.
  483. public BSDynamics GetVehicleActor(bool createIfNone)
  484. {
  485. BSDynamics ret = null;
  486. BSActor actor;
  487. if (PhysicalActors.TryGetActor(VehicleActorName, out actor))
  488. {
  489. ret = actor as BSDynamics;
  490. }
  491. else
  492. {
  493. if (createIfNone)
  494. {
  495. ret = new BSDynamics(PhysScene, this, VehicleActorName);
  496. PhysicalActors.Add(ret.ActorName, ret);
  497. }
  498. }
  499. return ret;
  500. }
  501. public override int VehicleType {
  502. get {
  503. int ret = (int)Vehicle.TYPE_NONE;
  504. BSDynamics vehicleActor = GetVehicleActor(false /* createIfNone */);
  505. if (vehicleActor != null)
  506. ret = (int)vehicleActor.Type;
  507. return ret;
  508. }
  509. set {
  510. Vehicle type = (Vehicle)value;
  511. PhysScene.TaintedObject(LocalID, "setVehicleType", delegate()
  512. {
  513. // Some vehicle scripts change vehicle type on the fly as an easy way to
  514. // change all the parameters. Like a plane changing to CAR when on the
  515. // ground. In this case, don't want to zero motion.
  516. // ZeroMotion(true /* inTaintTime */);
  517. if (type == Vehicle.TYPE_NONE)
  518. {
  519. // Vehicle type is 'none' so get rid of any actor that may have been allocated.
  520. BSDynamics vehicleActor = GetVehicleActor(false /* createIfNone */);
  521. if (vehicleActor != null)
  522. {
  523. PhysicalActors.RemoveAndRelease(vehicleActor.ActorName);
  524. }
  525. }
  526. else
  527. {
  528. // Vehicle type is not 'none' so create an actor and set it running.
  529. BSDynamics vehicleActor = GetVehicleActor(true /* createIfNone */);
  530. if (vehicleActor != null)
  531. {
  532. vehicleActor.ProcessTypeChange(type);
  533. ActivateIfPhysical(false);
  534. }
  535. }
  536. });
  537. }
  538. }
  539. public override void VehicleFloatParam(int param, float value)
  540. {
  541. PhysScene.TaintedObject(LocalID, "BSPrim.VehicleFloatParam", delegate()
  542. {
  543. BSDynamics vehicleActor = GetVehicleActor(true /* createIfNone */);
  544. if (vehicleActor != null)
  545. {
  546. vehicleActor.ProcessFloatVehicleParam((Vehicle)param, value);
  547. ActivateIfPhysical(false);
  548. }
  549. });
  550. }
  551. public override void VehicleVectorParam(int param, OMV.Vector3 value)
  552. {
  553. PhysScene.TaintedObject(LocalID, "BSPrim.VehicleVectorParam", delegate()
  554. {
  555. BSDynamics vehicleActor = GetVehicleActor(true /* createIfNone */);
  556. if (vehicleActor != null)
  557. {
  558. vehicleActor.ProcessVectorVehicleParam((Vehicle)param, value);
  559. ActivateIfPhysical(false);
  560. }
  561. });
  562. }
  563. public override void VehicleRotationParam(int param, OMV.Quaternion rotation)
  564. {
  565. PhysScene.TaintedObject(LocalID, "BSPrim.VehicleRotationParam", delegate()
  566. {
  567. BSDynamics vehicleActor = GetVehicleActor(true /* createIfNone */);
  568. if (vehicleActor != null)
  569. {
  570. vehicleActor.ProcessRotationVehicleParam((Vehicle)param, rotation);
  571. ActivateIfPhysical(false);
  572. }
  573. });
  574. }
  575. public override void VehicleFlags(int param, bool remove)
  576. {
  577. PhysScene.TaintedObject(LocalID, "BSPrim.VehicleFlags", delegate()
  578. {
  579. BSDynamics vehicleActor = GetVehicleActor(true /* createIfNone */);
  580. if (vehicleActor != null)
  581. {
  582. vehicleActor.ProcessVehicleFlags(param, remove);
  583. }
  584. });
  585. }
  586. public override void SetVehicle(object pvdata)
  587. {
  588. PhysScene.TaintedObject(LocalID, "BSPrim.SetVehicle", delegate ()
  589. {
  590. BSDynamics vehicleActor = GetVehicleActor(true /* createIfNone */);
  591. if (vehicleActor != null && (pvdata is VehicleData) )
  592. {
  593. VehicleData vdata = (VehicleData)pvdata;
  594. // vehicleActor.ProcessSetVehicle((VehicleData)vdata);
  595. vehicleActor.ProcessTypeChange(vdata.m_type);
  596. vehicleActor.ProcessVehicleFlags(-1, false);
  597. vehicleActor.ProcessVehicleFlags((int)vdata.m_flags, false);
  598. // Linear properties
  599. vehicleActor.ProcessVectorVehicleParam(Vehicle.LINEAR_MOTOR_DIRECTION, vdata.m_linearMotorDirection);
  600. vehicleActor.ProcessVectorVehicleParam(Vehicle.LINEAR_FRICTION_TIMESCALE, vdata.m_linearFrictionTimescale);
  601. vehicleActor.ProcessFloatVehicleParam(Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE, vdata.m_linearMotorDecayTimescale);
  602. vehicleActor.ProcessFloatVehicleParam(Vehicle.LINEAR_MOTOR_TIMESCALE, vdata.m_linearMotorTimescale);
  603. vehicleActor.ProcessVectorVehicleParam(Vehicle.LINEAR_MOTOR_OFFSET, vdata.m_linearMotorOffset);
  604. //Angular properties
  605. vehicleActor.ProcessVectorVehicleParam(Vehicle.ANGULAR_MOTOR_DIRECTION, vdata.m_angularMotorDirection);
  606. vehicleActor.ProcessFloatVehicleParam(Vehicle.ANGULAR_MOTOR_TIMESCALE, vdata.m_angularMotorTimescale);
  607. vehicleActor.ProcessFloatVehicleParam(Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE, vdata.m_angularMotorDecayTimescale);
  608. vehicleActor.ProcessVectorVehicleParam(Vehicle.ANGULAR_FRICTION_TIMESCALE, vdata.m_angularFrictionTimescale);
  609. //Deflection properties
  610. vehicleActor.ProcessFloatVehicleParam(Vehicle.ANGULAR_DEFLECTION_EFFICIENCY, vdata.m_angularDeflectionEfficiency);
  611. vehicleActor.ProcessFloatVehicleParam(Vehicle.ANGULAR_DEFLECTION_TIMESCALE, vdata.m_angularDeflectionTimescale);
  612. vehicleActor.ProcessFloatVehicleParam(Vehicle.LINEAR_DEFLECTION_EFFICIENCY, vdata.m_linearDeflectionEfficiency);
  613. vehicleActor.ProcessFloatVehicleParam(Vehicle.LINEAR_DEFLECTION_TIMESCALE, vdata.m_linearDeflectionTimescale);
  614. //Banking properties
  615. vehicleActor.ProcessFloatVehicleParam(Vehicle.BANKING_EFFICIENCY, vdata.m_bankingEfficiency);
  616. vehicleActor.ProcessFloatVehicleParam(Vehicle.BANKING_MIX, vdata.m_bankingMix);
  617. vehicleActor.ProcessFloatVehicleParam(Vehicle.BANKING_TIMESCALE, vdata.m_bankingTimescale);
  618. //Hover and Buoyancy properties
  619. vehicleActor.ProcessFloatVehicleParam(Vehicle.HOVER_HEIGHT, vdata.m_VhoverHeight);
  620. vehicleActor.ProcessFloatVehicleParam(Vehicle.HOVER_EFFICIENCY, vdata.m_VhoverEfficiency);
  621. vehicleActor.ProcessFloatVehicleParam(Vehicle.HOVER_TIMESCALE, vdata.m_VhoverTimescale);
  622. vehicleActor.ProcessFloatVehicleParam(Vehicle.BUOYANCY, vdata.m_VehicleBuoyancy);
  623. //Attractor properties
  624. vehicleActor.ProcessFloatVehicleParam(Vehicle.VERTICAL_ATTRACTION_EFFICIENCY, vdata.m_verticalAttractionEfficiency);
  625. vehicleActor.ProcessFloatVehicleParam(Vehicle.VERTICAL_ATTRACTION_TIMESCALE, vdata.m_verticalAttractionTimescale);
  626. vehicleActor.ProcessRotationVehicleParam(Vehicle.REFERENCE_FRAME, vdata.m_referenceFrame);
  627. }
  628. });
  629. }
  630. // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more
  631. public override void SetVolumeDetect(int param) {
  632. bool newValue = (param != 0);
  633. if (_isVolumeDetect != newValue)
  634. {
  635. _isVolumeDetect = newValue;
  636. PhysScene.TaintedObject(LocalID, "BSPrim.SetVolumeDetect", delegate()
  637. {
  638. // DetailLog("{0},setVolumeDetect,taint,volDetect={1}", LocalID, _isVolumeDetect);
  639. SetObjectDynamic(true);
  640. });
  641. }
  642. return;
  643. }
  644. public override bool IsVolumeDetect
  645. {
  646. get { return _isVolumeDetect; }
  647. }
  648. public override void SetMaterial(int material)
  649. {
  650. base.SetMaterial(material);
  651. PhysScene.TaintedObject(LocalID, "BSPrim.SetMaterial", delegate()
  652. {
  653. UpdatePhysicalParameters();
  654. });
  655. }
  656. public override float Friction
  657. {
  658. get { return base.Friction; }
  659. set
  660. {
  661. if (base.Friction != value)
  662. {
  663. base.Friction = value;
  664. PhysScene.TaintedObject(LocalID, "BSPrim.setFriction", delegate()
  665. {
  666. UpdatePhysicalParameters();
  667. });
  668. }
  669. }
  670. }
  671. public override float Restitution
  672. {
  673. get { return base.Restitution; }
  674. set
  675. {
  676. if (base.Restitution != value)
  677. {
  678. base.Restitution = value;
  679. PhysScene.TaintedObject(LocalID, "BSPrim.setRestitution", delegate()
  680. {
  681. UpdatePhysicalParameters();
  682. });
  683. }
  684. }
  685. }
  686. // The simulator/viewer keep density as 100kg/m3.
  687. // Remember to use BSParam.DensityScaleFactor to create the physical density.
  688. public override float Density
  689. {
  690. get { return base.Density; }
  691. set
  692. {
  693. if (base.Density != value)
  694. {
  695. base.Density = value;
  696. PhysScene.TaintedObject(LocalID, "BSPrim.setDensity", delegate()
  697. {
  698. UpdatePhysicalParameters();
  699. });
  700. }
  701. }
  702. }
  703. public override float GravModifier
  704. {
  705. get { return base.GravModifier; }
  706. set
  707. {
  708. if (base.GravModifier != value)
  709. {
  710. base.GravModifier = value;
  711. PhysScene.TaintedObject(LocalID, "BSPrim.setGravityModifier", delegate()
  712. {
  713. UpdatePhysicalParameters();
  714. });
  715. }
  716. }
  717. }
  718. public override OMV.Vector3 ForceVelocity {
  719. get { return RawVelocity; }
  720. set {
  721. RawVelocity = Util.ClampV(value, BSParam.MaxLinearVelocity);
  722. if (PhysBody.HasPhysicalBody)
  723. {
  724. DetailLog("{0},BSPrim.ForceVelocity,taint,vel={1}", LocalID, RawVelocity);
  725. PhysScene.PE.SetLinearVelocity(PhysBody, RawVelocity);
  726. ActivateIfPhysical(false);
  727. }
  728. }
  729. }
  730. public override OMV.Vector3 Torque {
  731. get { return RawTorque; }
  732. set {
  733. RawTorque = value;
  734. EnableActor(RawTorque != OMV.Vector3.Zero, SetTorqueActorName, delegate()
  735. {
  736. return new BSActorSetTorque(PhysScene, this, SetTorqueActorName);
  737. });
  738. DetailLog("{0},BSPrim.SetTorque,call,torque={1}", LocalID, RawTorque);
  739. // Call update so actor Refresh() is called to start things off
  740. PhysScene.TaintedObject(LocalID, "BSPrim.setTorque", delegate()
  741. {
  742. UpdatePhysicalParameters();
  743. });
  744. }
  745. }
  746. public override OMV.Vector3 Acceleration {
  747. get { return _acceleration; }
  748. set { _acceleration = value; }
  749. }
  750. public override OMV.Quaternion Orientation {
  751. get {
  752. return RawOrientation;
  753. }
  754. set {
  755. if (RawOrientation == value)
  756. return;
  757. RawOrientation = value;
  758. PhysScene.TaintedObject(LocalID, "BSPrim.setOrientation", delegate()
  759. {
  760. ForceOrientation = RawOrientation;
  761. });
  762. }
  763. }
  764. // Go directly to Bullet to get/set the value.
  765. public override OMV.Quaternion ForceOrientation
  766. {
  767. get
  768. {
  769. RawOrientation = PhysScene.PE.GetOrientation(PhysBody);
  770. return RawOrientation;
  771. }
  772. set
  773. {
  774. RawOrientation = value;
  775. if (PhysBody.HasPhysicalBody)
  776. PhysScene.PE.SetTranslation(PhysBody, RawPosition, RawOrientation);
  777. }
  778. }
  779. public override int PhysicsActorType {
  780. get { return _physicsActorType; }
  781. set { _physicsActorType = value; }
  782. }
  783. public override bool IsPhysical {
  784. get { return _isPhysical; }
  785. set {
  786. if (_isPhysical != value)
  787. {
  788. _isPhysical = value;
  789. PhysScene.TaintedObject(LocalID, "BSPrim.setIsPhysical", delegate()
  790. {
  791. DetailLog("{0},setIsPhysical,taint,isPhys={1}", LocalID, _isPhysical);
  792. SetObjectDynamic(true);
  793. // whether phys-to-static or static-to-phys, the object is not moving.
  794. ZeroMotion(true);
  795. });
  796. }
  797. }
  798. }
  799. // An object is static (does not move) if selected or not physical
  800. public override bool IsStatic
  801. {
  802. get { return _isSelected || !IsPhysical; }
  803. }
  804. // An object is solid if it's not phantom and if it's not doing VolumeDetect
  805. public override bool IsSolid
  806. {
  807. get { return !IsPhantom && !_isVolumeDetect; }
  808. }
  809. // The object is moving and is actively being dynamic in the physical world
  810. public override bool IsPhysicallyActive
  811. {
  812. get { return !_isSelected && IsPhysical; }
  813. }
  814. // Make gravity work if the object is physical and not selected
  815. // Called at taint-time!!
  816. private void SetObjectDynamic(bool forceRebuild)
  817. {
  818. // Recreate the physical object if necessary
  819. CreateGeomAndObject(forceRebuild);
  820. }
  821. // Convert the simulator's physical properties into settings on BulletSim objects.
  822. // There are four flags we're interested in:
  823. // IsStatic: Object does not move, otherwise the object has mass and moves
  824. // isSolid: other objects bounce off of this object
  825. // isVolumeDetect: other objects pass through but can generate collisions
  826. // collisionEvents: whether this object returns collision events
  827. // NOTE: overloaded by BSPrimLinkable to also update linkset physical parameters.
  828. public virtual void UpdatePhysicalParameters()
  829. {
  830. if (!PhysBody.HasPhysicalBody)
  831. {
  832. // This would only happen if updates are called for during initialization when the body is not set up yet.
  833. // DetailLog("{0},BSPrim.UpdatePhysicalParameters,taint,calledWithNoPhysBody", LocalID);
  834. return;
  835. }
  836. // Mangling all the physical properties requires the object not be in the physical world.
  837. // This is a NOOP if the object is not in the world (BulletSim and Bullet ignore objects not found).
  838. PhysScene.PE.RemoveObjectFromWorld(PhysScene.World, PhysBody);
  839. // Set up the object physicalness (does gravity and collisions move this object)
  840. MakeDynamic(IsStatic);
  841. // Update vehicle specific parameters (after MakeDynamic() so can change physical parameters)
  842. PhysicalActors.Refresh();
  843. // Arrange for collision events if the simulator wants them
  844. EnableCollisions(SubscribedEvents());
  845. // Make solid or not (do things bounce off or pass through this object).
  846. MakeSolid(IsSolid);
  847. AddObjectToPhysicalWorld();
  848. // Rebuild its shape
  849. PhysScene.PE.UpdateSingleAabb(PhysScene.World, PhysBody);
  850. DetailLog("{0},BSPrim.UpdatePhysicalParameters,taintExit,static={1},solid={2},mass={3},collide={4},cf={5:X},cType={6},body={7},shape={8}",
  851. LocalID, IsStatic, IsSolid, Mass, SubscribedEvents(),
  852. CurrentCollisionFlags, PhysBody.collisionType, PhysBody, PhysShape);
  853. }
  854. // "Making dynamic" means changing to and from static.
  855. // When static, gravity does not effect the object and it is fixed in space.
  856. // When dynamic, the object can fall and be pushed by others.
  857. // This is independent of its 'solidness' which controls what passes through
  858. // this object and what interacts with it.
  859. protected virtual void MakeDynamic(bool makeStatic)
  860. {
  861. if (makeStatic)
  862. {
  863. // Become a Bullet 'static' object type
  864. CurrentCollisionFlags = PhysScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.CF_STATIC_OBJECT);
  865. // Stop all movement
  866. ZeroMotion(true);
  867. // Set various physical properties so other object interact properly
  868. PhysScene.PE.SetFriction(PhysBody, Friction);
  869. PhysScene.PE.SetRestitution(PhysBody, Restitution);
  870. PhysScene.PE.SetContactProcessingThreshold(PhysBody, BSParam.ContactProcessingThreshold);
  871. // Mass is zero which disables a bunch of physics stuff in Bullet
  872. UpdatePhysicalMassProperties(0f, false);
  873. // Set collision detection parameters
  874. if (BSParam.CcdMotionThreshold > 0f)
  875. {
  876. PhysScene.PE.SetCcdMotionThreshold(PhysBody, BSParam.CcdMotionThreshold);
  877. PhysScene.PE.SetCcdSweptSphereRadius(PhysBody, BSParam.CcdSweptSphereRadius);
  878. }
  879. // The activation state is 'disabled' so Bullet will not try to act on it.
  880. // PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.DISABLE_SIMULATION);
  881. // Start it out sleeping and physical actions could wake it up.
  882. PhysScene.PE.ForceActivationState(PhysBody, ActivationState.ISLAND_SLEEPING);
  883. // This collides like a static object
  884. PhysBody.collisionType = CollisionType.Static;
  885. }
  886. else
  887. {
  888. // Not a Bullet static object
  889. CurrentCollisionFlags = PhysScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.CF_STATIC_OBJECT);
  890. // Set various physical properties so other object interact properly
  891. PhysScene.PE.SetFriction(PhysBody, Friction);
  892. PhysScene.PE.SetRestitution(PhysBody, Restitution);
  893. // DetailLog("{0},BSPrim.MakeDynamic,frict={1},rest={2}", LocalID, Friction, Restitution);
  894. // per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382
  895. // Since this can be called multiple times, only zero forces when becoming physical
  896. // PhysicsScene.PE.ClearAllForces(BSBody);
  897. // For good measure, make sure the transform is set through to the motion state
  898. ForcePosition = RawPosition;
  899. ForceVelocity = RawVelocity;
  900. ForceRotationalVelocity = RawRotationalVelocity;
  901. // A dynamic object has mass
  902. UpdatePhysicalMassProperties(RawMass, false);
  903. // Set collision detection parameters
  904. if (BSParam.CcdMotionThreshold > 0f)
  905. {
  906. PhysScene.PE.SetCcdMotionThreshold(PhysBody, BSParam.CcdMotionThreshold);
  907. PhysScene.PE.SetCcdSweptSphereRadius(PhysBody, BSParam.CcdSweptSphereRadius);
  908. }
  909. // Various values for simulation limits
  910. PhysScene.PE.SetDamping(PhysBody, BSParam.LinearDamping, BSParam.AngularDamping);
  911. // Whether to allow deactivation of this body when it is at rest.
  912. if (this.DisableDeactivation)
  913. {
  914. // The user has asked that the physical object never be deactivated.
  915. PhysScene.PE.ForceActivationState(PhysBody, ActivationState.DISABLE_DEACTIVATION);
  916. // DetailLog("{0},BSPrim.MakeDynamic,set ActivationState=DISABLE_DEACTIVATION", LocalID);
  917. }
  918. else
  919. {
  920. // Force activation of the object so Bullet will act on it.
  921. // Must do the ForceActivationState() to overcome the DISABLE_SIMULATION from static objects.
  922. PhysScene.PE.ForceActivationState(PhysBody, ActivationState.ACTIVE_TAG);
  923. // DetailLog("{0},BSPrim.MakeDynamic,set ActivationState=ACTIVE_TAG", LocalID);
  924. }
  925. PhysScene.PE.SetSleepingThresholds(PhysBody, BSParam.LinearSleepingThreshold, BSParam.AngularSleepingThreshold);
  926. PhysScene.PE.SetContactProcessingThreshold(PhysBody, BSParam.ContactProcessingThreshold);
  927. // This collides like an object.
  928. PhysBody.collisionType = CollisionType.Dynamic;
  929. }
  930. }
  931. // "Making solid" means that other object will not pass through this object.
  932. // To make transparent, we create a Bullet ghost object.
  933. // Note: This expects to be called from the UpdatePhysicalParameters() routine as
  934. // the functions after this one set up the state of a possibly newly created collision body.
  935. private void MakeSolid(bool makeSolid)
  936. {
  937. CollisionObjectTypes bodyType = (CollisionObjectTypes)PhysScene.PE.GetBodyType(PhysBody);
  938. if (makeSolid)
  939. {
  940. // Verify the previous code created the correct shape for this type of thing.
  941. if ((bodyType & CollisionObjectTypes.CO_RIGID_BODY) == 0)
  942. {
  943. m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for solidity. id={1}, type={2}", LogHeader, LocalID, bodyType);
  944. }
  945. CurrentCollisionFlags = PhysScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE);
  946. }
  947. else
  948. {
  949. if ((bodyType & CollisionObjectTypes.CO_GHOST_OBJECT) == 0)
  950. {
  951. m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for non-solidness. id={1}, type={2}", LogHeader, LocalID, bodyType);
  952. }
  953. CurrentCollisionFlags = PhysScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE);
  954. // Change collision info from a static object to a ghosty collision object
  955. PhysBody.collisionType = CollisionType.VolumeDetect;
  956. }
  957. }
  958. // Turn on or off the flag controlling whether collision events are returned to the simulator.
  959. private void EnableCollisions(bool wantsCollisionEvents)
  960. {
  961. if (wantsCollisionEvents)
  962. {
  963. CurrentCollisionFlags = PhysScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
  964. }
  965. else
  966. {
  967. CurrentCollisionFlags = PhysScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
  968. }
  969. }
  970. // Add me to the physical world.
  971. // Object MUST NOT already be in the world.
  972. // This routine exists because some assorted properties get mangled by adding to the world.
  973. internal void AddObjectToPhysicalWorld()
  974. {
  975. if (PhysBody.HasPhysicalBody)
  976. {
  977. PhysScene.PE.AddObjectToWorld(PhysScene.World, PhysBody);
  978. }
  979. else
  980. {
  981. m_log.ErrorFormat("{0} Attempt to add physical object without body. id={1}", LogHeader, LocalID);
  982. DetailLog("{0},BSPrim.AddObjectToPhysicalWorld,addObjectWithoutBody,cType={1}", LocalID, PhysBody.collisionType);
  983. }
  984. }
  985. // prims don't fly
  986. public override bool Flying {
  987. get { return _flying; }
  988. set {
  989. _flying = value;
  990. }
  991. }
  992. public override bool SetAlwaysRun {
  993. get { return _setAlwaysRun; }
  994. set { _setAlwaysRun = value; }
  995. }
  996. public override bool ThrottleUpdates {
  997. get { return _throttleUpdates; }
  998. set { _throttleUpdates = value; }
  999. }
  1000. public bool IsPhantom {
  1001. get {
  1002. // SceneObjectPart removes phantom objects from the physics scene
  1003. // so, although we could implement touching and such, we never
  1004. // are invoked as a phantom object
  1005. return false;
  1006. }
  1007. }
  1008. public override bool FloatOnWater {
  1009. set {
  1010. _floatOnWater = value;
  1011. PhysScene.TaintedObject(LocalID, "BSPrim.setFloatOnWater", delegate()
  1012. {
  1013. if (_floatOnWater)
  1014. CurrentCollisionFlags = PhysScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
  1015. else
  1016. CurrentCollisionFlags = PhysScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
  1017. });
  1018. }
  1019. }
  1020. public override bool Kinematic {
  1021. get { return _kinematic; }
  1022. set { _kinematic = value;
  1023. // m_log.DebugFormat("{0}: Kinematic={1}", LogHeader, _kinematic);
  1024. }
  1025. }
  1026. public override float Buoyancy {
  1027. get { return _buoyancy; }
  1028. set {
  1029. _buoyancy = value;
  1030. PhysScene.TaintedObject(LocalID, "BSPrim.setBuoyancy", delegate()
  1031. {
  1032. ForceBuoyancy = _buoyancy;
  1033. });
  1034. }
  1035. }
  1036. public override float ForceBuoyancy {
  1037. get { return _buoyancy; }
  1038. set {
  1039. _buoyancy = value;
  1040. // DetailLog("{0},BSPrim.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
  1041. // Force the recalculation of the various inertia,etc variables in the object
  1042. UpdatePhysicalMassProperties(RawMass, true);
  1043. DetailLog("{0},BSPrim.ForceBuoyancy,buoy={1},mass={2},grav={3}", LocalID, _buoyancy, RawMass, Gravity);
  1044. ActivateIfPhysical(false);
  1045. }
  1046. }
  1047. public override bool PIDActive
  1048. {
  1049. get
  1050. {
  1051. return MoveToTargetActive;
  1052. }
  1053. set
  1054. {
  1055. MoveToTargetActive = value;
  1056. EnableActor(MoveToTargetActive, MoveToTargetActorName, delegate()
  1057. {
  1058. return new BSActorMoveToTarget(PhysScene, this, MoveToTargetActorName);
  1059. });
  1060. // Call update so actor Refresh() is called to start things off
  1061. PhysScene.TaintedObject(LocalID, "BSPrim.PIDActive", delegate()
  1062. {
  1063. UpdatePhysicalParameters();
  1064. });
  1065. }
  1066. }
  1067. public override OMV.Vector3 PIDTarget
  1068. {
  1069. set
  1070. {
  1071. base.PIDTarget = value;
  1072. BSActor actor;
  1073. if (PhysicalActors.TryGetActor(MoveToTargetActorName, out actor))
  1074. {
  1075. // if the actor exists, tell it to refresh its values.
  1076. actor.Refresh();
  1077. }
  1078. }
  1079. }
  1080. // Used for llSetHoverHeight and maybe vehicle height
  1081. // Hover Height will override MoveTo target's Z
  1082. public override bool PIDHoverActive {
  1083. get
  1084. {
  1085. return base.HoverActive;
  1086. }
  1087. set {
  1088. base.HoverActive = value;
  1089. EnableActor(HoverActive, HoverActorName, delegate()
  1090. {
  1091. return new BSActorHover(PhysScene, this, HoverActorName);
  1092. });
  1093. // Call update so actor Refresh() is called to start things off
  1094. PhysScene.TaintedObject(LocalID, "BSPrim.PIDHoverActive", delegate()
  1095. {
  1096. UpdatePhysicalParameters();
  1097. });
  1098. }
  1099. }
  1100. public override void AvatarJump(float forceZ) { }
  1101. public override void AddForce(OMV.Vector3 force, bool pushforce) {
  1102. // Per documentation, max force is limited.
  1103. OMV.Vector3 addForce = Util.ClampV(force, BSParam.MaxAddForceMagnitude);
  1104. // Push forces seem to be scaled differently (follow pattern in ubODE)
  1105. if (!pushforce) {
  1106. // Since this force is being applied in only one step, make this a force per second.
  1107. addForce /= PhysScene.LastTimeStep;
  1108. }
  1109. AddForce(false /* inTaintTime */, addForce);
  1110. }
  1111. // Applying a force just adds this to the total force on the object.
  1112. // This added force will only last the next simulation tick.
  1113. public override void AddForce(bool inTaintTime, OMV.Vector3 force) {
  1114. // for an object, doesn't matter if force is a pushforce or not
  1115. if (IsPhysicallyActive)
  1116. {
  1117. if (force.IsFinite())
  1118. {
  1119. // DetailLog("{0},BSPrim.addForce,call,force={1}", LocalID, addForce);
  1120. OMV.Vector3 addForce = force;
  1121. PhysScene.TaintedObject(inTaintTime, LocalID, "BSPrim.AddForce", delegate()
  1122. {
  1123. // Bullet adds this central force to the total force for this tick.
  1124. // Deep down in Bullet:
  1125. // linearVelocity += totalForce / mass * timeStep;
  1126. DetailLog("{0},BSPrim.addForce,taint,force={1}", LocalID, addForce);
  1127. if (PhysBody.HasPhysicalBody)
  1128. {
  1129. PhysScene.PE.ApplyCentralForce(PhysBody, addForce);
  1130. ActivateIfPhysical(false);
  1131. }
  1132. });
  1133. }
  1134. else
  1135. {
  1136. m_log.WarnFormat("{0}: AddForce: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID);
  1137. return;
  1138. }
  1139. }
  1140. }
  1141. public void AddForceImpulse(OMV.Vector3 impulse, bool pushforce, bool inTaintTime) {
  1142. // for an object, doesn't matter if force is a pushforce or not
  1143. if (!IsPhysicallyActive)
  1144. {
  1145. if (impulse.IsFinite())
  1146. {
  1147. OMV.Vector3 addImpulse = Util.ClampV(impulse, BSParam.MaxAddForceMagnitude);
  1148. // DetailLog("{0},BSPrim.addForceImpulse,call,impulse={1}", LocalID, impulse);
  1149. PhysScene.TaintedObject(inTaintTime, LocalID, "BSPrim.AddImpulse", delegate()
  1150. {
  1151. // Bullet adds this impulse immediately to the velocity
  1152. DetailLog("{0},BSPrim.addForceImpulse,taint,impulseforce={1}", LocalID, addImpulse);
  1153. if (PhysBody.HasPhysicalBody)
  1154. {
  1155. PhysScene.PE.ApplyCentralImpulse(PhysBody, addImpulse);
  1156. ActivateIfPhysical(false);
  1157. }
  1158. });
  1159. }
  1160. else
  1161. {
  1162. m_log.WarnFormat("{0}: AddForceImpulse: Got a NaN impulse applied to a prim. LocalID={1}", LogHeader, LocalID);
  1163. return;
  1164. }
  1165. }
  1166. }
  1167. // BSPhysObject.AddAngularForce()
  1168. public override void AddAngularForce(bool inTaintTime, OMV.Vector3 force)
  1169. {
  1170. if (force.IsFinite())
  1171. {
  1172. OMV.Vector3 angForce = force;
  1173. PhysScene.TaintedObject(inTaintTime, LocalID, "BSPrim.AddAngularForce", delegate()
  1174. {
  1175. if (PhysBody.HasPhysicalBody)
  1176. {
  1177. DetailLog("{0},BSPrim.AddAngularForce,taint,angForce={1}", LocalID, angForce);
  1178. PhysScene.PE.ApplyTorque(PhysBody, angForce);
  1179. ActivateIfPhysical(false);
  1180. }
  1181. });
  1182. }
  1183. else
  1184. {
  1185. m_log.WarnFormat("{0}: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID);
  1186. return;
  1187. }
  1188. }
  1189. // A torque impulse.
  1190. // ApplyTorqueImpulse adds torque directly to the angularVelocity.
  1191. // AddAngularForce accumulates the force and applied it to the angular velocity all at once.
  1192. // Computed as: angularVelocity += impulse * inertia;
  1193. public void ApplyTorqueImpulse(OMV.Vector3 impulse, bool inTaintTime)
  1194. {
  1195. OMV.Vector3 applyImpulse = impulse;
  1196. PhysScene.TaintedObject(inTaintTime, LocalID, "BSPrim.ApplyTorqueImpulse", delegate()
  1197. {
  1198. if (PhysBody.HasPhysicalBody)
  1199. {
  1200. PhysScene.PE.ApplyTorqueImpulse(PhysBody, applyImpulse);
  1201. ActivateIfPhysical(false);
  1202. }
  1203. });
  1204. }
  1205. #region Mass Calculation
  1206. private float CalculateMass()
  1207. {
  1208. float volume = _size.X * _size.Y * _size.Z; // default
  1209. float tmp;
  1210. float returnMass = 0;
  1211. float hollowAmount = (float)BaseShape.ProfileHollow * 2.0e-5f;
  1212. float hollowVolume = hollowAmount * hollowAmount;
  1213. switch (BaseShape.ProfileShape)
  1214. {
  1215. case ProfileShape.Square:
  1216. // default box
  1217. if (BaseShape.PathCurve == (byte)Extrusion.Straight)
  1218. {
  1219. if (hollowAmount > 0.0)
  1220. {
  1221. switch (BaseShape.HollowShape)
  1222. {
  1223. case HollowShape.Square:
  1224. case HollowShape.Same:
  1225. break;
  1226. case HollowShape.Circle:
  1227. hollowVolume *= 0.78539816339f;
  1228. break;
  1229. case HollowShape.Triangle:
  1230. hollowVolume *= (0.5f * .5f);
  1231. break;
  1232. default:
  1233. hollowVolume = 0;
  1234. break;
  1235. }
  1236. volume *= (1.0f - hollowVolume);
  1237. }
  1238. }
  1239. else if (BaseShape.PathCurve == (byte)Extrusion.Curve1)
  1240. {
  1241. //a tube
  1242. volume *= 0.78539816339e-2f * (float)(200 - BaseShape.PathScaleX);
  1243. tmp= 1.0f -2.0e-2f * (float)(200 - BaseShape.PathScaleY);
  1244. volume -= volume*tmp*tmp;
  1245. if (hollowAmount > 0.0)
  1246. {
  1247. hollowVolume *= hollowAmount;
  1248. switch (BaseShape.HollowShape)
  1249. {
  1250. case HollowShape.Square:
  1251. case HollowShape.Same:
  1252. break;
  1253. case HollowShape.Circle:
  1254. hollowVolume *= 0.78539816339f;
  1255. break;
  1256. case HollowShape.Triangle:
  1257. hollowVolume *= 0.5f * 0.5f;
  1258. break;
  1259. default:
  1260. hollowVolume = 0;
  1261. break;
  1262. }
  1263. volume *= (1.0f - hollowVolume);
  1264. }
  1265. }
  1266. break;
  1267. case ProfileShape.Circle:
  1268. if (BaseShape.PathCurve == (byte)Extrusion.Straight)
  1269. {
  1270. volume *= 0.78539816339f; // elipse base
  1271. if (hollowAmount > 0.0)
  1272. {
  1273. switch (BaseShape.HollowShape)
  1274. {
  1275. case HollowShape.Same:
  1276. case HollowShape.Circle:
  1277. break;
  1278. case HollowShape.Square:
  1279. hollowVolume *= 0.5f * 2.5984480504799f;
  1280. break;
  1281. case HollowShape.Triangle:
  1282. hollowVolume *= .5f * 1.27323954473516f;
  1283. break;
  1284. default:
  1285. hollowVolume = 0;
  1286. break;
  1287. }
  1288. volume *= (1.0f - hollowVolume);
  1289. }
  1290. }
  1291. else if (BaseShape.PathCurve == (byte)Extrusion.Curve1)
  1292. {
  1293. volume *= 0.61685027506808491367715568749226e-2f * (float)(200 - BaseShape.PathScaleX);
  1294. tmp = 1.0f - .02f * (float)(200 - BaseShape.PathScaleY);
  1295. volume *= (1.0f - tmp * tmp);
  1296. if (hollowAmount > 0.0)
  1297. {
  1298. // calculate the hollow volume by it's shape compared to the prim shape
  1299. hollowVolume *= hollowAmount;
  1300. switch (BaseShape.HollowShape)
  1301. {
  1302. case HollowShape.Same:
  1303. case HollowShape.Circle:
  1304. break;
  1305. case HollowShape.Square:
  1306. hollowVolume *= 0.5f * 2.5984480504799f;
  1307. break;
  1308. case HollowShape.Triangle:
  1309. hollowVolume *= .5f * 1.27323954473516f;
  1310. break;
  1311. default:
  1312. hollowVolume = 0;
  1313. break;
  1314. }
  1315. volume *= (1.0f - hollowVolume);
  1316. }
  1317. }
  1318. break;
  1319. case ProfileShape.HalfCircle:
  1320. if (BaseShape.PathCurve == (byte)Extrusion.Curve1)
  1321. {
  1322. volume *= 0.52359877559829887307710723054658f;
  1323. }
  1324. break;
  1325. case ProfileShape.EquilateralTriangle:
  1326. if (BaseShape.PathCurve == (byte)Extrusion.Straight)
  1327. {
  1328. volume *= 0.32475953f;
  1329. if (hollowAmount > 0.0)
  1330. {
  1331. // calculate the hollow volume by it's shape compared to the prim shape
  1332. switch (BaseShape.HollowShape)
  1333. {
  1334. case HollowShape.Same:
  1335. case HollowShape.Triangle:
  1336. hollowVolume *= .25f;
  1337. break;
  1338. case HollowShape.Square:
  1339. hollowVolume *= 0.499849f * 3.07920140172638f;
  1340. break;
  1341. case HollowShape.Circle:
  1342. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  1343. // Cyllinder hollow volume calculation
  1344. hollowVolume *= 0.1963495f * 3.07920140172638f;
  1345. break;
  1346. default:
  1347. hollowVolume = 0;
  1348. break;
  1349. }
  1350. volume *= (1.0f - hollowVolume);
  1351. }
  1352. }
  1353. else if (BaseShape.PathCurve == (byte)Extrusion.Curve1)
  1354. {
  1355. volume *= 0.32475953f;
  1356. volume *= 0.01f * (float)(200 - BaseShape.PathScaleX);
  1357. tmp = 1.0f - .02f * (float)(200 - BaseShape.PathScaleY);
  1358. volume *= (1.0f - tmp * tmp);
  1359. if (hollowAmount > 0.0)
  1360. {
  1361. hollowVolume *= hollowAmount;
  1362. switch (BaseShape.HollowShape)
  1363. {
  1364. case HollowShape.Same:
  1365. case HollowShape.Triangle:
  1366. hollowVolume *= .25f;
  1367. break;
  1368. case HollowShape.Square:
  1369. hollowVolume *= 0.499849f * 3.07920140172638f;
  1370. break;
  1371. case HollowShape.Circle:
  1372. hollowVolume *= 0.1963495f * 3.07920140172638f;
  1373. break;
  1374. default:
  1375. hollowVolume = 0;
  1376. break;
  1377. }
  1378. volume *= (1.0f - hollowVolume);
  1379. }
  1380. }
  1381. break;
  1382. default:
  1383. break;
  1384. }
  1385. float taperX1;
  1386. float taperY1;
  1387. float taperX;
  1388. float taperY;
  1389. float pathBegin;
  1390. float pathEnd;
  1391. float profileBegin;
  1392. float profileEnd;
  1393. if (BaseShape.PathCurve == (byte)Extrusion.Straight || BaseShape.PathCurve == (byte)Extrusion.Flexible)
  1394. {
  1395. taperX1 = BaseShape.PathScaleX * 0.01f;
  1396. if (taperX1 > 1.0f)
  1397. taperX1 = 2.0f - taperX1;
  1398. taperX = 1.0f - taperX1;
  1399. taperY1 = BaseShape.PathScaleY * 0.01f;
  1400. if (taperY1 > 1.0f)
  1401. taperY1 = 2.0f - taperY1;
  1402. taperY = 1.0f - taperY1;
  1403. }
  1404. else
  1405. {
  1406. taperX = BaseShape.PathTaperX * 0.01f;
  1407. if (taperX < 0.0f)
  1408. taperX = -taperX;
  1409. taperX1 = 1.0f - taperX;
  1410. taperY = BaseShape.PathTaperY * 0.01f;
  1411. if (taperY < 0.0f)
  1412. taperY = -taperY;
  1413. taperY1 = 1.0f - taperY;
  1414. }
  1415. volume *= (taperX1 * taperY1 + 0.5f * (taperX1 * taperY + taperX * taperY1) + 0.3333333333f * taperX * taperY);
  1416. pathBegin = (float)BaseShape.PathBegin * 2.0e-5f;
  1417. pathEnd = 1.0f - (float)BaseShape.PathEnd * 2.0e-5f;
  1418. volume *= (pathEnd - pathBegin);
  1419. // this is crude aproximation
  1420. profileBegin = (float)BaseShape.ProfileBegin * 2.0e-5f;
  1421. profileEnd = 1.0f - (float)BaseShape.ProfileEnd * 2.0e-5f;
  1422. volume *= (profileEnd - profileBegin);
  1423. returnMass = Density * BSParam.DensityScaleFactor * volume;
  1424. returnMass = Math.Clamp(returnMass, BSParam.MinimumObjectMass, BSParam.MaximumObjectMass);
  1425. // DetailLog("{0},BSPrim.CalculateMass,den={1},vol={2},mass={3}", LocalID, Density, volume, returnMass);
  1426. DetailLog("{0},BSPrim.CalculateMass,den={1},vol={2},mass={3},pathB={4},pathE={5},profB={6},profE={7},siz={8}",
  1427. LocalID, Density, volume, returnMass, pathBegin, pathEnd, profileBegin, profileEnd, _size);
  1428. return returnMass;
  1429. }// end CalculateMass
  1430. #endregion Mass Calculation
  1431. // Rebuild the geometry and object.
  1432. // This is called when the shape changes so we need to recreate the mesh/hull.
  1433. // Called at taint-time!!!
  1434. public void CreateGeomAndObject(bool forceRebuild)
  1435. {
  1436. // Create the correct physical representation for this type of object.
  1437. // Updates base.PhysBody and base.PhysShape with the new information.
  1438. // Ignore 'forceRebuild'. 'GetBodyAndShape' makes the right choices and changes of necessary.
  1439. PhysScene.Shapes.GetBodyAndShape(false /*forceRebuild */, PhysScene.World, this, delegate(BulletBody pBody, BulletShape pShape)
  1440. {
  1441. // Called if the current prim body is about to be destroyed.
  1442. // Remove all the physical dependencies on the old body.
  1443. // (Maybe someday make the changing of BSShape an event to be subscribed to by BSLinkset, ...)
  1444. // Note: this virtual function is overloaded by BSPrimLinkable to remove linkset constraints.
  1445. RemoveDependencies();
  1446. });
  1447. // Make sure the properties are set on the new object
  1448. UpdatePhysicalParameters();
  1449. return;
  1450. }
  1451. // Called at taint-time
  1452. protected virtual void RemoveDependencies()
  1453. {
  1454. PhysicalActors.RemoveDependencies();
  1455. }
  1456. #region Extension
  1457. public override object Extension(string pFunct, params object[] pParams)
  1458. {
  1459. DetailLog("{0} BSPrim.Extension,op={1}", LocalID, pFunct);
  1460. object ret = null;
  1461. switch (pFunct)
  1462. {
  1463. case ExtendedPhysics.PhysFunctAxisLockLimits:
  1464. ret = SetAxisLockLimitsExtension(pParams);
  1465. break;
  1466. case ExtendedPhysics.PhysFunctDisableDeactivation:
  1467. ret = DisableDeactivationExtension(pParams);
  1468. break;
  1469. default:
  1470. ret = base.Extension(pFunct, pParams);
  1471. break;
  1472. }
  1473. return ret;
  1474. }
  1475. private void InitializeAxisActor()
  1476. {
  1477. EnableActor(LockedAngularAxis != LockedAxisFree || LockedLinearAxis != LockedAxisFree,
  1478. LockedAxisActorName, delegate()
  1479. {
  1480. return new BSActorLockAxis(PhysScene, this, LockedAxisActorName);
  1481. });
  1482. // Update parameters so the new actor's Refresh() action is called at the right time.
  1483. PhysScene.TaintedObject(LocalID, "BSPrim.LockAxis", delegate()
  1484. {
  1485. UpdatePhysicalParameters();
  1486. });
  1487. }
  1488. // Passed an array of an array of parameters, set the axis locking.
  1489. // This expects an int (PHYS_AXIS_*) followed by none or two limit floats
  1490. // followed by another int and floats, etc.
  1491. private object SetAxisLockLimitsExtension(object[] pParams)
  1492. {
  1493. DetailLog("{0} SetAxisLockLimitsExtension. parmlen={1}", LocalID, pParams.GetLength(0));
  1494. object ret = null;
  1495. try
  1496. {
  1497. int index = 2;
  1498. while (index < pParams.GetLength(0))
  1499. {
  1500. var funct = PassedParam<int>(pParams, index, 1);
  1501. DetailLog("{0} SetAxisLockLimitsExtension. op={1}, index={2}", LocalID, funct, index);
  1502. switch (funct)
  1503. {
  1504. // Those that take no parameters
  1505. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR:
  1506. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_X:
  1507. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Y:
  1508. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Z:
  1509. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR:
  1510. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_X:
  1511. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Y:
  1512. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Z:
  1513. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR:
  1514. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_X:
  1515. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Y:
  1516. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Z:
  1517. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR:
  1518. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_X:
  1519. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Y:
  1520. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Z:
  1521. case ExtendedPhysics.PHYS_AXIS_UNLOCK:
  1522. ApplyAxisLimits((int)funct, 0f, 0f);
  1523. index += 1;
  1524. break;
  1525. // Those that take two parameters (the limits)
  1526. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_X:
  1527. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Y:
  1528. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Z:
  1529. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_X:
  1530. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Y:
  1531. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Z:
  1532. var low = PassedParam<float>(pParams, index + 1, 0f);
  1533. var high = PassedParam<float>(pParams, index + 2, 0f);
  1534. ApplyAxisLimits((int)funct, low, high);
  1535. index += 3;
  1536. break;
  1537. default:
  1538. m_log.WarnFormat("{0} SetSxisLockLimitsExtension. Unknown op={1}", LogHeader, funct);
  1539. index += 1;
  1540. break;
  1541. }
  1542. }
  1543. InitializeAxisActor();
  1544. ret = (object)index;
  1545. }
  1546. catch (Exception e)
  1547. {
  1548. m_log.WarnFormat("{0} SetSxisLockLimitsExtension exception in object {1}: {2}", LogHeader, this.Name, e);
  1549. ret = null;
  1550. }
  1551. return ret; // not implemented yet
  1552. }
  1553. private object DisableDeactivationExtension(object[] pParams)
  1554. {
  1555. object ret = null;
  1556. try
  1557. {
  1558. var disable = PassedParam<int>(pParams, 2, 0);
  1559. DetailLog("{0} DisableDeactivationExtension. disable={1}", LocalID, disable);
  1560. this.DisableDeactivation = (disable != 0);
  1561. ret = (object)1;
  1562. // Update parameters so the prim is rebuilt with the new settings
  1563. PhysScene.TaintedObject(LocalID, "BSPrim.DisableDeactivationExtension", () => { UpdatePhysicalParameters(); });
  1564. }
  1565. catch (Exception e)
  1566. {
  1567. m_log.WarnFormat("{0} DisableDeactivationExtension exception in object {1}: {2}", LogHeader, this.Name, e);
  1568. ret = null;
  1569. }
  1570. return ret; // not implemented yet
  1571. }
  1572. // Get the passed parameter if it exists and is of the right type.
  1573. // This returns the default value if the parameter is not present or is the wrong type.
  1574. private T PassedParam<T>(object[] pParams, int index, T defaultValue)
  1575. {
  1576. if (pParams.GetLength(0) > index)
  1577. {
  1578. if (pParams[index] is T)
  1579. {
  1580. return (T)pParams[index];
  1581. }
  1582. }
  1583. return defaultValue;
  1584. }
  1585. // Set the locking parameters.
  1586. // If an axis is locked, the limits for the axis are set to zero,
  1587. // If the axis is being constrained, the high and low value are passed and set.
  1588. // When done here, LockedXXXAxis flags are set and LockedXXXAxixLow/High are set to the range.
  1589. protected void ApplyAxisLimits(int funct, float low, float high)
  1590. {
  1591. DetailLog("{0} ApplyAxisLimits. op={1}, low={2}, high={3}", LocalID, funct, low, high);
  1592. float linearMax = 23000f;
  1593. float angularMax = (float)Math.PI;
  1594. switch (funct)
  1595. {
  1596. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR:
  1597. this.LockedLinearAxis = new OMV.Vector3(LockedAxis, LockedAxis, LockedAxis);
  1598. this.LockedLinearAxisLow = OMV.Vector3.Zero;
  1599. this.LockedLinearAxisHigh = OMV.Vector3.Zero;
  1600. break;
  1601. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_X:
  1602. this.LockedLinearAxis.X = LockedAxis;
  1603. this.LockedLinearAxisLow.X = 0f;
  1604. this.LockedLinearAxisHigh.X = 0f;
  1605. break;
  1606. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_X:
  1607. this.LockedLinearAxis.X = LockedAxis;
  1608. this.LockedLinearAxisLow.X = OMV.Utils.Clamp(low, -linearMax, linearMax);
  1609. this.LockedLinearAxisHigh.X = OMV.Utils.Clamp(high, -linearMax, linearMax);
  1610. break;
  1611. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Y:
  1612. this.LockedLinearAxis.Y = LockedAxis;
  1613. this.LockedLinearAxisLow.Y = 0f;
  1614. this.LockedLinearAxisHigh.Y = 0f;
  1615. break;
  1616. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Y:
  1617. this.LockedLinearAxis.Y = LockedAxis;
  1618. this.LockedLinearAxisLow.Y = OMV.Utils.Clamp(low, -linearMax, linearMax);
  1619. this.LockedLinearAxisHigh.Y = OMV.Utils.Clamp(high, -linearMax, linearMax);
  1620. break;
  1621. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Z:
  1622. this.LockedLinearAxis.Z = LockedAxis;
  1623. this.LockedLinearAxisLow.Z = 0f;
  1624. this.LockedLinearAxisHigh.Z = 0f;
  1625. break;
  1626. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Z:
  1627. this.LockedLinearAxis.Z = LockedAxis;
  1628. this.LockedLinearAxisLow.Z = OMV.Utils.Clamp(low, -linearMax, linearMax);
  1629. this.LockedLinearAxisHigh.Z = OMV.Utils.Clamp(high, -linearMax, linearMax);
  1630. break;
  1631. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR:
  1632. this.LockedAngularAxis = new OMV.Vector3(LockedAxis, LockedAxis, LockedAxis);
  1633. this.LockedAngularAxisLow = OMV.Vector3.Zero;
  1634. this.LockedAngularAxisHigh = OMV.Vector3.Zero;
  1635. break;
  1636. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_X:
  1637. this.LockedAngularAxis.X = LockedAxis;
  1638. this.LockedAngularAxisLow.X = 0;
  1639. this.LockedAngularAxisHigh.X = 0;
  1640. break;
  1641. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_X:
  1642. this.LockedAngularAxis.X = LockedAxis;
  1643. this.LockedAngularAxisLow.X = OMV.Utils.Clamp(low, -angularMax, angularMax);
  1644. this.LockedAngularAxisHigh.X = OMV.Utils.Clamp(high, -angularMax, angularMax);
  1645. break;
  1646. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Y:
  1647. this.LockedAngularAxis.Y = LockedAxis;
  1648. this.LockedAngularAxisLow.Y = 0;
  1649. this.LockedAngularAxisHigh.Y = 0;
  1650. break;
  1651. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Y:
  1652. this.LockedAngularAxis.Y = LockedAxis;
  1653. this.LockedAngularAxisLow.Y = OMV.Utils.Clamp(low, -angularMax, angularMax);
  1654. this.LockedAngularAxisHigh.Y = OMV.Utils.Clamp(high, -angularMax, angularMax);
  1655. break;
  1656. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Z:
  1657. this.LockedAngularAxis.Z = LockedAxis;
  1658. this.LockedAngularAxisLow.Z = 0;
  1659. this.LockedAngularAxisHigh.Z = 0;
  1660. break;
  1661. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Z:
  1662. this.LockedAngularAxis.Z = LockedAxis;
  1663. this.LockedAngularAxisLow.Z = OMV.Utils.Clamp(low, -angularMax, angularMax);
  1664. this.LockedAngularAxisHigh.Z = OMV.Utils.Clamp(high, -angularMax, angularMax);
  1665. break;
  1666. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR:
  1667. this.LockedLinearAxis = LockedAxisFree;
  1668. this.LockedLinearAxisLow = new OMV.Vector3(-linearMax, -linearMax, -linearMax);
  1669. this.LockedLinearAxisHigh = new OMV.Vector3(linearMax, linearMax, linearMax);
  1670. break;
  1671. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_X:
  1672. this.LockedLinearAxis.X = FreeAxis;
  1673. this.LockedLinearAxisLow.X = -linearMax;
  1674. this.LockedLinearAxisHigh.X = linearMax;
  1675. break;
  1676. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Y:
  1677. this.LockedLinearAxis.Y = FreeAxis;
  1678. this.LockedLinearAxisLow.Y = -linearMax;
  1679. this.LockedLinearAxisHigh.Y = linearMax;
  1680. break;
  1681. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Z:
  1682. this.LockedLinearAxis.Z = FreeAxis;
  1683. this.LockedLinearAxisLow.Z = -linearMax;
  1684. this.LockedLinearAxisHigh.Z = linearMax;
  1685. break;
  1686. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR:
  1687. this.LockedAngularAxis = LockedAxisFree;
  1688. this.LockedAngularAxisLow = new OMV.Vector3(-angularMax, -angularMax, -angularMax);
  1689. this.LockedAngularAxisHigh = new OMV.Vector3(angularMax, angularMax, angularMax);
  1690. break;
  1691. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_X:
  1692. this.LockedAngularAxis.X = FreeAxis;
  1693. this.LockedAngularAxisLow.X = -angularMax;
  1694. this.LockedAngularAxisHigh.X = angularMax;
  1695. break;
  1696. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Y:
  1697. this.LockedAngularAxis.Y = FreeAxis;
  1698. this.LockedAngularAxisLow.Y = -angularMax;
  1699. this.LockedAngularAxisHigh.Y = angularMax;
  1700. break;
  1701. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Z:
  1702. this.LockedAngularAxis.Z = FreeAxis;
  1703. this.LockedAngularAxisLow.Z = -angularMax;
  1704. this.LockedAngularAxisHigh.Z = angularMax;
  1705. break;
  1706. case ExtendedPhysics.PHYS_AXIS_UNLOCK:
  1707. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR, 0f, 0f);
  1708. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR, 0f, 0f);
  1709. break;
  1710. default:
  1711. break;
  1712. }
  1713. return;
  1714. }
  1715. #endregion // Extension
  1716. // The physics engine says that properties have updated. Update same and inform
  1717. // the world that things have changed.
  1718. // NOTE: BSPrim.UpdateProperties is overloaded by BSPrimLinkable which modifies updates from root and children prims.
  1719. // NOTE: BSPrim.UpdateProperties is overloaded by BSPrimDisplaced which handles mapping physical position to simulator position.
  1720. public override void UpdateProperties(EntityProperties entprop)
  1721. {
  1722. // Let anyone (like the actors) modify the updated properties before they are pushed into the object and the simulator.
  1723. TriggerPreUpdatePropertyAction(ref entprop);
  1724. // DetailLog("{0},BSPrim.UpdateProperties,entry,entprop={1}", LocalID, entprop); // DEBUG DEBUG
  1725. // Assign directly to the local variables so the normal set actions do not happen
  1726. RawPosition = entprop.Position;
  1727. RawOrientation = entprop.Rotation;
  1728. // DEBUG DEBUG DEBUG -- smooth velocity changes a bit. The simulator seems to be
  1729. // very sensitive to velocity changes.
  1730. if (entprop.Velocity == OMV.Vector3.Zero || !entprop.Velocity.ApproxEquals(RawVelocity, BSParam.UpdateVelocityChangeThreshold))
  1731. RawVelocity = entprop.Velocity;
  1732. _acceleration = entprop.Acceleration;
  1733. RawRotationalVelocity = entprop.RotationalVelocity;
  1734. // DetailLog("{0},BSPrim.UpdateProperties,afterAssign,entprop={1}", LocalID, entprop); // DEBUG DEBUG
  1735. // The sanity check can change the velocity and/or position.
  1736. if (PositionSanityCheck(true /* inTaintTime */ ))
  1737. {
  1738. entprop.Position = RawPosition;
  1739. entprop.Velocity = RawVelocity;
  1740. entprop.RotationalVelocity = RawRotationalVelocity;
  1741. entprop.Acceleration = _acceleration;
  1742. }
  1743. OMV.Vector3 direction = OMV.Vector3.UnitX * RawOrientation; // DEBUG DEBUG DEBUG
  1744. DetailLog("{0},BSPrim.UpdateProperties,call,entProp={1},dir={2}", LocalID, entprop, direction);
  1745. // remember the current and last set values
  1746. LastEntityProperties = CurrentEntityProperties;
  1747. CurrentEntityProperties = entprop;
  1748. PhysScene.PostUpdate(this);
  1749. }
  1750. }
  1751. }