BSPrim.cs 74 KB

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