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 OpenSim.Region.OptionalModules.Scripting;
  33. using OMV = OpenMetaverse;
  34. namespace OpenSim.Region.Physics.BulletSPlugin
  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. private static readonly string LogHeader = "[BULLETS PRIMLINKABLE]";
  42. // This adds the overrides for link() and delink() so the prim is linkable.
  43. public BSLinkset Linkset { get; set; }
  44. // The index of this child prim.
  45. public int LinksetChildIndex { get; set; }
  46. public BSLinkset.LinksetImplementation LinksetType { get; set; }
  47. public BSPrimLinkable(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
  48. OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
  49. : base(localID, primName, parent_scene, pos, size, rotation, pbs, pisPhysical)
  50. {
  51. // Default linkset implementation for this prim
  52. LinksetType = (BSLinkset.LinksetImplementation)BSParam.LinksetImplementation;
  53. Linkset = BSLinkset.Factory(PhysScene, this);
  54. Linkset.Refresh(this);
  55. }
  56. public override void Destroy()
  57. {
  58. Linkset = Linkset.RemoveMeFromLinkset(this, false /* inTaintTime */);
  59. base.Destroy();
  60. }
  61. public override void link(Manager.PhysicsActor obj)
  62. {
  63. BSPrimLinkable parent = obj as BSPrimLinkable;
  64. if (parent != null)
  65. {
  66. BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
  67. int childrenBefore = Linkset.NumberOfChildren; // DEBUG
  68. Linkset = parent.Linkset.AddMeToLinkset(this);
  69. DetailLog("{0},BSPrimLinkable.link,call,parentBefore={1}, childrenBefore=={2}, parentAfter={3}, childrenAfter={4}",
  70. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  71. }
  72. return;
  73. }
  74. public override void delink()
  75. {
  76. // TODO: decide if this parent checking needs to happen at taint time
  77. // Race condition here: if link() and delink() in same simulation tick, the delink will not happen
  78. BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
  79. int childrenBefore = Linkset.NumberOfChildren; // DEBUG
  80. Linkset = Linkset.RemoveMeFromLinkset(this, false /* inTaintTime*/);
  81. DetailLog("{0},BSPrimLinkable.delink,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}, ",
  82. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  83. return;
  84. }
  85. // When simulator changes position, this might be moving a child of the linkset.
  86. public override OMV.Vector3 Position
  87. {
  88. get { return base.Position; }
  89. set
  90. {
  91. base.Position = value;
  92. PhysScene.TaintedObject(LocalID, "BSPrimLinkable.setPosition", delegate()
  93. {
  94. Linkset.UpdateProperties(UpdatedProperties.Position, this);
  95. });
  96. }
  97. }
  98. // When simulator changes orientation, this might be moving a child of the linkset.
  99. public override OMV.Quaternion Orientation
  100. {
  101. get { return base.Orientation; }
  102. set
  103. {
  104. base.Orientation = value;
  105. PhysScene.TaintedObject(LocalID, "BSPrimLinkable.setOrientation", delegate()
  106. {
  107. Linkset.UpdateProperties(UpdatedProperties.Orientation, this);
  108. });
  109. }
  110. }
  111. public override float TotalMass
  112. {
  113. get { return Linkset.LinksetMass; }
  114. }
  115. public override OMV.Vector3 CenterOfMass
  116. {
  117. get { return Linkset.CenterOfMass; }
  118. }
  119. public override OMV.Vector3 GeometricCenter
  120. {
  121. get { return Linkset.GeometricCenter; }
  122. }
  123. // Refresh the linkset structure and parameters when the prim's physical parameters are changed.
  124. public override void UpdatePhysicalParameters()
  125. {
  126. base.UpdatePhysicalParameters();
  127. // Recompute any linkset parameters.
  128. // When going from non-physical to physical, this re-enables the constraints that
  129. // had been automatically disabled when the mass was set to zero.
  130. // For compound based linksets, this enables and disables interactions of the children.
  131. if (Linkset != null) // null can happen during initialization
  132. Linkset.Refresh(this);
  133. }
  134. // When the prim is made dynamic or static, the linkset needs to change.
  135. protected override void MakeDynamic(bool makeStatic)
  136. {
  137. base.MakeDynamic(makeStatic);
  138. if (Linkset != null) // null can happen during initialization
  139. {
  140. if (makeStatic)
  141. Linkset.MakeStatic(this);
  142. else
  143. Linkset.MakeDynamic(this);
  144. }
  145. }
  146. // Body is being taken apart. Remove physical dependencies and schedule a rebuild.
  147. protected override void RemoveDependencies()
  148. {
  149. Linkset.RemoveDependencies(this);
  150. base.RemoveDependencies();
  151. }
  152. // Called after a simulation step for the changes in physical object properties.
  153. // Do any filtering/modification needed for linksets.
  154. public override void UpdateProperties(EntityProperties entprop)
  155. {
  156. if (Linkset.IsRoot(this) || Linkset.ShouldReportPropertyUpdates(this))
  157. {
  158. // Properties are only updated for the roots of a linkset.
  159. // TODO: this will have to change when linksets are articulated.
  160. base.UpdateProperties(entprop);
  161. }
  162. /*
  163. else
  164. {
  165. // For debugging, report the movement of children
  166. DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
  167. LocalID, entprop.Position, entprop.Rotation, entprop.Velocity,
  168. entprop.Acceleration, entprop.RotationalVelocity);
  169. }
  170. */
  171. // The linkset might like to know about changing locations
  172. Linkset.UpdateProperties(UpdatedProperties.EntPropUpdates, this);
  173. }
  174. // Called after a simulation step to post a collision with this object.
  175. // This returns 'true' if the collision has been queued and the SendCollisions call must
  176. // be made at the end of the simulation step.
  177. public override bool Collide(uint collidingWith, BSPhysObject collidee,
  178. 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(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth))
  183. {
  184. // The linkset didn't handle it so pass the collision through normal processing
  185. ret = base.Collide(collidingWith, 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. }