BulletSimAPI.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.Runtime.InteropServices;
  29. using System.Security;
  30. using System.Text;
  31. using OpenMetaverse;
  32. namespace OpenSim.Region.Physics.BulletSPlugin {
  33. [StructLayout(LayoutKind.Sequential)]
  34. public struct ConvexHull
  35. {
  36. Vector3 Offset;
  37. int VertexCount;
  38. Vector3[] Vertices;
  39. }
  40. [StructLayout(LayoutKind.Sequential)]
  41. public struct ShapeData
  42. {
  43. public enum PhysicsShapeType
  44. {
  45. SHAPE_AVATAR = 0,
  46. SHAPE_BOX = 1,
  47. SHAPE_CONE = 2,
  48. SHAPE_CYLINDER = 3,
  49. SHAPE_SPHERE = 4,
  50. SHAPE_MESH = 5,
  51. SHAPE_HULL = 6
  52. };
  53. public uint ID;
  54. public PhysicsShapeType Type;
  55. public Vector3 Position;
  56. public Quaternion Rotation;
  57. public Vector3 Velocity;
  58. public Vector3 Scale;
  59. public float Mass;
  60. public float Buoyancy;
  61. public System.UInt64 HullKey;
  62. public System.UInt64 MeshKey;
  63. public float Friction;
  64. public float Restitution;
  65. public int Collidable;
  66. public int Static; // true if a static object. Otherwise gravity, etc.
  67. // note that bools are passed as ints since bool size changes by language and architecture
  68. public const int numericTrue = 1;
  69. public const int numericFalse = 0;
  70. }
  71. [StructLayout(LayoutKind.Sequential)]
  72. public struct SweepHit
  73. {
  74. public uint ID;
  75. public float Fraction;
  76. public Vector3 Normal;
  77. public Vector3 Point;
  78. }
  79. [StructLayout(LayoutKind.Sequential)]
  80. public struct RaycastHit
  81. {
  82. public uint ID;
  83. public float Fraction;
  84. public Vector3 Normal;
  85. }
  86. [StructLayout(LayoutKind.Sequential)]
  87. public struct CollisionDesc
  88. {
  89. public uint aID;
  90. public uint bID;
  91. public Vector3 point;
  92. public Vector3 normal;
  93. }
  94. [StructLayout(LayoutKind.Sequential)]
  95. public struct EntityProperties
  96. {
  97. public uint ID;
  98. public Vector3 Position;
  99. public Quaternion Rotation;
  100. public Vector3 Velocity;
  101. public Vector3 Acceleration;
  102. public Vector3 RotationalVelocity;
  103. }
  104. // Format of this structure must match the definition in the C++ code
  105. [StructLayout(LayoutKind.Sequential)]
  106. public struct ConfigurationParameters
  107. {
  108. public float defaultFriction;
  109. public float defaultDensity;
  110. public float defaultRestitution;
  111. public float collisionMargin;
  112. public float gravity;
  113. public float linearDamping;
  114. public float angularDamping;
  115. public float deactivationTime;
  116. public float linearSleepingThreshold;
  117. public float angularSleepingThreshold;
  118. public float ccdMotionThreshold;
  119. public float ccdSweptSphereRadius;
  120. public float contactProcessingThreshold;
  121. public float terrainFriction;
  122. public float terrainHitFraction;
  123. public float terrainRestitution;
  124. public float avatarFriction;
  125. public float avatarDensity;
  126. public float avatarRestitution;
  127. public float avatarCapsuleRadius;
  128. public float avatarCapsuleHeight;
  129. public const float numericTrue = 1f;
  130. public const float numericFalse = 0f;
  131. }
  132. static class BulletSimAPI {
  133. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  134. [return: MarshalAs(UnmanagedType.LPStr)]
  135. public static extern string GetVersion();
  136. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  137. public static extern uint Initialize(Vector3 maxPosition, IntPtr parms,
  138. int maxCollisions, IntPtr collisionArray,
  139. int maxUpdates, IntPtr updateArray);
  140. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  141. public static extern bool UpdateParameter(uint worldID, uint localID,
  142. [MarshalAs(UnmanagedType.LPStr)]string paramCode, float value);
  143. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  144. public static extern void SetHeightmap(uint worldID, [MarshalAs(UnmanagedType.LPArray)] float[] heightMap);
  145. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  146. public static extern void Shutdown(uint worldID);
  147. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  148. public static extern int PhysicsStep(uint worldID, float timeStep, int maxSubSteps, float fixedTimeStep,
  149. out int updatedEntityCount,
  150. out IntPtr updatedEntitiesPtr,
  151. out int collidersCount,
  152. out IntPtr collidersPtr);
  153. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  154. public static extern bool CreateHull(uint worldID, System.UInt64 meshKey,
  155. int hullCount, [MarshalAs(UnmanagedType.LPArray)] float[] hulls
  156. );
  157. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  158. public static extern bool CreateMesh(uint worldID, System.UInt64 meshKey,
  159. int indexCount, [MarshalAs(UnmanagedType.LPArray)] int[] indices,
  160. int verticesCount, [MarshalAs(UnmanagedType.LPArray)] float[] vertices
  161. );
  162. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  163. public static extern bool DestroyHull(uint worldID, System.UInt64 meshKey);
  164. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  165. public static extern bool DestroyMesh(uint worldID, System.UInt64 meshKey);
  166. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  167. public static extern bool CreateObject(uint worldID, ShapeData shapeData);
  168. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  169. public static extern void CreateLinkset(uint worldID, int objectCount, ShapeData[] shapeDatas);
  170. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  171. public static extern void AddConstraint(uint worldID, uint id1, uint id2,
  172. Vector3 frame1, Quaternion frame1rot,
  173. Vector3 frame2, Quaternion frame2rot,
  174. Vector3 lowLinear, Vector3 hiLinear, Vector3 lowAngular, Vector3 hiAngular);
  175. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  176. public static extern bool RemoveConstraintByID(uint worldID, uint id1);
  177. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  178. public static extern bool RemoveConstraint(uint worldID, uint id1, uint id2);
  179. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  180. public static extern Vector3 GetObjectPosition(uint WorldID, uint id);
  181. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  182. public static extern bool SetObjectTranslation(uint worldID, uint id, Vector3 position, Quaternion rotation);
  183. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  184. public static extern bool SetObjectVelocity(uint worldID, uint id, Vector3 velocity);
  185. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  186. public static extern bool SetObjectAngularVelocity(uint worldID, uint id, Vector3 angularVelocity);
  187. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  188. public static extern bool SetObjectForce(uint worldID, uint id, Vector3 force);
  189. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  190. public static extern bool SetObjectScaleMass(uint worldID, uint id, Vector3 scale, float mass, bool isDynamic);
  191. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  192. public static extern bool SetObjectCollidable(uint worldID, uint id, bool phantom);
  193. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  194. public static extern bool SetObjectDynamic(uint worldID, uint id, bool isDynamic, float mass);
  195. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  196. public static extern bool SetObjectGhost(uint worldID, uint id, bool ghostly);
  197. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  198. public static extern bool SetObjectProperties(uint worldID, uint id, bool isStatic, bool isSolid, bool genCollisions, float mass);
  199. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  200. public static extern bool SetObjectBuoyancy(uint worldID, uint id, float buoyancy);
  201. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  202. public static extern bool HasObject(uint worldID, uint id);
  203. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  204. public static extern bool DestroyObject(uint worldID, uint id);
  205. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  206. public static extern SweepHit ConvexSweepTest(uint worldID, uint id, Vector3 to, float extraMargin);
  207. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  208. public static extern RaycastHit RayTest(uint worldID, uint id, Vector3 from, Vector3 to);
  209. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  210. public static extern Vector3 RecoverFromPenetration(uint worldID, uint id);
  211. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  212. public static extern void DumpBulletStatistics();
  213. // Log a debug message
  214. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  215. public delegate void DebugLogCallback([MarshalAs(UnmanagedType.LPStr)]string msg);
  216. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  217. public static extern void SetDebugLogCallback(DebugLogCallback callback);
  218. }
  219. }