BulletSimData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.Text;
  30. using OMV = OpenMetaverse;
  31. namespace OpenSim.Region.PhysicsModule.BulletS
  32. {
  33. // Classes to allow some type checking for the API
  34. // These hold pointers to allocated objects in the unmanaged space.
  35. // These classes are subclassed by the various physical implementations of
  36. // objects. In particular, there is a version for physical instances in
  37. // unmanaged memory ("unman") and one for in managed memory ("XNA").
  38. // Currently, the instances of these classes are a reference to a
  39. // physical representation and this has no releationship to other
  40. // instances. Someday, refarb the usage of these classes so each instance
  41. // refers to a particular physical instance and this class controls reference
  42. // counts and such. This should be done along with adding BSShapes.
  43. public class BulletWorld
  44. {
  45. public BulletWorld(uint worldId, BSScene bss)
  46. {
  47. worldID = worldId;
  48. physicsScene = bss;
  49. }
  50. public uint worldID;
  51. // The scene is only in here so very low level routines have a handle to print debug/error messages
  52. public BSScene physicsScene;
  53. }
  54. // An allocated Bullet btRigidBody
  55. public class BulletBody
  56. {
  57. public BulletBody(uint id)
  58. {
  59. ID = id;
  60. collisionType = CollisionType.Static;
  61. }
  62. public uint ID;
  63. public CollisionType collisionType;
  64. public virtual void Clear() { }
  65. public virtual bool HasPhysicalBody { get { return false; } }
  66. // Apply the specificed collision mask into the physical world
  67. public virtual bool ApplyCollisionMask(BSScene physicsScene)
  68. {
  69. // Should assert the body has been added to the physical world.
  70. // (The collision masks are stored in the collision proxy cache which only exists for
  71. // a collision body that is in the world.)
  72. return physicsScene.PE.SetCollisionGroupMask(this,
  73. BulletSimData.CollisionTypeMasks[collisionType].group,
  74. BulletSimData.CollisionTypeMasks[collisionType].mask);
  75. }
  76. // Used for log messages for a unique display of the memory/object allocated to this instance
  77. public virtual string AddrString
  78. {
  79. get { return "unknown"; }
  80. }
  81. public override string ToString()
  82. {
  83. StringBuilder buff = new StringBuilder();
  84. buff.Append("<id=");
  85. buff.Append(ID.ToString());
  86. buff.Append(",p=");
  87. buff.Append(AddrString);
  88. buff.Append(",c=");
  89. buff.Append(collisionType);
  90. buff.Append(">");
  91. return buff.ToString();
  92. }
  93. }
  94. // Handle to btCollisionObject - a shape that can be added to a btRidgidBody
  95. public class BulletShape
  96. {
  97. public BulletShape()
  98. {
  99. shapeType = BSPhysicsShapeType.SHAPE_UNKNOWN;
  100. shapeKey = (System.UInt64)FixedShapeKey.KEY_NONE;
  101. isNativeShape = false;
  102. }
  103. public BSPhysicsShapeType shapeType;
  104. public System.UInt64 shapeKey;
  105. public bool isNativeShape;
  106. public virtual void Clear() { }
  107. public virtual bool HasPhysicalShape { get { return false; } }
  108. // Make another reference to this physical object.
  109. public virtual BulletShape Clone() { return new BulletShape(); }
  110. // Return 'true' if this and other refer to the same physical object
  111. public virtual bool ReferenceSame(BulletShape xx) { return false; }
  112. // Used for log messages for a unique display of the memory/object allocated to this instance
  113. public virtual string AddrString
  114. {
  115. get { return "unknown"; }
  116. }
  117. public override string ToString()
  118. {
  119. StringBuilder buff = new StringBuilder();
  120. buff.Append("<p=");
  121. buff.Append(AddrString);
  122. buff.Append(",s=");
  123. buff.Append(shapeType.ToString());
  124. buff.Append(",k=");
  125. buff.Append(shapeKey.ToString("X"));
  126. buff.Append(",n=");
  127. buff.Append(isNativeShape.ToString());
  128. buff.Append(">");
  129. return buff.ToString();
  130. }
  131. }
  132. // An allocated Bullet btConstraint
  133. public class BulletConstraint
  134. {
  135. public BulletConstraint()
  136. {
  137. }
  138. public virtual void Clear() { }
  139. public virtual bool HasPhysicalConstraint { get { return false; } }
  140. // Used for log messages for a unique display of the memory/object allocated to this instance
  141. public virtual string AddrString
  142. {
  143. get { return "unknown"; }
  144. }
  145. }
  146. // An allocated HeightMapThing which holds various heightmap info.
  147. // Made a class rather than a struct so there would be only one
  148. // instance of this and C# will pass around pointers rather
  149. // than making copies.
  150. public class BulletHMapInfo
  151. {
  152. public BulletHMapInfo(uint id, float[] hm, float pSizeX, float pSizeY) {
  153. ID = id;
  154. heightMap = hm;
  155. terrainRegionBase = OMV.Vector3.Zero;
  156. minCoords = new OMV.Vector3(100f, 100f, 25f);
  157. maxCoords = new OMV.Vector3(101f, 101f, 26f);
  158. minZ = maxZ = 0f;
  159. sizeX = pSizeX;
  160. sizeY = pSizeY;
  161. }
  162. public uint ID;
  163. public float[] heightMap;
  164. public OMV.Vector3 terrainRegionBase;
  165. public OMV.Vector3 minCoords;
  166. public OMV.Vector3 maxCoords;
  167. public float sizeX, sizeY;
  168. public float minZ, maxZ;
  169. public BulletShape terrainShape;
  170. public BulletBody terrainBody;
  171. }
  172. // The general class of collsion object.
  173. public enum CollisionType
  174. {
  175. Avatar,
  176. PhantomToOthersAvatar, // An avatar that it phantom to other avatars but not to anything else
  177. Groundplane,
  178. Terrain,
  179. Static,
  180. Dynamic,
  181. VolumeDetect,
  182. // Linkset, // A linkset should be either Static or Dynamic
  183. LinksetChild,
  184. Unknown
  185. };
  186. // Hold specification of group and mask collision flags for a CollisionType
  187. public struct CollisionTypeFilterGroup
  188. {
  189. public CollisionTypeFilterGroup(CollisionType t, uint g, uint m)
  190. {
  191. type = t;
  192. group = g;
  193. mask = m;
  194. }
  195. public CollisionType type;
  196. public uint group;
  197. public uint mask;
  198. };
  199. public static class BulletSimData
  200. {
  201. // Map of collisionTypes to flags for collision groups and masks.
  202. // An object's 'group' is the collison groups this object belongs to
  203. // An object's 'filter' is the groups another object has to belong to in order to collide with me
  204. // A collision happens if ((obj1.group & obj2.filter) != 0) || ((obj2.group & obj1.filter) != 0)
  205. //
  206. // As mentioned above, don't use the CollisionFilterGroups definitions directly in the code
  207. // but, instead, use references to this dictionary. Finding and debugging
  208. // collision flag problems will be made easier.
  209. public static Dictionary<CollisionType, CollisionTypeFilterGroup> CollisionTypeMasks
  210. = new Dictionary<CollisionType, CollisionTypeFilterGroup>()
  211. {
  212. { CollisionType.Avatar,
  213. new CollisionTypeFilterGroup(CollisionType.Avatar,
  214. (uint)CollisionFilterGroups.BCharacterGroup,
  215. (uint)(CollisionFilterGroups.BAllGroup))
  216. },
  217. { CollisionType.PhantomToOthersAvatar,
  218. new CollisionTypeFilterGroup(CollisionType.PhantomToOthersAvatar,
  219. (uint)CollisionFilterGroups.BCharacterGroup,
  220. (uint)(CollisionFilterGroups.BAllGroup & ~CollisionFilterGroups.BCharacterGroup))
  221. },
  222. { CollisionType.Groundplane,
  223. new CollisionTypeFilterGroup(CollisionType.Groundplane,
  224. (uint)CollisionFilterGroups.BGroundPlaneGroup,
  225. // (uint)CollisionFilterGroups.BAllGroup)
  226. (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  227. },
  228. { CollisionType.Terrain,
  229. new CollisionTypeFilterGroup(CollisionType.Terrain,
  230. (uint)CollisionFilterGroups.BTerrainGroup,
  231. (uint)(CollisionFilterGroups.BAllGroup & ~CollisionFilterGroups.BStaticGroup))
  232. },
  233. { CollisionType.Static,
  234. new CollisionTypeFilterGroup(CollisionType.Static,
  235. (uint)CollisionFilterGroups.BStaticGroup,
  236. (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  237. },
  238. { CollisionType.Dynamic,
  239. new CollisionTypeFilterGroup(CollisionType.Dynamic,
  240. (uint)CollisionFilterGroups.BSolidGroup,
  241. (uint)(CollisionFilterGroups.BAllGroup))
  242. },
  243. { CollisionType.VolumeDetect,
  244. new CollisionTypeFilterGroup(CollisionType.VolumeDetect,
  245. (uint)CollisionFilterGroups.BSensorTrigger,
  246. (uint)(~CollisionFilterGroups.BSensorTrigger))
  247. },
  248. { CollisionType.LinksetChild,
  249. new CollisionTypeFilterGroup(CollisionType.LinksetChild,
  250. (uint)CollisionFilterGroups.BLinksetChildGroup,
  251. (uint)(CollisionFilterGroups.BNoneGroup))
  252. // (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  253. },
  254. };
  255. }
  256. }