BSPrim.cs 76 KB

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