BSPrim.cs 74 KB

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