BSPrimLinkable.cs 14 KB

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