RBI_api.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2013 Blender Foundation,
  17. * All rights reserved.
  18. */
  19. /** \file
  20. * \ingroup RigidBody
  21. * \brief Rigid Body API for interfacing with external Physics Engines
  22. */
  23. #ifndef __RB_API_H__
  24. #define __RB_API_H__
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* API Notes:
  29. * Currently, this API is optimised for Bullet RigidBodies, and doesn't
  30. * take into account other Physics Engines. Some tweaking may be necessary
  31. * to allow other systems to be used, in particular there may be references
  32. * to datatypes that aren't used here...
  33. *
  34. * -- Joshua Leung (22 June 2010)
  35. */
  36. /* ********************************** */
  37. /* Partial Type Defines - Aliases for the type of data we store */
  38. // ----------
  39. /* Dynamics World */
  40. typedef struct rbDynamicsWorld rbDynamicsWorld;
  41. /* Rigid Body */
  42. typedef struct rbRigidBody rbRigidBody;
  43. /* Collision Shape */
  44. typedef struct rbCollisionShape rbCollisionShape;
  45. /* Mesh Data (for Collision Shapes of Meshes) */
  46. typedef struct rbMeshData rbMeshData;
  47. /* Constraint */
  48. typedef struct rbConstraint rbConstraint;
  49. /* ********************************** */
  50. /* Dynamics World Methods */
  51. /* Setup ---------------------------- */
  52. /* Create a new dynamics world instance */
  53. // TODO: add args to set the type of constraint solvers, etc.
  54. rbDynamicsWorld *RB_dworld_new(const float gravity[3]);
  55. /* Delete the given dynamics world, and free any extra data it may require */
  56. void RB_dworld_delete(rbDynamicsWorld *world);
  57. /* Settings ------------------------- */
  58. /* Gravity */
  59. void RB_dworld_get_gravity(rbDynamicsWorld *world, float g_out[3]);
  60. void RB_dworld_set_gravity(rbDynamicsWorld *world, const float g_in[3]);
  61. /* Constraint Solver */
  62. void RB_dworld_set_solver_iterations(rbDynamicsWorld *world, int num_solver_iterations);
  63. /* Split Impulse */
  64. void RB_dworld_set_split_impulse(rbDynamicsWorld *world, int split_impulse);
  65. /* Simulation ----------------------- */
  66. /* Step the simulation by the desired amount (in seconds) with extra controls on substep sizes and
  67. * maximum substeps */
  68. void RB_dworld_step_simulation(rbDynamicsWorld *world,
  69. float timeStep,
  70. int maxSubSteps,
  71. float timeSubStep);
  72. /* Export -------------------------- */
  73. /* Exports the dynamics world to physics simulator's serialisation format */
  74. void RB_dworld_export(rbDynamicsWorld *world, const char *filename);
  75. /* ********************************** */
  76. /* Rigid Body Methods */
  77. /* Setup ---------------------------- */
  78. /* Add RigidBody to dynamics world */
  79. void RB_dworld_add_body(rbDynamicsWorld *world, rbRigidBody *body, int col_groups);
  80. /* Remove RigidBody from dynamics world */
  81. void RB_dworld_remove_body(rbDynamicsWorld *world, rbRigidBody *body);
  82. /* Collision detection */
  83. void RB_world_convex_sweep_test(rbDynamicsWorld *world,
  84. rbRigidBody *object,
  85. const float loc_start[3],
  86. const float loc_end[3],
  87. float v_location[3],
  88. float v_hitpoint[3],
  89. float v_normal[3],
  90. int *r_hit);
  91. /* ............ */
  92. /* Create new RigidBody instance */
  93. rbRigidBody *RB_body_new(rbCollisionShape *shape, const float loc[3], const float rot[4]);
  94. /* Delete the given RigidBody instance */
  95. void RB_body_delete(rbRigidBody *body);
  96. /* Settings ------------------------- */
  97. /* 'Type' */
  98. void RB_body_set_type(rbRigidBody *body, int type, float mass);
  99. /* ............ */
  100. /* Collision Shape */
  101. void RB_body_set_collision_shape(rbRigidBody *body, rbCollisionShape *shape);
  102. /* ............ */
  103. /* Mass */
  104. float RB_body_get_mass(rbRigidBody *body);
  105. void RB_body_set_mass(rbRigidBody *body, float value);
  106. /* Friction */
  107. float RB_body_get_friction(rbRigidBody *body);
  108. void RB_body_set_friction(rbRigidBody *body, float value);
  109. /* Restitution */
  110. float RB_body_get_restitution(rbRigidBody *body);
  111. void RB_body_set_restitution(rbRigidBody *body, float value);
  112. /* Damping */
  113. float RB_body_get_linear_damping(rbRigidBody *body);
  114. void RB_body_set_linear_damping(rbRigidBody *body, float value);
  115. float RB_body_get_angular_damping(rbRigidBody *body);
  116. void RB_body_set_angular_damping(rbRigidBody *body, float value);
  117. void RB_body_set_damping(rbRigidBody *object, float linear, float angular);
  118. /* Sleeping Thresholds */
  119. float RB_body_get_linear_sleep_thresh(rbRigidBody *body);
  120. void RB_body_set_linear_sleep_thresh(rbRigidBody *body, float value);
  121. float RB_body_get_angular_sleep_thresh(rbRigidBody *body);
  122. void RB_body_set_angular_sleep_thresh(rbRigidBody *body, float value);
  123. void RB_body_set_sleep_thresh(rbRigidBody *body, float linear, float angular);
  124. /* Linear Velocity */
  125. void RB_body_get_linear_velocity(rbRigidBody *body, float v_out[3]);
  126. void RB_body_set_linear_velocity(rbRigidBody *body, const float v_in[3]);
  127. /* Angular Velocity */
  128. void RB_body_get_angular_velocity(rbRigidBody *body, float v_out[3]);
  129. void RB_body_set_angular_velocity(rbRigidBody *body, const float v_in[3]);
  130. /* Linear/Angular Factor, used to lock translation/roation axes */
  131. void RB_body_set_linear_factor(rbRigidBody *object, float x, float y, float z);
  132. void RB_body_set_angular_factor(rbRigidBody *object, float x, float y, float z);
  133. /* Kinematic State */
  134. void RB_body_set_kinematic_state(rbRigidBody *body, int kinematic);
  135. /* RigidBody Interface - Rigid Body Activation States */
  136. int RB_body_get_activation_state(rbRigidBody *body);
  137. void RB_body_set_activation_state(rbRigidBody *body, int use_deactivation);
  138. void RB_body_activate(rbRigidBody *body);
  139. void RB_body_deactivate(rbRigidBody *body);
  140. /* Simulation ----------------------- */
  141. /* Get current transform matrix of RigidBody to use in Blender (OpenGL format) */
  142. void RB_body_get_transform_matrix(rbRigidBody *body, float m_out[4][4]);
  143. /* Set RigidBody's location and rotation */
  144. void RB_body_set_loc_rot(rbRigidBody *body, const float loc[3], const float rot[4]);
  145. /* Set RigidBody's local scaling */
  146. void RB_body_set_scale(rbRigidBody *body, const float scale[3]);
  147. /* ............ */
  148. /* Get RigidBody's position as vector */
  149. void RB_body_get_position(rbRigidBody *body, float v_out[3]);
  150. /* Get RigidBody's orientation as quaternion */
  151. void RB_body_get_orientation(rbRigidBody *body, float v_out[4]);
  152. /* ............ */
  153. void RB_body_apply_central_force(rbRigidBody *body, const float v_in[3]);
  154. /* ********************************** */
  155. /* Collision Shape Methods */
  156. /* Setup (Standard Shapes) ----------- */
  157. rbCollisionShape *RB_shape_new_box(float x, float y, float z);
  158. rbCollisionShape *RB_shape_new_sphere(float radius);
  159. rbCollisionShape *RB_shape_new_capsule(float radius, float height);
  160. rbCollisionShape *RB_shape_new_cone(float radius, float height);
  161. rbCollisionShape *RB_shape_new_cylinder(float radius, float height);
  162. /* Setup (Convex Hull) ------------ */
  163. rbCollisionShape *RB_shape_new_convex_hull(
  164. float *verts, int stride, int count, float margin, bool *can_embed);
  165. /* Setup (Triangle Mesh) ---------- */
  166. /* 1 */
  167. rbMeshData *RB_trimesh_data_new(int num_tris, int num_verts);
  168. void RB_trimesh_add_vertices(rbMeshData *mesh, float *vertices, int num_verts, int vert_stride);
  169. void RB_trimesh_add_triangle_indices(
  170. rbMeshData *mesh, int num, int index0, int index1, int index2);
  171. void RB_trimesh_finish(rbMeshData *mesh);
  172. /* 2a - Triangle Meshes */
  173. rbCollisionShape *RB_shape_new_trimesh(rbMeshData *mesh);
  174. /* 2b - GImpact Meshes */
  175. rbCollisionShape *RB_shape_new_gimpact_mesh(rbMeshData *mesh);
  176. /* Cleanup --------------------------- */
  177. void RB_shape_delete(rbCollisionShape *shape);
  178. /* Settings --------------------------- */
  179. /* Collision Margin */
  180. float RB_shape_get_margin(rbCollisionShape *shape);
  181. void RB_shape_set_margin(rbCollisionShape *shape, float value);
  182. void RB_shape_trimesh_update(rbCollisionShape *shape,
  183. float *vertices,
  184. int num_verts,
  185. int vert_stride,
  186. float min[3],
  187. float max[3]);
  188. /* ********************************** */
  189. /* Constraints */
  190. /* Setup ----------------------------- */
  191. /* Add Rigid Body Constraint to simulation world */
  192. void RB_dworld_add_constraint(rbDynamicsWorld *world, rbConstraint *con, int disable_collisions);
  193. /* Remove Rigid Body Constraint from simulation world */
  194. void RB_dworld_remove_constraint(rbDynamicsWorld *world, rbConstraint *con);
  195. rbConstraint *RB_constraint_new_point(float pivot[3], rbRigidBody *rb1, rbRigidBody *rb2);
  196. rbConstraint *RB_constraint_new_fixed(float pivot[3],
  197. float orn[4],
  198. rbRigidBody *rb1,
  199. rbRigidBody *rb2);
  200. rbConstraint *RB_constraint_new_hinge(float pivot[3],
  201. float orn[4],
  202. rbRigidBody *rb1,
  203. rbRigidBody *rb2);
  204. rbConstraint *RB_constraint_new_slider(float pivot[3],
  205. float orn[4],
  206. rbRigidBody *rb1,
  207. rbRigidBody *rb2);
  208. rbConstraint *RB_constraint_new_piston(float pivot[3],
  209. float orn[4],
  210. rbRigidBody *rb1,
  211. rbRigidBody *rb2);
  212. rbConstraint *RB_constraint_new_6dof(float pivot[3],
  213. float orn[4],
  214. rbRigidBody *rb1,
  215. rbRigidBody *rb2);
  216. rbConstraint *RB_constraint_new_6dof_spring(float pivot[3],
  217. float orn[4],
  218. rbRigidBody *rb1,
  219. rbRigidBody *rb2);
  220. rbConstraint *RB_constraint_new_6dof_spring2(float pivot[3],
  221. float orn[4],
  222. rbRigidBody *rb1,
  223. rbRigidBody *rb2);
  224. rbConstraint *RB_constraint_new_motor(float pivot[3],
  225. float orn[4],
  226. rbRigidBody *rb1,
  227. rbRigidBody *rb2);
  228. /* ............ */
  229. /* Cleanup --------------------------- */
  230. void RB_constraint_delete(rbConstraint *con);
  231. /* Settings --------------------------- */
  232. /* Enable or disable constraint */
  233. void RB_constraint_set_enabled(rbConstraint *con, int enabled);
  234. /* Limits */
  235. #define RB_LIMIT_LIN_X 0
  236. #define RB_LIMIT_LIN_Y 1
  237. #define RB_LIMIT_LIN_Z 2
  238. #define RB_LIMIT_ANG_X 3
  239. #define RB_LIMIT_ANG_Y 4
  240. #define RB_LIMIT_ANG_Z 5
  241. /* Bullet uses the following convention:
  242. * - lower limit == upper limit -> axis is locked
  243. * - lower limit > upper limit -> axis is free
  244. * - lower limit < upper limit -> axis is limited in given range
  245. */
  246. void RB_constraint_set_limits_hinge(rbConstraint *con, float lower, float upper);
  247. void RB_constraint_set_limits_slider(rbConstraint *con, float lower, float upper);
  248. void RB_constraint_set_limits_piston(
  249. rbConstraint *con, float lin_lower, float lin_upper, float ang_lower, float ang_upper);
  250. void RB_constraint_set_limits_6dof(rbConstraint *con, int axis, float lower, float upper);
  251. /* 6dof spring specific */
  252. void RB_constraint_set_stiffness_6dof_spring(rbConstraint *con, int axis, float stiffness);
  253. void RB_constraint_set_damping_6dof_spring(rbConstraint *con, int axis, float damping);
  254. void RB_constraint_set_spring_6dof_spring(rbConstraint *con, int axis, int enable);
  255. void RB_constraint_set_equilibrium_6dof_spring(rbConstraint *con);
  256. /* 6dof spring 2 specific */
  257. void RB_constraint_set_limits_6dof_spring2(rbConstraint *con, int axis, float lower, float upper);
  258. void RB_constraint_set_stiffness_6dof_spring2(rbConstraint *con, int axis, float stiffness);
  259. void RB_constraint_set_damping_6dof_spring2(rbConstraint *con, int axis, float damping);
  260. void RB_constraint_set_spring_6dof_spring2(rbConstraint *con, int axis, int enable);
  261. void RB_constraint_set_equilibrium_6dof_spring2(rbConstraint *con);
  262. /* motors */
  263. void RB_constraint_set_enable_motor(rbConstraint *con, int enable_lin, int enable_ang);
  264. void RB_constraint_set_max_impulse_motor(rbConstraint *con,
  265. float max_impulse_lin,
  266. float max_impulse_ang);
  267. void RB_constraint_set_target_velocity_motor(rbConstraint *con,
  268. float velocity_lin,
  269. float velocity_ang);
  270. /* Set number of constraint solver iterations made per step, this overrided world setting
  271. * To use default set it to -1 */
  272. void RB_constraint_set_solver_iterations(rbConstraint *con, int num_solver_iterations);
  273. /* Set breaking impulse threshold, if constraint shouldn't break it can be set to FLT_MAX */
  274. void RB_constraint_set_breaking_threshold(rbConstraint *con, float threshold);
  275. /* ********************************** */
  276. #ifdef __cplusplus
  277. }
  278. #endif
  279. #endif /* __RB_API_H__ */