1
0

BSPrim.cs 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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.Physics.Manager;
  35. using OpenSim.Region.Physics.ConvexDecompositionDotNet;
  36. using OpenSim.Region.OptionalModules.Scripting; // for ExtendedPhysics
  37. namespace OpenSim.Region.Physics.BulletSPlugin
  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. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR:
  1490. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR, 0f, 0f);
  1491. index += 1;
  1492. break;
  1493. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_X:
  1494. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_X, 0f, 0f);
  1495. index += 1;
  1496. break;
  1497. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_X:
  1498. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_X, (float)pParams[index + 1], (float)pParams[index + 2]);
  1499. index += 3;
  1500. break;
  1501. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Y:
  1502. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Y, 0f, 0f);
  1503. index += 1;
  1504. break;
  1505. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Y:
  1506. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Y, (float)pParams[index + 1], (float)pParams[index + 2]);
  1507. index += 3;
  1508. break;
  1509. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Z:
  1510. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Z, 0f, 0f);
  1511. index += 1;
  1512. break;
  1513. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Z:
  1514. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Z, (float)pParams[index + 1], (float)pParams[index + 2]);
  1515. index += 3;
  1516. break;
  1517. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR:
  1518. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR, 0f, 0f);
  1519. index += 1;
  1520. break;
  1521. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_X:
  1522. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_X, 0f, 0f);
  1523. index += 1;
  1524. break;
  1525. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_X:
  1526. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_X, (float)pParams[index + 1], (float)pParams[index + 2]);
  1527. index += 3;
  1528. break;
  1529. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Y:
  1530. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Y, 0f, 0f);
  1531. index += 1;
  1532. break;
  1533. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Y:
  1534. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Y, (float)pParams[index + 1], (float)pParams[index + 2]);
  1535. index += 3;
  1536. break;
  1537. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Z:
  1538. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Z, 0f, 0f);
  1539. index += 1;
  1540. break;
  1541. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Z:
  1542. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Z, (float)pParams[index + 1], (float)pParams[index + 2]);
  1543. index += 3;
  1544. break;
  1545. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR:
  1546. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR, 0f, 0f);
  1547. index += 1;
  1548. break;
  1549. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_X:
  1550. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_X, 0f, 0f);
  1551. index += 1;
  1552. break;
  1553. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Y:
  1554. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Y, 0f, 0f);
  1555. index += 1;
  1556. break;
  1557. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Z:
  1558. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Z, 0f, 0f);
  1559. index += 1;
  1560. break;
  1561. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR:
  1562. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR, 0f, 0f);
  1563. index += 1;
  1564. break;
  1565. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_X:
  1566. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_X, 0f, 0f);
  1567. index += 1;
  1568. break;
  1569. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Y:
  1570. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Y, 0f, 0f);
  1571. index += 1;
  1572. break;
  1573. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Z:
  1574. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Z, 0f, 0f);
  1575. index += 1;
  1576. break;
  1577. case ExtendedPhysics.PHYS_AXIS_UNLOCK:
  1578. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK, 0f, 0f);
  1579. index += 1;
  1580. break;
  1581. default:
  1582. m_log.WarnFormat("{0} SetSxisLockLimitsExtension. Unknown op={1}", LogHeader, funct);
  1583. index += 1;
  1584. break;
  1585. }
  1586. }
  1587. }
  1588. InitializeAxisActor();
  1589. ret = (object)index;
  1590. }
  1591. }
  1592. catch (Exception e)
  1593. {
  1594. m_log.WarnFormat("{0} SetSxisLockLimitsExtension exception in object {1}: {2}", LogHeader, this.Name, e);
  1595. ret = null;
  1596. }
  1597. return ret; // not implemented yet
  1598. }
  1599. // Set the locking parameters.
  1600. // If an axis is locked, the limits for the axis are set to zero,
  1601. // If the axis is being constrained, the high and low value are passed and set.
  1602. // When done here, LockedXXXAxis flags are set and LockedXXXAxixLow/High are set to the range.
  1603. protected void ApplyAxisLimits(int funct, float low, float high)
  1604. {
  1605. DetailLog("{0} ApplyAxisLimits. op={1}, low={2}, high={3}", LocalID, funct, low, high);
  1606. float linearMax = 23000f;
  1607. float angularMax = (float)Math.PI;
  1608. switch ((int)funct)
  1609. {
  1610. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR:
  1611. this.LockedLinearAxis = new OMV.Vector3(LockedAxis, LockedAxis, LockedAxis);
  1612. this.LockedLinearAxisLow = OMV.Vector3.Zero;
  1613. this.LockedLinearAxisHigh = OMV.Vector3.Zero;
  1614. break;
  1615. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_X:
  1616. this.LockedLinearAxis.X = LockedAxis;
  1617. this.LockedLinearAxisLow.X = 0f;
  1618. this.LockedLinearAxisHigh.X = 0f;
  1619. break;
  1620. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_X:
  1621. this.LockedLinearAxis.X = LockedAxis;
  1622. this.LockedLinearAxisLow.X = Util.Clip(low, -linearMax, linearMax);
  1623. this.LockedLinearAxisHigh.X = Util.Clip(high, -linearMax, linearMax);
  1624. break;
  1625. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Y:
  1626. this.LockedLinearAxis.Y = LockedAxis;
  1627. this.LockedLinearAxisLow.Y = 0f;
  1628. this.LockedLinearAxisHigh.Y = 0f;
  1629. break;
  1630. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Y:
  1631. this.LockedLinearAxis.Y = LockedAxis;
  1632. this.LockedLinearAxisLow.Y = Util.Clip(low, -linearMax, linearMax);
  1633. this.LockedLinearAxisHigh.Y = Util.Clip(high, -linearMax, linearMax);
  1634. break;
  1635. case ExtendedPhysics.PHYS_AXIS_LOCK_LINEAR_Z:
  1636. this.LockedLinearAxis.Z = LockedAxis;
  1637. this.LockedLinearAxisLow.Z = 0f;
  1638. this.LockedLinearAxisHigh.Z = 0f;
  1639. break;
  1640. case ExtendedPhysics.PHYS_AXIS_LIMIT_LINEAR_Z:
  1641. this.LockedLinearAxis.Z = LockedAxis;
  1642. this.LockedLinearAxisLow.Z = Util.Clip(low, -linearMax, linearMax);
  1643. this.LockedLinearAxisHigh.Z = Util.Clip(high, -linearMax, linearMax);
  1644. break;
  1645. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR:
  1646. this.LockedAngularAxis = new OMV.Vector3(LockedAxis, LockedAxis, LockedAxis);
  1647. this.LockedAngularAxisLow = OMV.Vector3.Zero;
  1648. this.LockedAngularAxisHigh = OMV.Vector3.Zero;
  1649. break;
  1650. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_X:
  1651. this.LockedAngularAxis.X = LockedAxis;
  1652. this.LockedAngularAxisLow.X = 0;
  1653. this.LockedAngularAxisHigh.X = 0;
  1654. break;
  1655. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_X:
  1656. this.LockedAngularAxis.X = LockedAxis;
  1657. this.LockedAngularAxisLow.X = Util.Clip(low, -angularMax, angularMax);
  1658. this.LockedAngularAxisHigh.X = Util.Clip(high, -angularMax, angularMax);
  1659. break;
  1660. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Y:
  1661. this.LockedAngularAxis.Y = LockedAxis;
  1662. this.LockedAngularAxisLow.Y = 0;
  1663. this.LockedAngularAxisHigh.Y = 0;
  1664. break;
  1665. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Y:
  1666. this.LockedAngularAxis.Y = LockedAxis;
  1667. this.LockedAngularAxisLow.Y = Util.Clip(low, -angularMax, angularMax);
  1668. this.LockedAngularAxisHigh.Y = Util.Clip(high, -angularMax, angularMax);
  1669. break;
  1670. case ExtendedPhysics.PHYS_AXIS_LOCK_ANGULAR_Z:
  1671. this.LockedAngularAxis.Z = LockedAxis;
  1672. this.LockedAngularAxisLow.Z = 0;
  1673. this.LockedAngularAxisHigh.Z = 0;
  1674. break;
  1675. case ExtendedPhysics.PHYS_AXIS_LIMIT_ANGULAR_Z:
  1676. this.LockedAngularAxis.Z = LockedAxis;
  1677. this.LockedAngularAxisLow.Z = Util.Clip(low, -angularMax, angularMax);
  1678. this.LockedAngularAxisHigh.Z = Util.Clip(high, -angularMax, angularMax);
  1679. break;
  1680. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR:
  1681. this.LockedLinearAxis = LockedAxisFree;
  1682. this.LockedLinearAxisLow = new OMV.Vector3(-linearMax, -linearMax, -linearMax);
  1683. this.LockedLinearAxisHigh = new OMV.Vector3(linearMax, linearMax, linearMax);
  1684. break;
  1685. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_X:
  1686. this.LockedLinearAxis.X = FreeAxis;
  1687. this.LockedLinearAxisLow.X = -linearMax;
  1688. this.LockedLinearAxisHigh.X = linearMax;
  1689. break;
  1690. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Y:
  1691. this.LockedLinearAxis.Y = FreeAxis;
  1692. this.LockedLinearAxisLow.Y = -linearMax;
  1693. this.LockedLinearAxisHigh.Y = linearMax;
  1694. break;
  1695. case ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR_Z:
  1696. this.LockedLinearAxis.Z = FreeAxis;
  1697. this.LockedLinearAxisLow.Z = -linearMax;
  1698. this.LockedLinearAxisHigh.Z = linearMax;
  1699. break;
  1700. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR:
  1701. this.LockedAngularAxis = LockedAxisFree;
  1702. this.LockedAngularAxisLow = new OMV.Vector3(-angularMax, -angularMax, -angularMax);
  1703. this.LockedAngularAxisHigh = new OMV.Vector3(angularMax, angularMax, angularMax);
  1704. break;
  1705. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_X:
  1706. this.LockedAngularAxis.X = FreeAxis;
  1707. this.LockedAngularAxisLow.X = -angularMax;
  1708. this.LockedAngularAxisHigh.X = angularMax;
  1709. break;
  1710. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Y:
  1711. this.LockedAngularAxis.Y = FreeAxis;
  1712. this.LockedAngularAxisLow.Y = -angularMax;
  1713. this.LockedAngularAxisHigh.Y = angularMax;
  1714. break;
  1715. case ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR_Z:
  1716. this.LockedAngularAxis.Z = FreeAxis;
  1717. this.LockedAngularAxisLow.Z = -angularMax;
  1718. this.LockedAngularAxisHigh.Z = angularMax;
  1719. break;
  1720. case ExtendedPhysics.PHYS_AXIS_UNLOCK:
  1721. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_LINEAR, 0f, 0f);
  1722. ApplyAxisLimits(ExtendedPhysics.PHYS_AXIS_UNLOCK_ANGULAR, 0f, 0f);
  1723. break;
  1724. default:
  1725. break;
  1726. }
  1727. return;
  1728. }
  1729. #endregion // Extension
  1730. // The physics engine says that properties have updated. Update same and inform
  1731. // the world that things have changed.
  1732. // NOTE: BSPrim.UpdateProperties is overloaded by BSPrimLinkable which modifies updates from root and children prims.
  1733. // NOTE: BSPrim.UpdateProperties is overloaded by BSPrimDisplaced which handles mapping physical position to simulator position.
  1734. public override void UpdateProperties(EntityProperties entprop)
  1735. {
  1736. // Let anyone (like the actors) modify the updated properties before they are pushed into the object and the simulator.
  1737. TriggerPreUpdatePropertyAction(ref entprop);
  1738. // DetailLog("{0},BSPrim.UpdateProperties,entry,entprop={1}", LocalID, entprop); // DEBUG DEBUG
  1739. // Assign directly to the local variables so the normal set actions do not happen
  1740. RawPosition = entprop.Position;
  1741. RawOrientation = entprop.Rotation;
  1742. // DEBUG DEBUG DEBUG -- smooth velocity changes a bit. The simulator seems to be
  1743. // very sensitive to velocity changes.
  1744. if (entprop.Velocity == OMV.Vector3.Zero || !entprop.Velocity.ApproxEquals(RawVelocity, BSParam.UpdateVelocityChangeThreshold))
  1745. RawVelocity = entprop.Velocity;
  1746. _acceleration = entprop.Acceleration;
  1747. _rotationalVelocity = entprop.RotationalVelocity;
  1748. // DetailLog("{0},BSPrim.UpdateProperties,afterAssign,entprop={1}", LocalID, entprop); // DEBUG DEBUG
  1749. // The sanity check can change the velocity and/or position.
  1750. if (PositionSanityCheck(true /* inTaintTime */ ))
  1751. {
  1752. entprop.Position = RawPosition;
  1753. entprop.Velocity = RawVelocity;
  1754. entprop.RotationalVelocity = _rotationalVelocity;
  1755. entprop.Acceleration = _acceleration;
  1756. }
  1757. OMV.Vector3 direction = OMV.Vector3.UnitX * RawOrientation; // DEBUG DEBUG DEBUG
  1758. DetailLog("{0},BSPrim.UpdateProperties,call,entProp={1},dir={2}", LocalID, entprop, direction);
  1759. // remember the current and last set values
  1760. LastEntityProperties = CurrentEntityProperties;
  1761. CurrentEntityProperties = entprop;
  1762. PhysScene.PostUpdate(this);
  1763. }
  1764. }
  1765. }