BSPrim.cs 76 KB

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