BulletSimAPI.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. public struct ConvexHull
  34. {
  35. Vector3 Offset;
  36. int VertexCount;
  37. Vector3[] Vertices;
  38. }
  39. public struct ShapeData
  40. {
  41. public enum PhysicsShapeType
  42. {
  43. SHAPE_AVATAR = 0,
  44. SHAPE_BOX = 1,
  45. SHAPE_CONE = 2,
  46. SHAPE_CYLINDER = 3,
  47. SHAPE_SPHERE = 4,
  48. SHAPE_HULL = 5
  49. };
  50. public const int numericTrue = 1;
  51. public const int numericFalse = 0;
  52. public uint ID;
  53. public PhysicsShapeType Type;
  54. public Vector3 Position;
  55. public Quaternion Rotation;
  56. public Vector3 Velocity;
  57. public Vector3 Scale;
  58. public float Mass;
  59. public float Buoyancy;
  60. public System.UInt64 MeshKey;
  61. public int Collidable;
  62. public float Friction;
  63. public int Static; // true if a static object. Otherwise gravity, etc.
  64. // note that bools are passed as ints since bool size changes by language
  65. }
  66. public struct SweepHit
  67. {
  68. public uint ID;
  69. public float Fraction;
  70. public Vector3 Normal;
  71. public Vector3 Point;
  72. }
  73. public struct RaycastHit
  74. {
  75. public uint ID;
  76. public float Fraction;
  77. public Vector3 Normal;
  78. }
  79. public struct EntityProperties
  80. {
  81. public uint ID;
  82. public Vector3 Position;
  83. public Quaternion Rotation;
  84. public Vector3 Velocity;
  85. public Vector3 Acceleration;
  86. public Vector3 AngularVelocity;
  87. }
  88. static class BulletSimAPI {
  89. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  90. public static extern uint Initialize(Vector3 maxPosition);
  91. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  92. public static extern void SetHeightmap(uint worldID, [MarshalAs(UnmanagedType.LPArray)] float[] heightMap);
  93. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  94. public static extern void Shutdown(uint worldID);
  95. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  96. public static extern int PhysicsStep(uint worldID, float timeStep, int maxSubSteps, float fixedTimeStep,
  97. out int updatedEntityCount,
  98. out IntPtr updatedEntitiesPtr,
  99. out int collidersCount,
  100. out IntPtr collidersPtr);
  101. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  102. public static extern bool CreateHull(uint worldID, System.UInt64 meshKey, int hullCount,
  103. [MarshalAs(UnmanagedType.LPArray)] float[] hulls
  104. );
  105. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  106. public static extern bool DestroyHull(uint worldID, System.UInt64 meshKey);
  107. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  108. public static extern bool CreateObject(uint worldID, ShapeData shapeData);
  109. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  110. public static extern void CreateLinkset(uint worldID, int objectCount, ShapeData[] shapeDatas);
  111. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  112. public static extern void AddConstraint(uint worldID, uint id1, uint id2,
  113. Vector3 frame1, Vector3 frame2, Vector3 lowLinear, Vector3 hiLinear, Vector3 lowAngular, Vector3 hiAngular);
  114. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  115. public static extern bool RemoveConstraint(uint worldID, uint id1, uint id2);
  116. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  117. public static extern Vector3 GetObjectPosition(uint WorldID, uint id);
  118. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  119. public static extern bool SetObjectTranslation(uint worldID, uint id, Vector3 position, Quaternion rotation);
  120. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  121. public static extern bool SetObjectVelocity(uint worldID, uint id, Vector3 velocity);
  122. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  123. public static extern bool SetObjectAngularVelocity(uint worldID, uint id, Vector3 angularVelocity);
  124. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  125. public static extern bool SetObjectForce(uint worldID, uint id, Vector3 force);
  126. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  127. public static extern bool SetObjectScaleMass(uint worldID, uint id, Vector3 scale, float mass, bool isDynamic);
  128. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  129. public static extern bool SetObjectCollidable(uint worldID, uint id, bool phantom);
  130. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  131. public static extern bool SetObjectDynamic(uint worldID, uint id, bool isDynamic, float mass);
  132. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  133. public static extern bool SetObjectGhost(uint worldID, uint id, bool ghostly);
  134. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  135. public static extern bool SetObjectProperties(uint worldID, uint id, bool isStatic, bool isSolid, bool genCollisions, float mass);
  136. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  137. public static extern bool SetObjectBuoyancy(uint worldID, uint id, float buoyancy);
  138. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  139. public static extern bool HasObject(uint worldID, uint id);
  140. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  141. public static extern bool DestroyObject(uint worldID, uint id);
  142. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  143. public static extern SweepHit ConvexSweepTest(uint worldID, uint id, Vector3 to, float extraMargin);
  144. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  145. public static extern RaycastHit RayTest(uint worldID, uint id, Vector3 from, Vector3 to);
  146. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  147. public static extern Vector3 RecoverFromPenetration(uint worldID, uint id);
  148. // Log a debug message
  149. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  150. public delegate void DebugLogCallback([MarshalAs(UnmanagedType.LPStr)]string msg);
  151. [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
  152. public static extern void SetDebugLogCallback(DebugLogCallback callback);
  153. }
  154. }