BSPrimLinkable.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. Linkset.RemoveDependencies(this);
  151. base.RemoveDependencies();
  152. }
  153. // Called after a simulation step for the changes in physical object properties.
  154. // Do any filtering/modification needed for linksets.
  155. public override void UpdateProperties(EntityProperties entprop)
  156. {
  157. if (Linkset.IsRoot(this) || Linkset.ShouldReportPropertyUpdates(this))
  158. {
  159. // Properties are only updated for the roots of a linkset.
  160. // TODO: this will have to change when linksets are articulated.
  161. base.UpdateProperties(entprop);
  162. }
  163. /*
  164. else
  165. {
  166. // For debugging, report the movement of children
  167. DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
  168. LocalID, entprop.Position, entprop.Rotation, entprop.Velocity,
  169. entprop.Acceleration, entprop.RotationalVelocity);
  170. }
  171. */
  172. // The linkset might like to know about changing locations
  173. Linkset.UpdateProperties(UpdatedProperties.EntPropUpdates, this);
  174. }
  175. // Called after a simulation step to post a collision with this object.
  176. // This returns 'true' if the collision has been queued and the SendCollisions call must
  177. // be made at the end of the simulation step.
  178. public override bool Collide(BSPhysObject collidee, OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
  179. {
  180. bool ret = false;
  181. // Ask the linkset if it wants to handle the collision
  182. if (!Linkset.HandleCollide(this, collidee, contactPoint, contactNormal, pentrationDepth))
  183. {
  184. // The linkset didn't handle it so pass the collision through normal processing
  185. ret = base.Collide(collidee, contactPoint, contactNormal, pentrationDepth);
  186. }
  187. return ret;
  188. }
  189. // A linkset reports any collision on any part of the linkset.
  190. public long SomeCollisionSimulationStep = 0;
  191. public override bool HasSomeCollision
  192. {
  193. get
  194. {
  195. return (SomeCollisionSimulationStep == PhysScene.SimulationStep) || base.IsColliding;
  196. }
  197. set
  198. {
  199. if (value)
  200. SomeCollisionSimulationStep = PhysScene.SimulationStep;
  201. else
  202. SomeCollisionSimulationStep = 0;
  203. base.HasSomeCollision = value;
  204. }
  205. }
  206. // Convert the existing linkset of this prim into a new type.
  207. public bool ConvertLinkset(BSLinkset.LinksetImplementation newType)
  208. {
  209. bool ret = false;
  210. if (LinksetType != newType)
  211. {
  212. DetailLog("{0},BSPrimLinkable.ConvertLinkset,oldT={1},newT={2}", LocalID, LinksetType, newType);
  213. // Set the implementation type first so the call to BSLinkset.Factory gets the new type.
  214. this.LinksetType = newType;
  215. BSLinkset oldLinkset = this.Linkset;
  216. BSLinkset newLinkset = BSLinkset.Factory(PhysScene, this);
  217. this.Linkset = newLinkset;
  218. // Pick up any physical dependencies this linkset might have in the physics engine.
  219. oldLinkset.RemoveDependencies(this);
  220. // Create a list of the children (mainly because can't interate through a list that's changing)
  221. List<BSPrimLinkable> children = new List<BSPrimLinkable>();
  222. oldLinkset.ForEachMember((child) =>
  223. {
  224. if (!oldLinkset.IsRoot(child))
  225. children.Add(child);
  226. return false; // 'false' says to continue to next member
  227. });
  228. // Remove the children from the old linkset and add to the new (will be a new instance from the factory)
  229. foreach (BSPrimLinkable child in children)
  230. {
  231. oldLinkset.RemoveMeFromLinkset(child, true /*inTaintTime*/);
  232. }
  233. foreach (BSPrimLinkable child in children)
  234. {
  235. newLinkset.AddMeToLinkset(child);
  236. child.Linkset = newLinkset;
  237. }
  238. // Force the shape and linkset to get reconstructed
  239. newLinkset.Refresh(this);
  240. this.ForceBodyShapeRebuild(true /* inTaintTime */);
  241. }
  242. return ret;
  243. }
  244. #region Extension
  245. public override object Extension(string pFunct, params object[] pParams)
  246. {
  247. DetailLog("{0} BSPrimLinkable.Extension,op={1},nParam={2}", LocalID, pFunct, pParams.Length);
  248. object ret = null;
  249. switch (pFunct)
  250. {
  251. // physGetLinksetType();
  252. // pParams = [ BSPhysObject root, null ]
  253. case ExtendedPhysics.PhysFunctGetLinksetType:
  254. {
  255. ret = (object)LinksetType;
  256. DetailLog("{0},BSPrimLinkable.Extension.physGetLinksetType,type={1}", LocalID, ret);
  257. break;
  258. }
  259. // physSetLinksetType(type);
  260. // pParams = [ BSPhysObject root, null, integer type ]
  261. case ExtendedPhysics.PhysFunctSetLinksetType:
  262. {
  263. if (pParams.Length > 2)
  264. {
  265. BSLinkset.LinksetImplementation linksetType = (BSLinkset.LinksetImplementation)pParams[2];
  266. if (Linkset.IsRoot(this))
  267. {
  268. PhysScene.TaintedObject(LocalID, "BSPrim.PhysFunctSetLinksetType", delegate()
  269. {
  270. // Cause the linkset type to change
  271. DetailLog("{0},BSPrimLinkable.Extension.physSetLinksetType, oldType={1},newType={2}",
  272. LocalID, Linkset.LinksetImpl, linksetType);
  273. ConvertLinkset(linksetType);
  274. });
  275. }
  276. ret = (object)(int)linksetType;
  277. }
  278. break;
  279. }
  280. // physChangeLinkType(linknum, typeCode);
  281. // pParams = [ BSPhysObject root, BSPhysObject child, integer linkType ]
  282. case ExtendedPhysics.PhysFunctChangeLinkType:
  283. {
  284. ret = Linkset.Extension(pFunct, pParams);
  285. break;
  286. }
  287. // physGetLinkType(linknum);
  288. // pParams = [ BSPhysObject root, BSPhysObject child ]
  289. case ExtendedPhysics.PhysFunctGetLinkType:
  290. {
  291. ret = Linkset.Extension(pFunct, pParams);
  292. break;
  293. }
  294. // physChangeLinkParams(linknum, [code, value, code, value, ...]);
  295. // pParams = [ BSPhysObject root, BSPhysObject child, object[] [ string op, object opParam, string op, object opParam, ... ] ]
  296. case ExtendedPhysics.PhysFunctChangeLinkParams:
  297. {
  298. ret = Linkset.Extension(pFunct, pParams);
  299. break;
  300. }
  301. default:
  302. ret = base.Extension(pFunct, pParams);
  303. break;
  304. }
  305. return ret;
  306. }
  307. #endregion // Extension
  308. }
  309. }