BSPrimLinkable.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Collections.Generic;
  29. using System.Linq;
  30. using System.Text;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.OptionalModules.Scripting;
  33. using OMV = OpenMetaverse;
  34. namespace OpenSim.Region.PhysicsModule.BulletS
  35. {
  36. public class BSPrimLinkable : BSPrimDisplaced
  37. {
  38. // The purpose of this subclass is to add linkset functionality to the prim. This overrides
  39. // operations necessary for keeping the linkset created and, additionally, this
  40. // calls the linkset implementation for its creation and management.
  41. #pragma warning disable 414
  42. private static readonly string LogHeader = "[BULLETS PRIMLINKABLE]";
  43. #pragma warning restore 414
  44. // This adds the overrides for link() and delink() so the prim is linkable.
  45. public BSLinkset Linkset { get; set; }
  46. // The index of this child prim.
  47. public int LinksetChildIndex { get; set; }
  48. public BSLinkset.LinksetImplementation LinksetType { get; set; }
  49. public BSPrimLinkable(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
  50. OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
  51. : base(localID, primName, parent_scene, pos, size, rotation, pbs, pisPhysical)
  52. {
  53. // Default linkset implementation for this prim
  54. LinksetType = (BSLinkset.LinksetImplementation)BSParam.LinksetImplementation;
  55. Linkset = BSLinkset.Factory(PhysScene, this);
  56. Linkset.Refresh(this);
  57. }
  58. public override void Destroy()
  59. {
  60. Linkset = Linkset.RemoveMeFromLinkset(this, false /* inTaintTime */);
  61. base.Destroy();
  62. }
  63. public override void link(OpenSim.Region.PhysicsModules.SharedBase.PhysicsActor obj)
  64. {
  65. BSPrimLinkable parent = obj as BSPrimLinkable;
  66. if (parent != null)
  67. {
  68. BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
  69. int childrenBefore = Linkset.NumberOfChildren; // DEBUG
  70. Linkset = parent.Linkset.AddMeToLinkset(this);
  71. DetailLog("{0},BSPrimLinkable.link,call,parentBefore={1}, childrenBefore=={2}, parentAfter={3}, childrenAfter={4}",
  72. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  73. }
  74. return;
  75. }
  76. public override void delink()
  77. {
  78. // TODO: decide if this parent checking needs to happen at taint time
  79. // Race condition here: if link() and delink() in same simulation tick, the delink will not happen
  80. BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
  81. int childrenBefore = Linkset.NumberOfChildren; // DEBUG
  82. Linkset = Linkset.RemoveMeFromLinkset(this, false /* inTaintTime*/);
  83. DetailLog("{0},BSPrimLinkable.delink,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}, ",
  84. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  85. return;
  86. }
  87. // When simulator changes position, this might be moving a child of the linkset.
  88. public override OMV.Vector3 Position
  89. {
  90. get { return base.Position; }
  91. set
  92. {
  93. base.Position = value;
  94. PhysScene.TaintedObject(LocalID, "BSPrimLinkable.setPosition", delegate()
  95. {
  96. Linkset.UpdateProperties(UpdatedProperties.Position, this);
  97. });
  98. }
  99. }
  100. // When simulator changes orientation, this might be moving a child of the linkset.
  101. public override OMV.Quaternion Orientation
  102. {
  103. get { return base.Orientation; }
  104. set
  105. {
  106. base.Orientation = value;
  107. PhysScene.TaintedObject(LocalID, "BSPrimLinkable.setOrientation", delegate()
  108. {
  109. Linkset.UpdateProperties(UpdatedProperties.Orientation, this);
  110. });
  111. }
  112. }
  113. public override float TotalMass
  114. {
  115. get { return Linkset.LinksetMass; }
  116. }
  117. public override OMV.Vector3 CenterOfMass
  118. {
  119. get { return Linkset.CenterOfMass; }
  120. }
  121. public override OMV.Vector3 GeometricCenter
  122. {
  123. get { return Linkset.GeometricCenter; }
  124. }
  125. // Refresh the linkset structure and parameters when the prim's physical parameters are changed.
  126. public override void UpdatePhysicalParameters()
  127. {
  128. base.UpdatePhysicalParameters();
  129. // Recompute any linkset parameters.
  130. // When going from non-physical to physical, this re-enables the constraints that
  131. // had been automatically disabled when the mass was set to zero.
  132. // For compound based linksets, this enables and disables interactions of the children.
  133. if (Linkset != null) // null can happen during initialization
  134. Linkset.Refresh(this);
  135. }
  136. // When the prim is made dynamic or static, the linkset needs to change.
  137. protected override void MakeDynamic(bool makeStatic)
  138. {
  139. base.MakeDynamic(makeStatic);
  140. if (Linkset != null) // null can happen during initialization
  141. {
  142. if (makeStatic)
  143. Linkset.MakeStatic(this);
  144. else
  145. Linkset.MakeDynamic(this);
  146. }
  147. }
  148. // Body is being taken apart. Remove physical dependencies and schedule a rebuild.
  149. protected override void RemoveDependencies()
  150. {
  151. Linkset.RemoveDependencies(this);
  152. base.RemoveDependencies();
  153. }
  154. // Called after a simulation step for the changes in physical object properties.
  155. // Do any filtering/modification needed for linksets.
  156. public override void UpdateProperties(EntityProperties entprop)
  157. {
  158. if (Linkset.IsRoot(this) || Linkset.ShouldReportPropertyUpdates(this))
  159. {
  160. // Properties are only updated for the roots of a linkset.
  161. // TODO: this will have to change when linksets are articulated.
  162. base.UpdateProperties(entprop);
  163. }
  164. /*
  165. else
  166. {
  167. // For debugging, report the movement of children
  168. DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
  169. LocalID, entprop.Position, entprop.Rotation, entprop.Velocity,
  170. entprop.Acceleration, entprop.RotationalVelocity);
  171. }
  172. */
  173. // The linkset might like to know about changing locations
  174. Linkset.UpdateProperties(UpdatedProperties.EntPropUpdates, this);
  175. }
  176. // Called after a simulation step to post a collision with this object.
  177. // This returns 'true' if the collision has been queued and the SendCollisions call must
  178. // be made at the end of the simulation step.
  179. public override bool Collide(uint collidingWith, BSPhysObject collidee,
  180. OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
  181. {
  182. bool ret = false;
  183. // Ask the linkset if it wants to handle the collision
  184. if (!Linkset.HandleCollide(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth))
  185. {
  186. // The linkset didn't handle it so pass the collision through normal processing
  187. ret = base.Collide(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth);
  188. }
  189. return ret;
  190. }
  191. // A linkset reports any collision on any part of the linkset.
  192. public long SomeCollisionSimulationStep = 0;
  193. public override bool HasSomeCollision
  194. {
  195. get
  196. {
  197. return (SomeCollisionSimulationStep == PhysScene.SimulationStep) || base.IsColliding;
  198. }
  199. set
  200. {
  201. if (value)
  202. SomeCollisionSimulationStep = PhysScene.SimulationStep;
  203. else
  204. SomeCollisionSimulationStep = 0;
  205. base.HasSomeCollision = value;
  206. }
  207. }
  208. // Convert the existing linkset of this prim into a new type.
  209. public bool ConvertLinkset(BSLinkset.LinksetImplementation newType)
  210. {
  211. bool ret = false;
  212. if (LinksetType != newType)
  213. {
  214. DetailLog("{0},BSPrimLinkable.ConvertLinkset,oldT={1},newT={2}", LocalID, LinksetType, newType);
  215. // Set the implementation type first so the call to BSLinkset.Factory gets the new type.
  216. this.LinksetType = newType;
  217. BSLinkset oldLinkset = this.Linkset;
  218. BSLinkset newLinkset = BSLinkset.Factory(PhysScene, this);
  219. this.Linkset = newLinkset;
  220. // Pick up any physical dependencies this linkset might have in the physics engine.
  221. oldLinkset.RemoveDependencies(this);
  222. // Create a list of the children (mainly because can't interate through a list that's changing)
  223. List<BSPrimLinkable> children = new List<BSPrimLinkable>();
  224. oldLinkset.ForEachMember((child) =>
  225. {
  226. if (!oldLinkset.IsRoot(child))
  227. children.Add(child);
  228. return false; // 'false' says to continue to next member
  229. });
  230. // Remove the children from the old linkset and add to the new (will be a new instance from the factory)
  231. foreach (BSPrimLinkable child in children)
  232. {
  233. oldLinkset.RemoveMeFromLinkset(child, true /*inTaintTime*/);
  234. }
  235. foreach (BSPrimLinkable child in children)
  236. {
  237. newLinkset.AddMeToLinkset(child);
  238. child.Linkset = newLinkset;
  239. }
  240. // Force the shape and linkset to get reconstructed
  241. newLinkset.Refresh(this);
  242. this.ForceBodyShapeRebuild(true /* inTaintTime */);
  243. }
  244. return ret;
  245. }
  246. #region Extension
  247. public override object Extension(string pFunct, params object[] pParams)
  248. {
  249. DetailLog("{0} BSPrimLinkable.Extension,op={1},nParam={2}", LocalID, pFunct, pParams.Length);
  250. object ret = null;
  251. switch (pFunct)
  252. {
  253. // physGetLinksetType();
  254. // pParams = [ BSPhysObject root, null ]
  255. case ExtendedPhysics.PhysFunctGetLinksetType:
  256. {
  257. ret = (object)LinksetType;
  258. DetailLog("{0},BSPrimLinkable.Extension.physGetLinksetType,type={1}", LocalID, ret);
  259. break;
  260. }
  261. // physSetLinksetType(type);
  262. // pParams = [ BSPhysObject root, null, integer type ]
  263. case ExtendedPhysics.PhysFunctSetLinksetType:
  264. {
  265. if (pParams.Length > 2)
  266. {
  267. BSLinkset.LinksetImplementation linksetType = (BSLinkset.LinksetImplementation)pParams[2];
  268. if (Linkset.IsRoot(this))
  269. {
  270. PhysScene.TaintedObject(LocalID, "BSPrim.PhysFunctSetLinksetType", delegate()
  271. {
  272. // Cause the linkset type to change
  273. DetailLog("{0},BSPrimLinkable.Extension.physSetLinksetType, oldType={1},newType={2}",
  274. LocalID, Linkset.LinksetImpl, linksetType);
  275. ConvertLinkset(linksetType);
  276. });
  277. }
  278. ret = (object)(int)linksetType;
  279. }
  280. break;
  281. }
  282. // physChangeLinkType(linknum, typeCode);
  283. // pParams = [ BSPhysObject root, BSPhysObject child, integer linkType ]
  284. case ExtendedPhysics.PhysFunctChangeLinkType:
  285. {
  286. ret = Linkset.Extension(pFunct, pParams);
  287. break;
  288. }
  289. // physGetLinkType(linknum);
  290. // pParams = [ BSPhysObject root, BSPhysObject child ]
  291. case ExtendedPhysics.PhysFunctGetLinkType:
  292. {
  293. ret = Linkset.Extension(pFunct, pParams);
  294. break;
  295. }
  296. // physChangeLinkParams(linknum, [code, value, code, value, ...]);
  297. // pParams = [ BSPhysObject root, BSPhysObject child, object[] [ string op, object opParam, string op, object opParam, ... ] ]
  298. case ExtendedPhysics.PhysFunctChangeLinkParams:
  299. {
  300. ret = Linkset.Extension(pFunct, pParams);
  301. break;
  302. }
  303. default:
  304. ret = base.Extension(pFunct, pParams);
  305. break;
  306. }
  307. return ret;
  308. }
  309. #endregion // Extension
  310. }
  311. }