BulletSimData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.Physics.BulletSPlugin
  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. public class BulletShape
  95. {
  96. public BulletShape()
  97. {
  98. shapeType = BSPhysicsShapeType.SHAPE_UNKNOWN;
  99. shapeKey = (System.UInt64)FixedShapeKey.KEY_NONE;
  100. isNativeShape = false;
  101. }
  102. public BSPhysicsShapeType shapeType;
  103. public System.UInt64 shapeKey;
  104. public bool isNativeShape;
  105. public virtual void Clear() { }
  106. public virtual bool HasPhysicalShape { get { return false; } }
  107. // Make another reference to this physical object.
  108. public virtual BulletShape Clone() { return new BulletShape(); }
  109. // Return 'true' if this and other refer to the same physical object
  110. public virtual bool ReferenceSame(BulletShape xx) { return false; }
  111. // Used for log messages for a unique display of the memory/object allocated to this instance
  112. public virtual string AddrString
  113. {
  114. get { return "unknown"; }
  115. }
  116. public override string ToString()
  117. {
  118. StringBuilder buff = new StringBuilder();
  119. buff.Append("<p=");
  120. buff.Append(AddrString);
  121. buff.Append(",s=");
  122. buff.Append(shapeType.ToString());
  123. buff.Append(",k=");
  124. buff.Append(shapeKey.ToString("X"));
  125. buff.Append(",n=");
  126. buff.Append(isNativeShape.ToString());
  127. buff.Append(">");
  128. return buff.ToString();
  129. }
  130. }
  131. // An allocated Bullet btConstraint
  132. public class BulletConstraint
  133. {
  134. public BulletConstraint()
  135. {
  136. }
  137. public virtual void Clear() { }
  138. public virtual bool HasPhysicalConstraint { get { return false; } }
  139. // Used for log messages for a unique display of the memory/object allocated to this instance
  140. public virtual string AddrString
  141. {
  142. get { return "unknown"; }
  143. }
  144. }
  145. // An allocated HeightMapThing which holds various heightmap info.
  146. // Made a class rather than a struct so there would be only one
  147. // instance of this and C# will pass around pointers rather
  148. // than making copies.
  149. public class BulletHMapInfo
  150. {
  151. public BulletHMapInfo(uint id, float[] hm, float pSizeX, float pSizeY) {
  152. ID = id;
  153. heightMap = hm;
  154. terrainRegionBase = OMV.Vector3.Zero;
  155. minCoords = new OMV.Vector3(100f, 100f, 25f);
  156. maxCoords = new OMV.Vector3(101f, 101f, 26f);
  157. minZ = maxZ = 0f;
  158. sizeX = pSizeX;
  159. sizeY = pSizeY;
  160. }
  161. public uint ID;
  162. public float[] heightMap;
  163. public OMV.Vector3 terrainRegionBase;
  164. public OMV.Vector3 minCoords;
  165. public OMV.Vector3 maxCoords;
  166. public float sizeX, sizeY;
  167. public float minZ, maxZ;
  168. public BulletShape terrainShape;
  169. public BulletBody terrainBody;
  170. }
  171. // The general class of collsion object.
  172. public enum CollisionType
  173. {
  174. Avatar,
  175. Groundplane,
  176. Terrain,
  177. Static,
  178. Dynamic,
  179. VolumeDetect,
  180. // Linkset, // A linkset should be either Static or Dynamic
  181. LinksetChild,
  182. Unknown
  183. };
  184. // Hold specification of group and mask collision flags for a CollisionType
  185. public struct CollisionTypeFilterGroup
  186. {
  187. public CollisionTypeFilterGroup(CollisionType t, uint g, uint m)
  188. {
  189. type = t;
  190. group = g;
  191. mask = m;
  192. }
  193. public CollisionType type;
  194. public uint group;
  195. public uint mask;
  196. };
  197. public static class BulletSimData
  198. {
  199. // Map of collisionTypes to flags for collision groups and masks.
  200. // An object's 'group' is the collison groups this object belongs to
  201. // An object's 'filter' is the groups another object has to belong to in order to collide with me
  202. // A collision happens if ((obj1.group & obj2.filter) != 0) || ((obj2.group & obj1.filter) != 0)
  203. //
  204. // As mentioned above, don't use the CollisionFilterGroups definitions directly in the code
  205. // but, instead, use references to this dictionary. Finding and debugging
  206. // collision flag problems will be made easier.
  207. public static Dictionary<CollisionType, CollisionTypeFilterGroup> CollisionTypeMasks
  208. = new Dictionary<CollisionType, CollisionTypeFilterGroup>()
  209. {
  210. { CollisionType.Avatar,
  211. new CollisionTypeFilterGroup(CollisionType.Avatar,
  212. (uint)CollisionFilterGroups.BCharacterGroup,
  213. (uint)CollisionFilterGroups.BAllGroup)
  214. },
  215. { CollisionType.Groundplane,
  216. new CollisionTypeFilterGroup(CollisionType.Groundplane,
  217. (uint)CollisionFilterGroups.BGroundPlaneGroup,
  218. // (uint)CollisionFilterGroups.BAllGroup)
  219. (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  220. },
  221. { CollisionType.Terrain,
  222. new CollisionTypeFilterGroup(CollisionType.Terrain,
  223. (uint)CollisionFilterGroups.BTerrainGroup,
  224. (uint)(CollisionFilterGroups.BAllGroup & ~CollisionFilterGroups.BStaticGroup))
  225. },
  226. { CollisionType.Static,
  227. new CollisionTypeFilterGroup(CollisionType.Static,
  228. (uint)CollisionFilterGroups.BStaticGroup,
  229. (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  230. },
  231. { CollisionType.Dynamic,
  232. new CollisionTypeFilterGroup(CollisionType.Dynamic,
  233. (uint)CollisionFilterGroups.BSolidGroup,
  234. (uint)(CollisionFilterGroups.BAllGroup))
  235. },
  236. { CollisionType.VolumeDetect,
  237. new CollisionTypeFilterGroup(CollisionType.VolumeDetect,
  238. (uint)CollisionFilterGroups.BSensorTrigger,
  239. (uint)(~CollisionFilterGroups.BSensorTrigger))
  240. },
  241. { CollisionType.LinksetChild,
  242. new CollisionTypeFilterGroup(CollisionType.LinksetChild,
  243. (uint)CollisionFilterGroups.BLinksetChildGroup,
  244. (uint)(CollisionFilterGroups.BNoneGroup))
  245. // (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  246. },
  247. };
  248. }
  249. }