BulletSimData.cs 12 KB

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