2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2013 Blender Foundation,
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Joshua Leung, Sergej Reich
25 * ***** END GPL LICENSE BLOCK *****
30 * \brief Rigid Body API for interfacing with external Physics Engines
41 * Currently, this API is optimised for Bullet RigidBodies, and doesn't
42 * take into account other Physics Engines. Some tweaking may be necessary
43 * to allow other systems to be used, in particular there may be references
44 * to datatypes that aren't used here...
46 * -- Joshua Leung (22 June 2010)
49 /* ********************************** */
50 /* Partial Type Defines - Aliases for the type of data we store */
55 typedef struct rbDynamicsWorld rbDynamicsWorld;
58 typedef struct rbRigidBody rbRigidBody;
61 typedef struct rbCollisionShape rbCollisionShape;
63 /* Mesh Data (for Collision Shapes of Meshes) */
64 typedef struct rbMeshData rbMeshData;
67 typedef struct rbConstraint rbConstraint;
69 /* ********************************** */
70 /* Dynamics World Methods */
72 /* Setup ---------------------------- */
74 /* Create a new dynamics world instance */
75 // TODO: add args to set the type of constraint solvers, etc.
76 extern rbDynamicsWorld *RB_dworld_new(const float gravity[3]);
78 /* Delete the given dynamics world, and free any extra data it may require */
79 extern void RB_dworld_delete(rbDynamicsWorld *world);
81 /* Settings ------------------------- */
84 extern void RB_dworld_get_gravity(rbDynamicsWorld *world, float g_out[3]);
85 extern void RB_dworld_set_gravity(rbDynamicsWorld *world, const float g_in[3]);
87 /* Constraint Solver */
88 extern void RB_dworld_set_solver_iterations(rbDynamicsWorld *world, int num_solver_iterations);
90 extern void RB_dworld_set_split_impulse(rbDynamicsWorld *world, int split_impulse);
92 /* Simulation ----------------------- */
94 /* Step the simulation by the desired amount (in seconds) with extra controls on substep sizes and maximum substeps */
95 extern void RB_dworld_step_simulation(rbDynamicsWorld *world, float timeStep, int maxSubSteps, float timeSubStep);
97 /* Export -------------------------- */
99 /* Exports the dynamics world to physics simulator's serialisation format */
100 void RB_dworld_export(rbDynamicsWorld *world, const char *filename);
102 /* ********************************** */
103 /* Rigid Body Methods */
105 /* Setup ---------------------------- */
107 /* Add RigidBody to dynamics world */
108 extern void RB_dworld_add_body(rbDynamicsWorld *world, rbRigidBody *body, int col_groups);
110 /* Remove RigidBody from dynamics world */
111 extern void RB_dworld_remove_body(rbDynamicsWorld *world, rbRigidBody *body);
115 /* Create new RigidBody instance */
116 extern rbRigidBody *RB_body_new(rbCollisionShape *shape, const float loc[3], const float rot[4]);
118 /* Delete the given RigidBody instance */
119 extern void RB_body_delete(rbRigidBody *body);
121 /* Settings ------------------------- */
124 extern void RB_body_set_type(rbRigidBody *body, int type, float mass);
128 /* Collision Shape */
129 extern void RB_body_set_collision_shape(rbRigidBody *body, rbCollisionShape *shape);
134 extern float RB_body_get_mass(rbRigidBody *body);
135 extern void RB_body_set_mass(rbRigidBody *body, float value);
138 extern float RB_body_get_friction(rbRigidBody *body);
139 extern void RB_body_set_friction(rbRigidBody *body, float value);
142 extern float RB_body_get_restitution(rbRigidBody *body);
143 extern void RB_body_set_restitution(rbRigidBody *body, float value);
146 extern float RB_body_get_linear_damping(rbRigidBody *body);
147 extern void RB_body_set_linear_damping(rbRigidBody *body, float value);
149 extern float RB_body_get_angular_damping(rbRigidBody *body);
150 extern void RB_body_set_angular_damping(rbRigidBody *body, float value);
152 extern void RB_body_set_damping(rbRigidBody *object, float linear, float angular);
154 /* Sleeping Thresholds */
155 extern float RB_body_get_linear_sleep_thresh(rbRigidBody *body);
156 extern void RB_body_set_linear_sleep_thresh(rbRigidBody *body, float value);
158 extern float RB_body_get_angular_sleep_thresh(rbRigidBody *body);
159 extern void RB_body_set_angular_sleep_thresh(rbRigidBody *body, float value);
161 extern void RB_body_set_sleep_thresh(rbRigidBody *body, float linear, float angular);
163 /* Linear Velocity */
164 extern void RB_body_get_linear_velocity(rbRigidBody *body, float v_out[3]);
165 extern void RB_body_set_linear_velocity(rbRigidBody *body, const float v_in[3]);
167 /* Angular Velocity */
168 extern void RB_body_get_angular_velocity(rbRigidBody *body, float v_out[3]);
169 extern void RB_body_set_angular_velocity(rbRigidBody *body, const float v_in[3]);
171 /* Linear/Angular Factor, used to lock translation/roation axes */
172 extern void RB_body_set_linear_factor(rbRigidBody *object, float x, float y, float z);
173 extern void RB_body_set_angular_factor(rbRigidBody *object, float x, float y, float z);
175 /* Kinematic State */
176 extern void RB_body_set_kinematic_state(rbRigidBody *body, int kinematic);
178 /* RigidBody Interface - Rigid Body Activation States */
179 extern int RB_body_get_activation_state(rbRigidBody *body);
180 extern void RB_body_set_activation_state(rbRigidBody *body, int use_deactivation);
181 extern void RB_body_activate(rbRigidBody *body);
182 extern void RB_body_deactivate(rbRigidBody *body);
185 /* Simulation ----------------------- */
187 /* Get current transform matrix of RigidBody to use in Blender (OpenGL format) */
188 extern void RB_body_get_transform_matrix(rbRigidBody *body, float m_out[4][4]);
190 /* Set RigidBody's location and rotation */
191 extern void RB_body_set_loc_rot(rbRigidBody *body, const float loc[3], const float rot[4]);
192 /* Set RigidBody's local scaling */
193 extern void RB_body_set_scale(rbRigidBody *body, const float scale[3]);
197 /* Get RigidBody's position as vector */
198 void RB_body_get_position(rbRigidBody *body, float v_out[3]);
199 /* Get RigidBody's orientation as quaternion */
200 void RB_body_get_orientation(rbRigidBody *body, float v_out[4]);
204 extern void RB_body_apply_central_force(rbRigidBody *body, const float v_in[3]);
206 /* ********************************** */
207 /* Collision Shape Methods */
209 /* Setup (Standard Shapes) ----------- */
211 extern rbCollisionShape *RB_shape_new_box(float x, float y, float z);
212 extern rbCollisionShape *RB_shape_new_sphere(float radius);
213 extern rbCollisionShape *RB_shape_new_capsule(float radius, float height);
214 extern rbCollisionShape *RB_shape_new_cone(float radius, float height);
215 extern rbCollisionShape *RB_shape_new_cylinder(float radius, float height);
217 /* Setup (Convex Hull) ------------ */
219 extern rbCollisionShape *RB_shape_new_convex_hull(float *verts, int stride, int count, float margin, bool *can_embed);
221 /* Setup (Triangle Mesh) ---------- */
224 extern rbMeshData *RB_trimesh_data_new(void);
225 extern void RB_trimesh_add_triangle(rbMeshData *mesh, const float v1[3], const float v2[3], const float v3[3]);
226 /* 2a - Triangle Meshes */
227 extern rbCollisionShape *RB_shape_new_trimesh(rbMeshData *mesh);
228 /* 2b - GImpact Meshes */
229 extern rbCollisionShape *RB_shape_new_gimpact_mesh(rbMeshData *mesh);
232 /* Cleanup --------------------------- */
234 extern void RB_shape_delete(rbCollisionShape *shape);
236 /* Settings --------------------------- */
238 /* Collision Margin */
239 extern float RB_shape_get_margin(rbCollisionShape *shape);
240 extern void RB_shape_set_margin(rbCollisionShape *shape, float value);
242 /* ********************************** */
245 /* Setup ----------------------------- */
247 /* Add Rigid Body Constraint to simulation world */
248 extern void RB_dworld_add_constraint(rbDynamicsWorld *world, rbConstraint *con, int disable_collisions);
250 /* Remove Rigid Body Constraint from simulation world */
251 extern void RB_dworld_remove_constraint(rbDynamicsWorld *world, rbConstraint *con);
253 extern rbConstraint *RB_constraint_new_point(float pivot[3], rbRigidBody *rb1, rbRigidBody *rb2);
254 extern rbConstraint *RB_constraint_new_fixed(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2);
255 extern rbConstraint *RB_constraint_new_hinge(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2);
256 extern rbConstraint *RB_constraint_new_slider(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2);
257 extern rbConstraint *RB_constraint_new_piston(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2);
258 extern rbConstraint *RB_constraint_new_6dof(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2);
259 extern rbConstraint *RB_constraint_new_6dof_spring(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2);
263 /* Cleanup --------------------------- */
265 extern void RB_constraint_delete(rbConstraint *con);
267 /* Settings --------------------------- */
269 /* Enable or disable constraint */
270 extern void RB_constraint_set_enabled(rbConstraint *con, int enabled);
273 #define RB_LIMIT_LIN_X 0
274 #define RB_LIMIT_LIN_Y 1
275 #define RB_LIMIT_LIN_Z 2
276 #define RB_LIMIT_ANG_X 3
277 #define RB_LIMIT_ANG_Y 4
278 #define RB_LIMIT_ANG_Z 5
279 /* Bullet uses the following convention:
280 * - lower limit == upper limit -> axis is locked
281 * - lower limit > upper limit -> axis is free
282 * - lower limit < upper limit -> axis is limited in given range
284 extern void RB_constraint_set_limits_hinge(rbConstraint *con, float lower, float upper);
285 extern void RB_constraint_set_limits_slider(rbConstraint *con, float lower, float upper);
286 extern void RB_constraint_set_limits_piston(rbConstraint *con, float lin_lower, float lin_upper, float ang_lower, float ang_upper);
287 extern void RB_constraint_set_limits_6dof(rbConstraint *con, int axis, float lower, float upper);
289 /* 6dof spring specific */
290 extern void RB_constraint_set_stiffness_6dof_spring(rbConstraint *con, int axis, float stiffness);
291 extern void RB_constraint_set_damping_6dof_spring(rbConstraint *con, int axis, float damping);
292 extern void RB_constraint_set_spring_6dof_spring(rbConstraint *con, int axis, int enable);
293 extern void RB_constraint_set_equilibrium_6dof_spring(rbConstraint *con);
295 /* Set number of constraint solver iterations made per step, this overrided world setting
296 * To use default set it to -1 */
297 extern void RB_constraint_set_solver_iterations(rbConstraint *con, int num_solver_iterations);
299 /* Set breaking impulse threshold, if constraint shouldn't break it can be set to FLT_MAX */
300 extern void RB_constraint_set_breaking_threshold(rbConstraint *con, float threshold);
302 /* ********************************** */
308 #endif /* __RB_API_H__ */