BulletSimData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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) {
  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 = sizeY = 256f;
  159. }
  160. public uint ID;
  161. public float[] heightMap;
  162. public OMV.Vector3 terrainRegionBase;
  163. public OMV.Vector3 minCoords;
  164. public OMV.Vector3 maxCoords;
  165. public float sizeX, sizeY;
  166. public float minZ, maxZ;
  167. public BulletShape terrainShape;
  168. public BulletBody terrainBody;
  169. }
  170. // The general class of collsion object.
  171. public enum CollisionType
  172. {
  173. Avatar,
  174. Groundplane,
  175. Terrain,
  176. Static,
  177. Dynamic,
  178. VolumeDetect,
  179. // Linkset, // A linkset should be either Static or Dynamic
  180. LinksetChild,
  181. Unknown
  182. };
  183. // Hold specification of group and mask collision flags for a CollisionType
  184. public struct CollisionTypeFilterGroup
  185. {
  186. public CollisionTypeFilterGroup(CollisionType t, uint g, uint m)
  187. {
  188. type = t;
  189. group = g;
  190. mask = m;
  191. }
  192. public CollisionType type;
  193. public uint group;
  194. public uint mask;
  195. };
  196. public static class BulletSimData
  197. {
  198. // Map of collisionTypes to flags for collision groups and masks.
  199. // An object's 'group' is the collison groups this object belongs to
  200. // An object's 'filter' is the groups another object has to belong to in order to collide with me
  201. // A collision happens if ((obj1.group & obj2.filter) != 0) || ((obj2.group & obj1.filter) != 0)
  202. //
  203. // As mentioned above, don't use the CollisionFilterGroups definitions directly in the code
  204. // but, instead, use references to this dictionary. Finding and debugging
  205. // collision flag problems will be made easier.
  206. public static Dictionary<CollisionType, CollisionTypeFilterGroup> CollisionTypeMasks
  207. = new Dictionary<CollisionType, CollisionTypeFilterGroup>()
  208. {
  209. { CollisionType.Avatar,
  210. new CollisionTypeFilterGroup(CollisionType.Avatar,
  211. (uint)CollisionFilterGroups.BCharacterGroup,
  212. (uint)CollisionFilterGroups.BAllGroup)
  213. },
  214. { CollisionType.Groundplane,
  215. new CollisionTypeFilterGroup(CollisionType.Groundplane,
  216. (uint)CollisionFilterGroups.BGroundPlaneGroup,
  217. // (uint)CollisionFilterGroups.BAllGroup)
  218. (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  219. },
  220. { CollisionType.Terrain,
  221. new CollisionTypeFilterGroup(CollisionType.Terrain,
  222. (uint)CollisionFilterGroups.BTerrainGroup,
  223. (uint)(CollisionFilterGroups.BAllGroup & ~CollisionFilterGroups.BStaticGroup))
  224. },
  225. { CollisionType.Static,
  226. new CollisionTypeFilterGroup(CollisionType.Static,
  227. (uint)CollisionFilterGroups.BStaticGroup,
  228. (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  229. },
  230. { CollisionType.Dynamic,
  231. new CollisionTypeFilterGroup(CollisionType.Dynamic,
  232. (uint)CollisionFilterGroups.BSolidGroup,
  233. (uint)(CollisionFilterGroups.BAllGroup))
  234. },
  235. { CollisionType.VolumeDetect,
  236. new CollisionTypeFilterGroup(CollisionType.VolumeDetect,
  237. (uint)CollisionFilterGroups.BSensorTrigger,
  238. (uint)(~CollisionFilterGroups.BSensorTrigger))
  239. },
  240. { CollisionType.LinksetChild,
  241. new CollisionTypeFilterGroup(CollisionType.LinksetChild,
  242. (uint)CollisionFilterGroups.BLinksetChildGroup,
  243. (uint)(CollisionFilterGroups.BNoneGroup))
  244. // (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
  245. },
  246. };
  247. }
  248. }