BulletSimAPI.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 terrainFriction;
  121. public float terrainHitFraction;
  122. public float terrainRestitution;
  123. public float avatarFriction;
  124. public float avatarDensity;
  125. public float avatarRestitution;
  126. public float avatarCapsuleRadius;
  127. public float avatarCapsuleHeight;
  128. public const float numericTrue = 1f;
  129. public const float numericFalse = 0f;
  130. }
  131. static class BulletSimAPI {
  132. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  133. [return: MarshalAs(UnmanagedType.LPStr)]
  134. public static extern string GetVersion();
  135. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  136. public static extern uint Initialize(Vector3 maxPosition, IntPtr parms,
  137. int maxCollisions, IntPtr collisionArray,
  138. int maxUpdates, IntPtr updateArray);
  139. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  140. public static extern bool UpdateParameter(uint worldID, uint localID,
  141. [MarshalAs(UnmanagedType.LPStr)]string paramCode, float value);
  142. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  143. public static extern void SetHeightmap(uint worldID, [MarshalAs(UnmanagedType.LPArray)] float[] heightMap);
  144. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  145. public static extern void Shutdown(uint worldID);
  146. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  147. public static extern int PhysicsStep(uint worldID, float timeStep, int maxSubSteps, float fixedTimeStep,
  148. out int updatedEntityCount,
  149. out IntPtr updatedEntitiesPtr,
  150. out int collidersCount,
  151. out IntPtr collidersPtr);
  152. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  153. public static extern bool CreateHull(uint worldID, System.UInt64 meshKey,
  154. int hullCount, [MarshalAs(UnmanagedType.LPArray)] float[] hulls
  155. );
  156. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  157. public static extern bool CreateMesh(uint worldID, System.UInt64 meshKey,
  158. int indexCount, [MarshalAs(UnmanagedType.LPArray)] int[] indices,
  159. int verticesCount, [MarshalAs(UnmanagedType.LPArray)] float[] vertices
  160. );
  161. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  162. public static extern bool DestroyHull(uint worldID, System.UInt64 meshKey);
  163. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  164. public static extern bool DestroyMesh(uint worldID, System.UInt64 meshKey);
  165. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  166. public static extern bool CreateObject(uint worldID, ShapeData shapeData);
  167. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  168. public static extern void CreateLinkset(uint worldID, int objectCount, ShapeData[] shapeDatas);
  169. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  170. public static extern void AddConstraint(uint worldID, uint id1, uint id2,
  171. Vector3 frame1, Quaternion frame1rot,
  172. Vector3 frame2, Quaternion frame2rot,
  173. Vector3 lowLinear, Vector3 hiLinear, Vector3 lowAngular, Vector3 hiAngular);
  174. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  175. public static extern bool RemoveConstraintByID(uint worldID, uint id1);
  176. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  177. public static extern bool RemoveConstraint(uint worldID, uint id1, uint id2);
  178. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  179. public static extern Vector3 GetObjectPosition(uint WorldID, uint id);
  180. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  181. public static extern bool SetObjectTranslation(uint worldID, uint id, Vector3 position, Quaternion rotation);
  182. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  183. public static extern bool SetObjectVelocity(uint worldID, uint id, Vector3 velocity);
  184. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  185. public static extern bool SetObjectAngularVelocity(uint worldID, uint id, Vector3 angularVelocity);
  186. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  187. public static extern bool SetObjectForce(uint worldID, uint id, Vector3 force);
  188. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  189. public static extern bool SetObjectScaleMass(uint worldID, uint id, Vector3 scale, float mass, bool isDynamic);
  190. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  191. public static extern bool SetObjectCollidable(uint worldID, uint id, bool phantom);
  192. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  193. public static extern bool SetObjectDynamic(uint worldID, uint id, bool isDynamic, float mass);
  194. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  195. public static extern bool SetObjectGhost(uint worldID, uint id, bool ghostly);
  196. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  197. public static extern bool SetObjectProperties(uint worldID, uint id, bool isStatic, bool isSolid, bool genCollisions, float mass);
  198. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  199. public static extern bool SetObjectBuoyancy(uint worldID, uint id, float buoyancy);
  200. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  201. public static extern bool HasObject(uint worldID, uint id);
  202. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  203. public static extern bool DestroyObject(uint worldID, uint id);
  204. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  205. public static extern SweepHit ConvexSweepTest(uint worldID, uint id, Vector3 to, float extraMargin);
  206. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  207. public static extern RaycastHit RayTest(uint worldID, uint id, Vector3 from, Vector3 to);
  208. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  209. public static extern Vector3 RecoverFromPenetration(uint worldID, uint id);
  210. // Log a debug message
  211. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  212. public delegate void DebugLogCallback([MarshalAs(UnmanagedType.LPStr)]string msg);
  213. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  214. public static extern void SetDebugLogCallback(DebugLogCallback callback);
  215. }
  216. }