Cleanup: remove redundant, invalid info from headers
[blender.git] / source / blender / blenkernel / intern / rigidbody.c
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
20 /** \file rigidbody.c
21  *  \ingroup blenkernel
22  *  \brief Blender-side interface and methods for dealing with Rigid Body simulations
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stddef.h>
28 #include <float.h>
29 #include <math.h>
30 #include <limits.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_math.h"
36
37 #ifdef WITH_BULLET
38 #  include "RBI_api.h"
39 #endif
40
41 #include "DNA_ID.h"
42 #include "DNA_group_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_object_force_types.h"
46 #include "DNA_rigidbody_types.h"
47 #include "DNA_scene_types.h"
48
49 #include "BKE_cdderivedmesh.h"
50 #include "BKE_depsgraph.h"
51 #include "BKE_effect.h"
52 #include "BKE_global.h"
53 #include "BKE_library.h"
54 #include "BKE_library_query.h"
55 #include "BKE_mesh.h"
56 #include "BKE_object.h"
57 #include "BKE_pointcache.h"
58 #include "BKE_rigidbody.h"
59 #include "BKE_scene.h"
60
61 #include "DEG_depsgraph.h"
62
63 /* ************************************** */
64 /* Memory Management */
65
66 /* Freeing Methods --------------------- */
67
68 #ifndef WITH_BULLET
69
70 static void RB_dworld_remove_constraint(void *UNUSED(world), void *UNUSED(con)) {}
71 static void RB_dworld_remove_body(void *UNUSED(world), void *UNUSED(body)) {}
72 static void RB_dworld_delete(void *UNUSED(world)) {}
73 static void RB_body_delete(void *UNUSED(body)) {}
74 static void RB_shape_delete(void *UNUSED(shape)) {}
75 static void RB_constraint_delete(void *UNUSED(con)) {}
76
77 #endif
78
79 /* Free rigidbody world */
80 void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
81 {
82         /* sanity check */
83         if (!rbw)
84                 return;
85
86         if (rbw->physics_world) {
87                 /* free physics references, we assume that all physics objects in will have been added to the world */
88                 GroupObject *go;
89                 if (rbw->constraints) {
90                         for (go = rbw->constraints->gobject.first; go; go = go->next) {
91                                 if (go->ob && go->ob->rigidbody_constraint) {
92                                         RigidBodyCon *rbc = go->ob->rigidbody_constraint;
93
94                                         if (rbc->physics_constraint)
95                                                 RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
96                                 }
97                         }
98                 }
99                 if (rbw->group) {
100                         for (go = rbw->group->gobject.first; go; go = go->next) {
101                                 if (go->ob && go->ob->rigidbody_object) {
102                                         RigidBodyOb *rbo = go->ob->rigidbody_object;
103
104                                         if (rbo->physics_object)
105                                                 RB_dworld_remove_body(rbw->physics_world, rbo->physics_object);
106                                 }
107                         }
108                 }
109                 /* free dynamics world */
110                 RB_dworld_delete(rbw->physics_world);
111         }
112         if (rbw->objects)
113                 free(rbw->objects);
114
115         /* free cache */
116         BKE_ptcache_free_list(&(rbw->ptcaches));
117         rbw->pointcache = NULL;
118
119         /* free effector weights */
120         if (rbw->effector_weights)
121                 MEM_freeN(rbw->effector_weights);
122
123         /* free rigidbody world itself */
124         MEM_freeN(rbw);
125 }
126
127 /* Free RigidBody settings and sim instances */
128 void BKE_rigidbody_free_object(Object *ob)
129 {
130         RigidBodyOb *rbo = (ob) ? ob->rigidbody_object : NULL;
131
132         /* sanity check */
133         if (rbo == NULL)
134                 return;
135
136         /* free physics references */
137         if (rbo->physics_object) {
138                 RB_body_delete(rbo->physics_object);
139                 rbo->physics_object = NULL;
140         }
141
142         if (rbo->physics_shape) {
143                 RB_shape_delete(rbo->physics_shape);
144                 rbo->physics_shape = NULL;
145         }
146
147         /* free data itself */
148         MEM_freeN(rbo);
149         ob->rigidbody_object = NULL;
150 }
151
152 /* Free RigidBody constraint and sim instance */
153 void BKE_rigidbody_free_constraint(Object *ob)
154 {
155         RigidBodyCon *rbc = (ob) ? ob->rigidbody_constraint : NULL;
156
157         /* sanity check */
158         if (rbc == NULL)
159                 return;
160
161         /* free physics reference */
162         if (rbc->physics_constraint) {
163                 RB_constraint_delete(rbc->physics_constraint);
164                 rbc->physics_constraint = NULL;
165         }
166
167         /* free data itself */
168         MEM_freeN(rbc);
169         ob->rigidbody_constraint = NULL;
170 }
171
172 #ifdef WITH_BULLET
173
174 /* Copying Methods --------------------- */
175
176 /* These just copy the data, clearing out references to physics objects.
177  * Anything that uses them MUST verify that the copied object will
178  * be added to relevant groups later...
179  */
180
181 RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob, const int UNUSED(flag))
182 {
183         RigidBodyOb *rboN = NULL;
184
185         if (ob->rigidbody_object) {
186                 /* just duplicate the whole struct first (to catch all the settings) */
187                 rboN = MEM_dupallocN(ob->rigidbody_object);
188
189                 /* tag object as needing to be verified */
190                 rboN->flag |= RBO_FLAG_NEEDS_VALIDATE;
191
192                 /* clear out all the fields which need to be revalidated later */
193                 rboN->physics_object = NULL;
194                 rboN->physics_shape = NULL;
195         }
196
197         /* return new copy of settings */
198         return rboN;
199 }
200
201 RigidBodyCon *BKE_rigidbody_copy_constraint(const Object *ob, const int UNUSED(flag))
202 {
203         RigidBodyCon *rbcN = NULL;
204
205         if (ob->rigidbody_constraint) {
206                 /* just duplicate the whole struct first (to catch all the settings) */
207                 rbcN = MEM_dupallocN(ob->rigidbody_constraint);
208
209                 /* tag object as needing to be verified */
210                 rbcN->flag |= RBC_FLAG_NEEDS_VALIDATE;
211
212                 /* clear out all the fields which need to be revalidated later */
213                 rbcN->physics_constraint = NULL;
214         }
215
216         /* return new copy of settings */
217         return rbcN;
218 }
219
220 /* ************************************** */
221 /* Setup Utilities - Validate Sim Instances */
222
223 /* get the appropriate DerivedMesh based on rigid body mesh source */
224 static DerivedMesh *rigidbody_get_mesh(Object *ob)
225 {
226         if (ob->rigidbody_object->mesh_source == RBO_MESH_DEFORM) {
227                 return ob->derivedDeform;
228         }
229         else if (ob->rigidbody_object->mesh_source == RBO_MESH_FINAL) {
230                 return ob->derivedFinal;
231         }
232         else {
233                 return CDDM_from_mesh(ob->data);
234         }
235 }
236
237 /* create collision shape of mesh - convex hull */
238 static rbCollisionShape *rigidbody_get_shape_convexhull_from_mesh(Object *ob, float margin, bool *can_embed)
239 {
240         rbCollisionShape *shape = NULL;
241         DerivedMesh *dm = NULL;
242         MVert *mvert = NULL;
243         int totvert = 0;
244
245         if (ob->type == OB_MESH && ob->data) {
246                 dm = rigidbody_get_mesh(ob);
247                 mvert   = (dm) ? dm->getVertArray(dm) : NULL;
248                 totvert = (dm) ? dm->getNumVerts(dm) : 0;
249         }
250         else {
251                 printf("ERROR: cannot make Convex Hull collision shape for non-Mesh object\n");
252         }
253
254         if (totvert) {
255                 shape = RB_shape_new_convex_hull((float *)mvert, sizeof(MVert), totvert, margin, can_embed);
256         }
257         else {
258                 printf("ERROR: no vertices to define Convex Hull collision shape with\n");
259         }
260
261         if (dm && ob->rigidbody_object->mesh_source == RBO_MESH_BASE)
262                 dm->release(dm);
263
264         return shape;
265 }
266
267 /* create collision shape of mesh - triangulated mesh
268  * returns NULL if creation fails.
269  */
270 static rbCollisionShape *rigidbody_get_shape_trimesh_from_mesh(Object *ob)
271 {
272         rbCollisionShape *shape = NULL;
273
274         if (ob->type == OB_MESH) {
275                 DerivedMesh *dm = NULL;
276                 MVert *mvert;
277                 const MLoopTri *looptri;
278                 int totvert;
279                 int tottri;
280                 const MLoop *mloop;
281
282                 dm = rigidbody_get_mesh(ob);
283
284                 /* ensure mesh validity, then grab data */
285                 if (dm == NULL)
286                         return NULL;
287
288                 mvert   = dm->getVertArray(dm);
289                 totvert = dm->getNumVerts(dm);
290                 looptri = dm->getLoopTriArray(dm);
291                 tottri = dm->getNumLoopTri(dm);
292                 mloop = dm->getLoopArray(dm);
293
294                 /* sanity checking - potential case when no data will be present */
295                 if ((totvert == 0) || (tottri == 0)) {
296                         printf("WARNING: no geometry data converted for Mesh Collision Shape (ob = %s)\n", ob->id.name + 2);
297                 }
298                 else {
299                         rbMeshData *mdata;
300                         int i;
301
302                         /* init mesh data for collision shape */
303                         mdata = RB_trimesh_data_new(tottri, totvert);
304
305                         RB_trimesh_add_vertices(mdata, (float *)mvert, totvert, sizeof(MVert));
306
307                         /* loop over all faces, adding them as triangles to the collision shape
308                          * (so for some faces, more than triangle will get added)
309                          */
310                         if (mvert && looptri) {
311                                 for (i = 0; i < tottri; i++) {
312                                         /* add first triangle - verts 1,2,3 */
313                                         const MLoopTri *lt = &looptri[i];
314                                         int vtri[3];
315
316                                         vtri[0] = mloop[lt->tri[0]].v;
317                                         vtri[1] = mloop[lt->tri[1]].v;
318                                         vtri[2] = mloop[lt->tri[2]].v;
319
320                                         RB_trimesh_add_triangle_indices(mdata, i, UNPACK3(vtri));
321                                 }
322                         }
323
324                         RB_trimesh_finish(mdata);
325
326                         /* construct collision shape
327                          *
328                          * These have been chosen to get better speed/accuracy tradeoffs with regards
329                          * to limitations of each:
330                          *    - BVH-Triangle Mesh: for passive objects only. Despite having greater
331                          *                         speed/accuracy, they cannot be used for moving objects.
332                          *    - GImpact Mesh:      for active objects. These are slower and less stable,
333                          *                         but are more flexible for general usage.
334                          */
335                         if (ob->rigidbody_object->type == RBO_TYPE_PASSIVE) {
336                                 shape = RB_shape_new_trimesh(mdata);
337                         }
338                         else {
339                                 shape = RB_shape_new_gimpact_mesh(mdata);
340                         }
341                 }
342
343                 /* cleanup temp data */
344                 if (ob->rigidbody_object->mesh_source == RBO_MESH_BASE) {
345                         dm->release(dm);
346                 }
347         }
348         else {
349                 printf("ERROR: cannot make Triangular Mesh collision shape for non-Mesh object\n");
350         }
351
352         return shape;
353 }
354
355 /* Create new physics sim collision shape for object and store it,
356  * or remove the existing one first and replace...
357  */
358 static void rigidbody_validate_sim_shape(Object *ob, bool rebuild)
359 {
360         RigidBodyOb *rbo = ob->rigidbody_object;
361         rbCollisionShape *new_shape = NULL;
362         BoundBox *bb = NULL;
363         float size[3] = {1.0f, 1.0f, 1.0f};
364         float radius = 1.0f;
365         float height = 1.0f;
366         float capsule_height;
367         float hull_margin = 0.0f;
368         bool can_embed = true;
369         bool has_volume;
370
371         /* sanity check */
372         if (rbo == NULL)
373                 return;
374
375         /* don't create a new shape if we already have one and don't want to rebuild it */
376         if (rbo->physics_shape && !rebuild)
377                 return;
378
379         /* if automatically determining dimensions, use the Object's boundbox
380          * - assume that all quadrics are standing upright on local z-axis
381          * - assume even distribution of mass around the Object's pivot
382          *   (i.e. Object pivot is centralized in boundbox)
383          */
384         // XXX: all dimensions are auto-determined now... later can add stored settings for this
385         /* get object dimensions without scaling */
386         bb = BKE_object_boundbox_get(ob);
387         if (bb) {
388                 size[0] = (bb->vec[4][0] - bb->vec[0][0]);
389                 size[1] = (bb->vec[2][1] - bb->vec[0][1]);
390                 size[2] = (bb->vec[1][2] - bb->vec[0][2]);
391         }
392         mul_v3_fl(size, 0.5f);
393
394         if (ELEM(rbo->shape, RB_SHAPE_CAPSULE, RB_SHAPE_CYLINDER, RB_SHAPE_CONE)) {
395                 /* take radius as largest x/y dimension, and height as z-dimension */
396                 radius = MAX2(size[0], size[1]);
397                 height = size[2];
398         }
399         else if (rbo->shape == RB_SHAPE_SPHERE) {
400                 /* take radius to the largest dimension to try and encompass everything */
401                 radius = MAX3(size[0], size[1], size[2]);
402         }
403
404         /* create new shape */
405         switch (rbo->shape) {
406                 case RB_SHAPE_BOX:
407                         new_shape = RB_shape_new_box(size[0], size[1], size[2]);
408                         break;
409
410                 case RB_SHAPE_SPHERE:
411                         new_shape = RB_shape_new_sphere(radius);
412                         break;
413
414                 case RB_SHAPE_CAPSULE:
415                         capsule_height = (height - radius) * 2.0f;
416                         new_shape = RB_shape_new_capsule(radius, (capsule_height > 0.0f) ? capsule_height : 0.0f);
417                         break;
418                 case RB_SHAPE_CYLINDER:
419                         new_shape = RB_shape_new_cylinder(radius, height);
420                         break;
421                 case RB_SHAPE_CONE:
422                         new_shape = RB_shape_new_cone(radius, height * 2.0f);
423                         break;
424
425                 case RB_SHAPE_CONVEXH:
426                         /* try to emged collision margin */
427                         has_volume = (MIN3(size[0], size[1], size[2]) > 0.0f);
428
429                         if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && has_volume)
430                                 hull_margin = 0.04f;
431                         new_shape = rigidbody_get_shape_convexhull_from_mesh(ob, hull_margin, &can_embed);
432                         if (!(rbo->flag & RBO_FLAG_USE_MARGIN))
433                                 rbo->margin = (can_embed && has_volume) ? 0.04f : 0.0f;  /* RB_TODO ideally we shouldn't directly change the margin here */
434                         break;
435                 case RB_SHAPE_TRIMESH:
436                         new_shape = rigidbody_get_shape_trimesh_from_mesh(ob);
437                         break;
438         }
439         /* use box shape if we can't fall back to old shape */
440         if (new_shape == NULL && rbo->physics_shape == NULL) {
441                 new_shape = RB_shape_new_box(size[0], size[1], size[2]);
442         }
443         /* assign new collision shape if creation was successful */
444         if (new_shape) {
445                 if (rbo->physics_shape)
446                         RB_shape_delete(rbo->physics_shape);
447                 rbo->physics_shape = new_shape;
448                 RB_shape_set_margin(rbo->physics_shape, RBO_GET_MARGIN(rbo));
449         }
450 }
451
452 /* --------------------- */
453
454 /* helper function to calculate volume of rigidbody object */
455 // TODO: allow a parameter to specify method used to calculate this?
456 void BKE_rigidbody_calc_volume(Object *ob, float *r_vol)
457 {
458         RigidBodyOb *rbo = ob->rigidbody_object;
459
460         float size[3]  = {1.0f, 1.0f, 1.0f};
461         float radius = 1.0f;
462         float height = 1.0f;
463
464         float volume = 0.0f;
465
466         /* if automatically determining dimensions, use the Object's boundbox
467          * - assume that all quadrics are standing upright on local z-axis
468          * - assume even distribution of mass around the Object's pivot
469          *   (i.e. Object pivot is centralized in boundbox)
470          * - boundbox gives full width
471          */
472         // XXX: all dimensions are auto-determined now... later can add stored settings for this
473         BKE_object_dimensions_get(ob, size);
474
475         if (ELEM(rbo->shape, RB_SHAPE_CAPSULE, RB_SHAPE_CYLINDER, RB_SHAPE_CONE)) {
476                 /* take radius as largest x/y dimension, and height as z-dimension */
477                 radius = MAX2(size[0], size[1]) * 0.5f;
478                 height = size[2];
479         }
480         else if (rbo->shape == RB_SHAPE_SPHERE) {
481                 /* take radius to the largest dimension to try and encompass everything */
482                 radius = max_fff(size[0], size[1], size[2]) * 0.5f;
483         }
484
485         /* calculate volume as appropriate  */
486         switch (rbo->shape) {
487                 case RB_SHAPE_BOX:
488                         volume = size[0] * size[1] * size[2];
489                         break;
490
491                 case RB_SHAPE_SPHERE:
492                         volume = 4.0f / 3.0f * (float)M_PI * radius * radius * radius;
493                         break;
494
495                 /* for now, assume that capsule is close enough to a cylinder... */
496                 case RB_SHAPE_CAPSULE:
497                 case RB_SHAPE_CYLINDER:
498                         volume = (float)M_PI * radius * radius * height;
499                         break;
500
501                 case RB_SHAPE_CONE:
502                         volume = (float)M_PI / 3.0f * radius * radius * height;
503                         break;
504
505                 case RB_SHAPE_CONVEXH:
506                 case RB_SHAPE_TRIMESH:
507                 {
508                         if (ob->type == OB_MESH) {
509                                 DerivedMesh *dm = rigidbody_get_mesh(ob);
510                                 MVert *mvert;
511                                 const MLoopTri *lt = NULL;
512                                 int totvert, tottri = 0;
513                                 const MLoop *mloop = NULL;
514
515                                 /* ensure mesh validity, then grab data */
516                                 if (dm == NULL)
517                                         return;
518
519                                 mvert   = dm->getVertArray(dm);
520                                 totvert = dm->getNumVerts(dm);
521                                 lt = dm->getLoopTriArray(dm);
522                                 tottri = dm->getNumLoopTri(dm);
523                                 mloop = dm->getLoopArray(dm);
524
525                                 if (totvert > 0 && tottri > 0) {
526                                         BKE_mesh_calc_volume(mvert, totvert, lt, tottri, mloop, &volume, NULL);
527                                 }
528
529                                 /* cleanup temp data */
530                                 if (ob->rigidbody_object->mesh_source == RBO_MESH_BASE) {
531                                         dm->release(dm);
532                                 }
533                         }
534                         else {
535                                 /* rough estimate from boundbox as fallback */
536                                 /* XXX could implement other types of geometry here (curves, etc.) */
537                                 volume = size[0] * size[1] * size[2];
538                         }
539                         break;
540                 }
541
542 #if 0 // XXX: not defined yet
543                 case RB_SHAPE_COMPOUND:
544                         volume = 0.0f;
545                         break;
546 #endif
547         }
548
549         /* return the volume calculated */
550         if (r_vol) *r_vol = volume;
551 }
552
553 void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3])
554 {
555         RigidBodyOb *rbo = ob->rigidbody_object;
556
557         float size[3]  = {1.0f, 1.0f, 1.0f};
558         float height = 1.0f;
559
560         zero_v3(r_center);
561
562         /* if automatically determining dimensions, use the Object's boundbox
563          * - assume that all quadrics are standing upright on local z-axis
564          * - assume even distribution of mass around the Object's pivot
565          *   (i.e. Object pivot is centralized in boundbox)
566          * - boundbox gives full width
567          */
568         // XXX: all dimensions are auto-determined now... later can add stored settings for this
569         BKE_object_dimensions_get(ob, size);
570
571         /* calculate volume as appropriate  */
572         switch (rbo->shape) {
573                 case RB_SHAPE_BOX:
574                 case RB_SHAPE_SPHERE:
575                 case RB_SHAPE_CAPSULE:
576                 case RB_SHAPE_CYLINDER:
577                         break;
578
579                 case RB_SHAPE_CONE:
580                         /* take radius as largest x/y dimension, and height as z-dimension */
581                         height = size[2];
582                         /* cone is geometrically centered on the median,
583                          * center of mass is 1/4 up from the base
584                          */
585                         r_center[2] = -0.25f * height;
586                         break;
587
588                 case RB_SHAPE_CONVEXH:
589                 case RB_SHAPE_TRIMESH:
590                 {
591                         if (ob->type == OB_MESH) {
592                                 DerivedMesh *dm = rigidbody_get_mesh(ob);
593                                 MVert *mvert;
594                                 const MLoopTri *looptri;
595                                 int totvert, tottri;
596                                 const MLoop *mloop;
597
598                                 /* ensure mesh validity, then grab data */
599                                 if (dm == NULL)
600                                         return;
601
602                                 mvert   = dm->getVertArray(dm);
603                                 totvert = dm->getNumVerts(dm);
604                                 looptri = dm->getLoopTriArray(dm);
605                                 tottri = dm->getNumLoopTri(dm);
606                                 mloop = dm->getLoopArray(dm);
607
608                                 if (totvert > 0 && tottri > 0) {
609                                         BKE_mesh_calc_volume(mvert, totvert, looptri, tottri, mloop, NULL, r_center);
610                                 }
611
612                                 /* cleanup temp data */
613                                 if (ob->rigidbody_object->mesh_source == RBO_MESH_BASE) {
614                                         dm->release(dm);
615                                 }
616                         }
617                         break;
618                 }
619
620 #if 0 // XXX: not defined yet
621                 case RB_SHAPE_COMPOUND:
622                         volume = 0.0f;
623                         break;
624 #endif
625         }
626 }
627
628 /* --------------------- */
629
630 /**
631  * Create physics sim representation of object given RigidBody settings
632  *
633  * \param rebuild: Even if an instance already exists, replace it
634  */
635 static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool rebuild)
636 {
637         RigidBodyOb *rbo = (ob) ? ob->rigidbody_object : NULL;
638         float loc[3];
639         float rot[4];
640
641         /* sanity checks:
642          * - object doesn't have RigidBody info already: then why is it here?
643          */
644         if (rbo == NULL)
645                 return;
646
647         /* make sure collision shape exists */
648         /* FIXME we shouldn't always have to rebuild collision shapes when rebuilding objects, but it's needed for constraints to update correctly */
649         if (rbo->physics_shape == NULL || rebuild)
650                 rigidbody_validate_sim_shape(ob, true);
651
652         if (rbo->physics_object && rebuild == false) {
653                 RB_dworld_remove_body(rbw->physics_world, rbo->physics_object);
654         }
655         if (!rbo->physics_object || rebuild) {
656                 /* remove rigid body if it already exists before creating a new one */
657                 if (rbo->physics_object) {
658                         RB_body_delete(rbo->physics_object);
659                 }
660
661                 mat4_to_loc_quat(loc, rot, ob->obmat);
662
663                 rbo->physics_object = RB_body_new(rbo->physics_shape, loc, rot);
664
665                 RB_body_set_friction(rbo->physics_object, rbo->friction);
666                 RB_body_set_restitution(rbo->physics_object, rbo->restitution);
667
668                 RB_body_set_damping(rbo->physics_object, rbo->lin_damping, rbo->ang_damping);
669                 RB_body_set_sleep_thresh(rbo->physics_object, rbo->lin_sleep_thresh, rbo->ang_sleep_thresh);
670                 RB_body_set_activation_state(rbo->physics_object, rbo->flag & RBO_FLAG_USE_DEACTIVATION);
671
672                 if (rbo->type == RBO_TYPE_PASSIVE || rbo->flag & RBO_FLAG_START_DEACTIVATED)
673                         RB_body_deactivate(rbo->physics_object);
674
675
676                 RB_body_set_linear_factor(rbo->physics_object,
677                                           (ob->protectflag & OB_LOCK_LOCX) == 0,
678                                           (ob->protectflag & OB_LOCK_LOCY) == 0,
679                                           (ob->protectflag & OB_LOCK_LOCZ) == 0);
680                 RB_body_set_angular_factor(rbo->physics_object,
681                                            (ob->protectflag & OB_LOCK_ROTX) == 0,
682                                            (ob->protectflag & OB_LOCK_ROTY) == 0,
683                                            (ob->protectflag & OB_LOCK_ROTZ) == 0);
684
685                 RB_body_set_mass(rbo->physics_object, RBO_GET_MASS(rbo));
686                 RB_body_set_kinematic_state(rbo->physics_object, rbo->flag & RBO_FLAG_KINEMATIC || rbo->flag & RBO_FLAG_DISABLED);
687         }
688
689         if (rbw && rbw->physics_world)
690                 RB_dworld_add_body(rbw->physics_world, rbo->physics_object, rbo->col_groups);
691 }
692
693 /* --------------------- */
694
695 static void rigidbody_constraint_init_spring(
696         RigidBodyCon *rbc, void (*set_spring)(rbConstraint *, int, int),
697         void (*set_stiffness)(rbConstraint *, int, float), void (*set_damping)(rbConstraint *, int, float))
698 {
699         set_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->flag & RBC_FLAG_USE_SPRING_X);
700         set_stiffness(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->spring_stiffness_x);
701         set_damping(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->spring_damping_x);
702
703         set_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->flag & RBC_FLAG_USE_SPRING_Y);
704         set_stiffness(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->spring_stiffness_y);
705         set_damping(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->spring_damping_y);
706
707         set_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->flag & RBC_FLAG_USE_SPRING_Z);
708         set_stiffness(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->spring_stiffness_z);
709         set_damping(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->spring_damping_z);
710
711         set_spring(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->flag & RBC_FLAG_USE_SPRING_ANG_X);
712         set_stiffness(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->spring_stiffness_ang_x);
713         set_damping(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->spring_damping_ang_x);
714
715         set_spring(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->flag & RBC_FLAG_USE_SPRING_ANG_Y);
716         set_stiffness(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->spring_stiffness_ang_y);
717         set_damping(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->spring_damping_ang_y);
718
719         set_spring(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->flag & RBC_FLAG_USE_SPRING_ANG_Z);
720         set_stiffness(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->spring_stiffness_ang_z);
721         set_damping(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->spring_damping_ang_z);
722 }
723
724 static void rigidbody_constraint_set_limits(RigidBodyCon *rbc, void (*set_limits)(rbConstraint *, int, float, float))
725 {
726         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
727                 set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
728         else
729                 set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
730
731         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
732                 set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
733         else
734                 set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
735
736         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
737                 set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
738         else
739                 set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
740
741         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
742                 set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
743         else
744                 set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
745
746         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
747                 set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
748         else
749                 set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
750
751         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
752                 set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
753         else
754                 set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
755 }
756
757 /**
758  * Create physics sim representation of constraint given rigid body constraint settings
759  *
760  * \param rebuild: Even if an instance already exists, replace it
761  */
762 static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, bool rebuild)
763 {
764         RigidBodyCon *rbc = (ob) ? ob->rigidbody_constraint : NULL;
765         float loc[3];
766         float rot[4];
767         float lin_lower;
768         float lin_upper;
769         float ang_lower;
770         float ang_upper;
771
772         /* sanity checks:
773          * - object should have a rigid body constraint
774          * - rigid body constraint should have at least one constrained object
775          */
776         if (rbc == NULL) {
777                 return;
778         }
779
780         if (ELEM(NULL, rbc->ob1, rbc->ob1->rigidbody_object, rbc->ob2, rbc->ob2->rigidbody_object)) {
781                 if (rbc->physics_constraint) {
782                         RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
783                         RB_constraint_delete(rbc->physics_constraint);
784                         rbc->physics_constraint = NULL;
785                 }
786                 return;
787         }
788
789         if (rbc->physics_constraint && rebuild == false) {
790                 RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
791         }
792         if (rbc->physics_constraint == NULL || rebuild) {
793                 rbRigidBody *rb1 = rbc->ob1->rigidbody_object->physics_object;
794                 rbRigidBody *rb2 = rbc->ob2->rigidbody_object->physics_object;
795
796                 /* remove constraint if it already exists before creating a new one */
797                 if (rbc->physics_constraint) {
798                         RB_constraint_delete(rbc->physics_constraint);
799                         rbc->physics_constraint = NULL;
800                 }
801
802                 mat4_to_loc_quat(loc, rot, ob->obmat);
803
804                 if (rb1 && rb2) {
805                         switch (rbc->type) {
806                                 case RBC_TYPE_POINT:
807                                         rbc->physics_constraint = RB_constraint_new_point(loc, rb1, rb2);
808                                         break;
809                                 case RBC_TYPE_FIXED:
810                                         rbc->physics_constraint = RB_constraint_new_fixed(loc, rot, rb1, rb2);
811                                         break;
812                                 case RBC_TYPE_HINGE:
813                                         rbc->physics_constraint = RB_constraint_new_hinge(loc, rot, rb1, rb2);
814                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z) {
815                                                 RB_constraint_set_limits_hinge(rbc->physics_constraint, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
816                                         }
817                                         else
818                                                 RB_constraint_set_limits_hinge(rbc->physics_constraint, 0.0f, -1.0f);
819                                         break;
820                                 case RBC_TYPE_SLIDER:
821                                         rbc->physics_constraint = RB_constraint_new_slider(loc, rot, rb1, rb2);
822                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
823                                                 RB_constraint_set_limits_slider(rbc->physics_constraint, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
824                                         else
825                                                 RB_constraint_set_limits_slider(rbc->physics_constraint, 0.0f, -1.0f);
826                                         break;
827                                 case RBC_TYPE_PISTON:
828                                         rbc->physics_constraint = RB_constraint_new_piston(loc, rot, rb1, rb2);
829                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X) {
830                                                 lin_lower = rbc->limit_lin_x_lower;
831                                                 lin_upper = rbc->limit_lin_x_upper;
832                                         }
833                                         else {
834                                                 lin_lower = 0.0f;
835                                                 lin_upper = -1.0f;
836                                         }
837                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X) {
838                                                 ang_lower = rbc->limit_ang_x_lower;
839                                                 ang_upper = rbc->limit_ang_x_upper;
840                                         }
841                                         else {
842                                                 ang_lower = 0.0f;
843                                                 ang_upper = -1.0f;
844                                         }
845                                         RB_constraint_set_limits_piston(rbc->physics_constraint, lin_lower, lin_upper, ang_lower, ang_upper);
846                                         break;
847                                 case RBC_TYPE_6DOF_SPRING:
848                                         if (rbc->spring_type == RBC_SPRING_TYPE2) {
849                                                 rbc->physics_constraint = RB_constraint_new_6dof_spring2(loc, rot, rb1, rb2);
850
851                                                 rigidbody_constraint_init_spring(rbc, RB_constraint_set_spring_6dof_spring2, RB_constraint_set_stiffness_6dof_spring2, RB_constraint_set_damping_6dof_spring2);
852
853                                                 RB_constraint_set_equilibrium_6dof_spring2(rbc->physics_constraint);
854
855                                                 rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof_spring2);
856                                         }
857                                         else {
858                                                 rbc->physics_constraint = RB_constraint_new_6dof_spring(loc, rot, rb1, rb2);
859
860                                                 rigidbody_constraint_init_spring(rbc, RB_constraint_set_spring_6dof_spring, RB_constraint_set_stiffness_6dof_spring, RB_constraint_set_damping_6dof_spring);
861
862                                                 RB_constraint_set_equilibrium_6dof_spring(rbc->physics_constraint);
863
864                                                 rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof);
865                                         }
866                                         break;
867                                 case RBC_TYPE_6DOF:
868                                         rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
869
870                                         rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof);
871                                         break;
872                                 case RBC_TYPE_MOTOR:
873                                         rbc->physics_constraint = RB_constraint_new_motor(loc, rot, rb1, rb2);
874
875                                         RB_constraint_set_enable_motor(rbc->physics_constraint, rbc->flag & RBC_FLAG_USE_MOTOR_LIN, rbc->flag & RBC_FLAG_USE_MOTOR_ANG);
876                                         RB_constraint_set_max_impulse_motor(rbc->physics_constraint, rbc->motor_lin_max_impulse, rbc->motor_ang_max_impulse);
877                                         RB_constraint_set_target_velocity_motor(rbc->physics_constraint, rbc->motor_lin_target_velocity, rbc->motor_ang_target_velocity);
878                                         break;
879                         }
880                 }
881                 else { /* can't create constraint without both rigid bodies */
882                         return;
883                 }
884
885                 RB_constraint_set_enabled(rbc->physics_constraint, rbc->flag & RBC_FLAG_ENABLED);
886
887                 if (rbc->flag & RBC_FLAG_USE_BREAKING)
888                         RB_constraint_set_breaking_threshold(rbc->physics_constraint, rbc->breaking_threshold);
889                 else
890                         RB_constraint_set_breaking_threshold(rbc->physics_constraint, FLT_MAX);
891
892                 if (rbc->flag & RBC_FLAG_OVERRIDE_SOLVER_ITERATIONS)
893                         RB_constraint_set_solver_iterations(rbc->physics_constraint, rbc->num_solver_iterations);
894                 else
895                         RB_constraint_set_solver_iterations(rbc->physics_constraint, -1);
896         }
897
898         if (rbw && rbw->physics_world && rbc->physics_constraint) {
899                 RB_dworld_add_constraint(rbw->physics_world, rbc->physics_constraint, rbc->flag & RBC_FLAG_DISABLE_COLLISIONS);
900         }
901 }
902
903 /* --------------------- */
904
905 /* Create physics sim world given RigidBody world settings */
906 // NOTE: this does NOT update object references that the scene uses, in case those aren't ready yet!
907 void BKE_rigidbody_validate_sim_world(Scene *scene, RigidBodyWorld *rbw, bool rebuild)
908 {
909         /* sanity checks */
910         if (rbw == NULL)
911                 return;
912
913         /* create new sim world */
914         if (rebuild || rbw->physics_world == NULL) {
915                 if (rbw->physics_world)
916                         RB_dworld_delete(rbw->physics_world);
917                 rbw->physics_world = RB_dworld_new(scene->physics_settings.gravity);
918         }
919
920         RB_dworld_set_solver_iterations(rbw->physics_world, rbw->num_solver_iterations);
921         RB_dworld_set_split_impulse(rbw->physics_world, rbw->flag & RBW_FLAG_USE_SPLIT_IMPULSE);
922 }
923
924 /* ************************************** */
925 /* Setup Utilities - Create Settings Blocks */
926
927 /* Set up RigidBody world */
928 RigidBodyWorld *BKE_rigidbody_create_world(Scene *scene)
929 {
930         /* try to get whatever RigidBody world that might be representing this already */
931         RigidBodyWorld *rbw;
932
933         /* sanity checks
934          * - there must be a valid scene to add world to
935          * - there mustn't be a sim world using this group already
936          */
937         if (scene == NULL)
938                 return NULL;
939
940         /* create a new sim world */
941         rbw = MEM_callocN(sizeof(RigidBodyWorld), "RigidBodyWorld");
942
943         /* set default settings */
944         rbw->effector_weights = BKE_effector_add_weights(NULL);
945
946         rbw->ltime = PSFRA;
947
948         rbw->time_scale = 1.0f;
949
950         rbw->steps_per_second = 60; /* Bullet default (60 Hz) */
951         rbw->num_solver_iterations = 10; /* 10 is bullet default */
952
953         rbw->pointcache = BKE_ptcache_add(&(rbw->ptcaches));
954         rbw->pointcache->step = 1;
955
956         /* return this sim world */
957         return rbw;
958 }
959
960 RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw, const int flag)
961 {
962         RigidBodyWorld *rbw_copy = MEM_dupallocN(rbw);
963
964         if (rbw->effector_weights) {
965                 rbw_copy->effector_weights = MEM_dupallocN(rbw->effector_weights);
966         }
967         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
968                 id_us_plus((ID *)rbw_copy->group);
969                 id_us_plus((ID *)rbw_copy->constraints);
970         }
971
972         /* XXX Never copy caches here? */
973         rbw_copy->pointcache = BKE_ptcache_copy_list(&rbw_copy->ptcaches, &rbw->ptcaches, flag & ~LIB_ID_COPY_CACHES);
974
975         rbw_copy->objects = NULL;
976         rbw_copy->physics_world = NULL;
977         rbw_copy->numbodies = 0;
978
979         return rbw_copy;
980 }
981
982 void BKE_rigidbody_world_groups_relink(RigidBodyWorld *rbw)
983 {
984         ID_NEW_REMAP(rbw->group);
985         ID_NEW_REMAP(rbw->constraints);
986         ID_NEW_REMAP(rbw->effector_weights->group);
987 }
988
989 void BKE_rigidbody_world_id_loop(RigidBodyWorld *rbw, RigidbodyWorldIDFunc func, void *userdata)
990 {
991         func(rbw, (ID **)&rbw->group, userdata, IDWALK_CB_NOP);
992         func(rbw, (ID **)&rbw->constraints, userdata, IDWALK_CB_NOP);
993         func(rbw, (ID **)&rbw->effector_weights->group, userdata, IDWALK_CB_NOP);
994
995         if (rbw->objects) {
996                 int i;
997                 for (i = 0; i < rbw->numbodies; i++) {
998                         func(rbw, (ID **)&rbw->objects[i], userdata, IDWALK_CB_NOP);
999                 }
1000         }
1001 }
1002
1003 /* Add rigid body settings to the specified object */
1004 RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type)
1005 {
1006         RigidBodyOb *rbo;
1007         RigidBodyWorld *rbw = scene->rigidbody_world;
1008
1009         /* sanity checks
1010          * - rigidbody world must exist
1011          * - object must exist
1012          * - cannot add rigid body if it already exists
1013          */
1014         if (ob == NULL || (ob->rigidbody_object != NULL))
1015                 return NULL;
1016
1017         /* create new settings data, and link it up */
1018         rbo = MEM_callocN(sizeof(RigidBodyOb), "RigidBodyOb");
1019
1020         /* set default settings */
1021         rbo->type = type;
1022
1023         rbo->mass = 1.0f;
1024
1025         rbo->friction = 0.5f; /* best when non-zero. 0.5 is Bullet default */
1026         rbo->restitution = 0.0f; /* best when zero. 0.0 is Bullet default */
1027
1028         rbo->margin = 0.04f; /* 0.04 (in meters) is Bullet default */
1029
1030         rbo->lin_sleep_thresh = 0.4f; /* 0.4 is half of Bullet default */
1031         rbo->ang_sleep_thresh = 0.5f; /* 0.5 is half of Bullet default */
1032
1033         rbo->lin_damping = 0.04f; /* 0.04 is game engine default */
1034         rbo->ang_damping = 0.1f; /* 0.1 is game engine default */
1035
1036         rbo->col_groups = 1;
1037
1038         /* use triangle meshes for passive objects
1039          * use convex hulls for active objects since dynamic triangle meshes are very unstable
1040          */
1041         if (type == RBO_TYPE_ACTIVE)
1042                 rbo->shape = RB_SHAPE_CONVEXH;
1043         else
1044                 rbo->shape = RB_SHAPE_TRIMESH;
1045
1046         rbo->mesh_source = RBO_MESH_DEFORM;
1047
1048         /* set initial transform */
1049         mat4_to_loc_quat(rbo->pos, rbo->orn, ob->obmat);
1050
1051         /* flag cache as outdated */
1052         BKE_rigidbody_cache_reset(rbw);
1053
1054         /* return this object */
1055         return rbo;
1056 }
1057
1058 /* Add rigid body constraint to the specified object */
1059 RigidBodyCon *BKE_rigidbody_create_constraint(Scene *scene, Object *ob, short type)
1060 {
1061         RigidBodyCon *rbc;
1062         RigidBodyWorld *rbw = scene->rigidbody_world;
1063
1064         /* sanity checks
1065          * - rigidbody world must exist
1066          * - object must exist
1067          * - cannot add constraint if it already exists
1068          */
1069         if (ob == NULL || (ob->rigidbody_constraint != NULL))
1070                 return NULL;
1071
1072         /* create new settings data, and link it up */
1073         rbc = MEM_callocN(sizeof(RigidBodyCon), "RigidBodyCon");
1074
1075         /* set default settings */
1076         rbc->type = type;
1077
1078         rbc->ob1 = NULL;
1079         rbc->ob2 = NULL;
1080
1081         rbc->flag |= RBC_FLAG_ENABLED;
1082         rbc->flag |= RBC_FLAG_DISABLE_COLLISIONS;
1083
1084         rbc->spring_type = RBC_SPRING_TYPE2;
1085
1086         rbc->breaking_threshold = 10.0f; /* no good default here, just use 10 for now */
1087         rbc->num_solver_iterations = 10; /* 10 is Bullet default */
1088
1089         rbc->limit_lin_x_lower = -1.0f;
1090         rbc->limit_lin_x_upper = 1.0f;
1091         rbc->limit_lin_y_lower = -1.0f;
1092         rbc->limit_lin_y_upper = 1.0f;
1093         rbc->limit_lin_z_lower = -1.0f;
1094         rbc->limit_lin_z_upper = 1.0f;
1095         rbc->limit_ang_x_lower = -M_PI_4;
1096         rbc->limit_ang_x_upper = M_PI_4;
1097         rbc->limit_ang_y_lower = -M_PI_4;
1098         rbc->limit_ang_y_upper = M_PI_4;
1099         rbc->limit_ang_z_lower = -M_PI_4;
1100         rbc->limit_ang_z_upper = M_PI_4;
1101
1102         rbc->spring_damping_x = 0.5f;
1103         rbc->spring_damping_y = 0.5f;
1104         rbc->spring_damping_z = 0.5f;
1105         rbc->spring_damping_ang_x = 0.5f;
1106         rbc->spring_damping_ang_y = 0.5f;
1107         rbc->spring_damping_ang_z = 0.5f;
1108         rbc->spring_stiffness_x = 10.0f;
1109         rbc->spring_stiffness_y = 10.0f;
1110         rbc->spring_stiffness_z = 10.0f;
1111         rbc->spring_stiffness_ang_x = 10.0f;
1112         rbc->spring_stiffness_ang_y = 10.0f;
1113         rbc->spring_stiffness_ang_z = 10.0f;
1114
1115         rbc->motor_lin_max_impulse = 1.0f;
1116         rbc->motor_lin_target_velocity = 1.0f;
1117         rbc->motor_ang_max_impulse = 1.0f;
1118         rbc->motor_ang_target_velocity = 1.0f;
1119
1120         /* flag cache as outdated */
1121         BKE_rigidbody_cache_reset(rbw);
1122
1123         /* return this object */
1124         return rbc;
1125 }
1126
1127 /* ************************************** */
1128 /* Utilities API */
1129
1130 /* Get RigidBody world for the given scene, creating one if needed
1131  *
1132  * \param scene: Scene to find active Rigid Body world for
1133  */
1134 RigidBodyWorld *BKE_rigidbody_get_world(Scene *scene)
1135 {
1136         /* sanity check */
1137         if (scene == NULL)
1138                 return NULL;
1139
1140         return scene->rigidbody_world;
1141 }
1142
1143 void BKE_rigidbody_remove_object(Scene *scene, Object *ob)
1144 {
1145         RigidBodyWorld *rbw = scene->rigidbody_world;
1146         RigidBodyOb *rbo = ob->rigidbody_object;
1147         RigidBodyCon *rbc;
1148         GroupObject *go;
1149         int i;
1150
1151         if (rbw) {
1152                 /* remove from rigidbody world, free object won't do this */
1153                 if (rbw->physics_world && rbo->physics_object)
1154                         RB_dworld_remove_body(rbw->physics_world, rbo->physics_object);
1155
1156                 /* remove object from array */
1157                 if (rbw && rbw->objects) {
1158                         for (i = 0; i < rbw->numbodies; i++) {
1159                                 if (rbw->objects[i] == ob) {
1160                                         rbw->objects[i] = NULL;
1161                                         break;
1162                                 }
1163                         }
1164                 }
1165
1166                 /* remove object from rigid body constraints */
1167                 if (rbw->constraints) {
1168                         for (go = rbw->constraints->gobject.first; go; go = go->next) {
1169                                 Object *obt = go->ob;
1170                                 if (obt && obt->rigidbody_constraint) {
1171                                         rbc = obt->rigidbody_constraint;
1172                                         if (ELEM(ob, rbc->ob1, rbc->ob2)) {
1173                                                 BKE_rigidbody_remove_constraint(scene, obt);
1174                                         }
1175                                 }
1176                         }
1177                 }
1178         }
1179
1180         /* remove object's settings */
1181         BKE_rigidbody_free_object(ob);
1182
1183         /* flag cache as outdated */
1184         BKE_rigidbody_cache_reset(rbw);
1185 }
1186
1187 void BKE_rigidbody_remove_constraint(Scene *scene, Object *ob)
1188 {
1189         RigidBodyWorld *rbw = scene->rigidbody_world;
1190         RigidBodyCon *rbc = ob->rigidbody_constraint;
1191
1192         /* remove from rigidbody world, free object won't do this */
1193         if (rbw && rbw->physics_world && rbc->physics_constraint) {
1194                 RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
1195         }
1196         /* remove object's settings */
1197         BKE_rigidbody_free_constraint(ob);
1198
1199         /* flag cache as outdated */
1200         BKE_rigidbody_cache_reset(rbw);
1201 }
1202
1203
1204 /* ************************************** */
1205 /* Simulation Interface - Bullet */
1206
1207 /* Update object array and rigid body count so they're in sync with the rigid body group */
1208 static void rigidbody_update_ob_array(RigidBodyWorld *rbw)
1209 {
1210         GroupObject *go;
1211         int i, n;
1212
1213         n = BLI_listbase_count(&rbw->group->gobject);
1214
1215         if (rbw->numbodies != n) {
1216                 rbw->numbodies = n;
1217                 rbw->objects = realloc(rbw->objects, sizeof(Object *) * rbw->numbodies);
1218         }
1219
1220         for (go = rbw->group->gobject.first, i = 0; go; go = go->next, i++) {
1221                 Object *ob = go->ob;
1222                 rbw->objects[i] = ob;
1223         }
1224 }
1225
1226 static void rigidbody_update_sim_world(Scene *scene, RigidBodyWorld *rbw)
1227 {
1228         float adj_gravity[3];
1229
1230         /* adjust gravity to take effector weights into account */
1231         if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
1232                 copy_v3_v3(adj_gravity, scene->physics_settings.gravity);
1233                 mul_v3_fl(adj_gravity, rbw->effector_weights->global_gravity * rbw->effector_weights->weight[0]);
1234         }
1235         else {
1236                 zero_v3(adj_gravity);
1237         }
1238
1239         /* update gravity, since this RNA setting is not part of RigidBody settings */
1240         RB_dworld_set_gravity(rbw->physics_world, adj_gravity);
1241
1242         /* update object array in case there are changes */
1243         rigidbody_update_ob_array(rbw);
1244 }
1245
1246 static void rigidbody_update_sim_ob(Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo)
1247 {
1248         float loc[3];
1249         float rot[4];
1250         float scale[3];
1251
1252         /* only update if rigid body exists */
1253         if (rbo->physics_object == NULL)
1254                 return;
1255
1256         if (rbo->shape == RB_SHAPE_TRIMESH && rbo->flag & RBO_FLAG_USE_DEFORM) {
1257                 DerivedMesh *dm = ob->derivedDeform;
1258                 if (dm) {
1259                         MVert *mvert = dm->getVertArray(dm);
1260                         int totvert = dm->getNumVerts(dm);
1261                         BoundBox *bb = BKE_object_boundbox_get(ob);
1262
1263                         RB_shape_trimesh_update(rbo->physics_shape, (float *)mvert, totvert, sizeof(MVert), bb->vec[0], bb->vec[6]);
1264                 }
1265         }
1266
1267         mat4_decompose(loc, rot, scale, ob->obmat);
1268
1269         /* update scale for all objects */
1270         RB_body_set_scale(rbo->physics_object, scale);
1271         /* compensate for embedded convex hull collision margin */
1272         if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && rbo->shape == RB_SHAPE_CONVEXH)
1273                 RB_shape_set_margin(rbo->physics_shape, RBO_GET_MARGIN(rbo) * MIN3(scale[0], scale[1], scale[2]));
1274
1275         /* make transformed objects temporarily kinmatic so that they can be moved by the user during simulation */
1276         if (ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ) {
1277                 RB_body_set_kinematic_state(rbo->physics_object, true);
1278                 RB_body_set_mass(rbo->physics_object, 0.0f);
1279         }
1280
1281         /* update rigid body location and rotation for kinematic bodies */
1282         if (rbo->flag & RBO_FLAG_KINEMATIC || (ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ)) {
1283                 RB_body_activate(rbo->physics_object);
1284                 RB_body_set_loc_rot(rbo->physics_object, loc, rot);
1285         }
1286         /* update influence of effectors - but don't do it on an effector */
1287         /* only dynamic bodies need effector update */
1288         else if (rbo->type == RBO_TYPE_ACTIVE && ((ob->pd == NULL) || (ob->pd->forcefield == PFIELD_NULL))) {
1289                 EffectorWeights *effector_weights = rbw->effector_weights;
1290                 EffectedPoint epoint;
1291                 ListBase *effectors;
1292
1293                 /* get effectors present in the group specified by effector_weights */
1294                 effectors = pdInitEffectors(scene, ob, NULL, effector_weights, true);
1295                 if (effectors) {
1296                         float eff_force[3] = {0.0f, 0.0f, 0.0f};
1297                         float eff_loc[3], eff_vel[3];
1298
1299                         /* create dummy 'point' which represents last known position of object as result of sim */
1300                         // XXX: this can create some inaccuracies with sim position, but is probably better than using unsimulated vals?
1301                         RB_body_get_position(rbo->physics_object, eff_loc);
1302                         RB_body_get_linear_velocity(rbo->physics_object, eff_vel);
1303
1304                         pd_point_from_loc(scene, eff_loc, eff_vel, 0, &epoint);
1305
1306                         /* calculate net force of effectors, and apply to sim object
1307                          * - we use 'central force' since apply force requires a "relative position" which we don't have...
1308                          */
1309                         pdDoEffectors(effectors, NULL, effector_weights, &epoint, eff_force, NULL);
1310                         if (G.f & G_DEBUG)
1311                                 printf("\tapplying force (%f,%f,%f) to '%s'\n", eff_force[0], eff_force[1], eff_force[2], ob->id.name + 2);
1312                         /* activate object in case it is deactivated */
1313                         if (!is_zero_v3(eff_force))
1314                                 RB_body_activate(rbo->physics_object);
1315                         RB_body_apply_central_force(rbo->physics_object, eff_force);
1316                 }
1317                 else if (G.f & G_DEBUG)
1318                         printf("\tno forces to apply to '%s'\n", ob->id.name + 2);
1319
1320                 /* cleanup */
1321                 pdEndEffectors(&effectors);
1322         }
1323         /* NOTE: passive objects don't need to be updated since they don't move */
1324
1325         /* NOTE: no other settings need to be explicitly updated here,
1326          * since RNA setters take care of the rest :)
1327          */
1328 }
1329
1330 /**
1331  * Updates and validates world, bodies and shapes.
1332  *
1333  * \param rebuild: Rebuild entire simulation
1334  */
1335 static void rigidbody_update_simulation(Scene *scene, RigidBodyWorld *rbw, bool rebuild)
1336 {
1337         GroupObject *go;
1338
1339         /* update world */
1340         if (rebuild)
1341                 BKE_rigidbody_validate_sim_world(scene, rbw, true);
1342         rigidbody_update_sim_world(scene, rbw);
1343
1344         /* XXX TODO For rebuild: remove all constraints first.
1345          * Otherwise we can end up deleting objects that are still
1346          * referenced by constraints, corrupting bullet's internal list.
1347          *
1348          * Memory management needs redesign here, this is just a dirty workaround.
1349          */
1350         if (rebuild && rbw->constraints) {
1351                 for (go = rbw->constraints->gobject.first; go; go = go->next) {
1352                         Object *ob = go->ob;
1353                         if (ob) {
1354                                 RigidBodyCon *rbc = ob->rigidbody_constraint;
1355                                 if (rbc && rbc->physics_constraint) {
1356                                         RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
1357                                         RB_constraint_delete(rbc->physics_constraint);
1358                                         rbc->physics_constraint = NULL;
1359                                 }
1360                         }
1361                 }
1362         }
1363
1364         /* update objects */
1365         for (go = rbw->group->gobject.first; go; go = go->next) {
1366                 Object *ob = go->ob;
1367
1368                 if (ob && ob->type == OB_MESH) {
1369                         /* validate that we've got valid object set up here... */
1370                         RigidBodyOb *rbo = ob->rigidbody_object;
1371                         /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
1372                         BKE_object_where_is_calc(scene, ob);
1373
1374                         if (rbo == NULL) {
1375                                 /* Since this object is included in the sim group but doesn't have
1376                                  * rigid body settings (perhaps it was added manually), add!
1377                                  * - assume object to be active? That is the default for newly added settings...
1378                                  */
1379                                 ob->rigidbody_object = BKE_rigidbody_create_object(scene, ob, RBO_TYPE_ACTIVE);
1380                                 rigidbody_validate_sim_object(rbw, ob, true);
1381
1382                                 rbo = ob->rigidbody_object;
1383                         }
1384                         else {
1385                                 /* perform simulation data updates as tagged */
1386                                 /* refresh object... */
1387                                 if (rebuild) {
1388                                         /* World has been rebuilt so rebuild object */
1389                                         rigidbody_validate_sim_object(rbw, ob, true);
1390                                 }
1391                                 else if (rbo->flag & RBO_FLAG_NEEDS_VALIDATE) {
1392                                         rigidbody_validate_sim_object(rbw, ob, false);
1393                                 }
1394                                 /* refresh shape... */
1395                                 if (rbo->flag & RBO_FLAG_NEEDS_RESHAPE) {
1396                                         /* mesh/shape data changed, so force shape refresh */
1397                                         rigidbody_validate_sim_shape(ob, true);
1398                                         /* now tell RB sim about it */
1399                                         // XXX: we assume that this can only get applied for active/passive shapes that will be included as rigidbodies
1400                                         RB_body_set_collision_shape(rbo->physics_object, rbo->physics_shape);
1401                                 }
1402                                 rbo->flag &= ~(RBO_FLAG_NEEDS_VALIDATE | RBO_FLAG_NEEDS_RESHAPE);
1403                         }
1404
1405                         /* update simulation object... */
1406                         rigidbody_update_sim_ob(scene, rbw, ob, rbo);
1407                 }
1408         }
1409
1410         /* update constraints */
1411         if (rbw->constraints == NULL) /* no constraints, move on */
1412                 return;
1413         for (go = rbw->constraints->gobject.first; go; go = go->next) {
1414                 Object *ob = go->ob;
1415
1416                 if (ob) {
1417                         /* validate that we've got valid object set up here... */
1418                         RigidBodyCon *rbc = ob->rigidbody_constraint;
1419                         /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
1420                         BKE_object_where_is_calc(scene, ob);
1421
1422                         if (rbc == NULL) {
1423                                 /* Since this object is included in the group but doesn't have
1424                                  * constraint settings (perhaps it was added manually), add!
1425                                  */
1426                                 ob->rigidbody_constraint = BKE_rigidbody_create_constraint(scene, ob, RBC_TYPE_FIXED);
1427                                 rigidbody_validate_sim_constraint(rbw, ob, true);
1428
1429                                 rbc = ob->rigidbody_constraint;
1430                         }
1431                         else {
1432                                 /* perform simulation data updates as tagged */
1433                                 if (rebuild) {
1434                                         /* World has been rebuilt so rebuild constraint */
1435                                         rigidbody_validate_sim_constraint(rbw, ob, true);
1436                                 }
1437                                 else if (rbc->flag & RBC_FLAG_NEEDS_VALIDATE) {
1438                                         rigidbody_validate_sim_constraint(rbw, ob, false);
1439                                 }
1440                                 rbc->flag &= ~RBC_FLAG_NEEDS_VALIDATE;
1441                         }
1442                 }
1443         }
1444 }
1445
1446 static void rigidbody_update_simulation_post_step(RigidBodyWorld *rbw)
1447 {
1448         GroupObject *go;
1449
1450         for (go = rbw->group->gobject.first; go; go = go->next) {
1451                 Object *ob = go->ob;
1452
1453                 if (ob) {
1454                         RigidBodyOb *rbo = ob->rigidbody_object;
1455                         /* reset kinematic state for transformed objects */
1456                         if (rbo && (ob->flag & SELECT) && (G.moving & G_TRANSFORM_OBJ)) {
1457                                 RB_body_set_kinematic_state(rbo->physics_object, rbo->flag & RBO_FLAG_KINEMATIC || rbo->flag & RBO_FLAG_DISABLED);
1458                                 RB_body_set_mass(rbo->physics_object, RBO_GET_MASS(rbo));
1459                                 /* deactivate passive objects so they don't interfere with deactivation of active objects */
1460                                 if (rbo->type == RBO_TYPE_PASSIVE)
1461                                         RB_body_deactivate(rbo->physics_object);
1462                         }
1463                 }
1464         }
1465 }
1466
1467 bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime)
1468 {
1469         return (rbw && (rbw->flag & RBW_FLAG_MUTED) == 0 && ctime > rbw->pointcache->startframe);
1470 }
1471
1472 /* Sync rigid body and object transformations */
1473 void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime)
1474 {
1475         RigidBodyOb *rbo = ob->rigidbody_object;
1476
1477         /* keep original transform for kinematic and passive objects */
1478         if (ELEM(NULL, rbw, rbo) || rbo->flag & RBO_FLAG_KINEMATIC || rbo->type == RBO_TYPE_PASSIVE)
1479                 return;
1480
1481         /* use rigid body transform after cache start frame if objects is not being transformed */
1482         if (BKE_rigidbody_check_sim_running(rbw, ctime) && !(ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ)) {
1483                 float mat[4][4], size_mat[4][4], size[3];
1484
1485                 normalize_qt(rbo->orn); // RB_TODO investigate why quaternion isn't normalized at this point
1486                 quat_to_mat4(mat, rbo->orn);
1487                 copy_v3_v3(mat[3], rbo->pos);
1488
1489                 mat4_to_size(size, ob->obmat);
1490                 size_to_mat4(size_mat, size);
1491                 mul_m4_m4m4(mat, mat, size_mat);
1492
1493                 copy_m4_m4(ob->obmat, mat);
1494         }
1495         /* otherwise set rigid body transform to current obmat */
1496         else {
1497                 mat4_to_loc_quat(rbo->pos, rbo->orn, ob->obmat);
1498         }
1499 }
1500
1501 /* Used when canceling transforms - return rigidbody and object to initial states */
1502 void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle)
1503 {
1504         RigidBodyOb *rbo = ob->rigidbody_object;
1505         bool correct_delta = !(rbo->flag & RBO_FLAG_KINEMATIC || rbo->type == RBO_TYPE_PASSIVE);
1506
1507         /* return rigid body and object to their initial states */
1508         copy_v3_v3(rbo->pos, ob->loc);
1509         copy_v3_v3(ob->loc, loc);
1510
1511         if (correct_delta) {
1512                 add_v3_v3(rbo->pos, ob->dloc);
1513         }
1514
1515         if (ob->rotmode > 0) {
1516                 float qt[4];
1517                 eulO_to_quat(qt, ob->rot, ob->rotmode);
1518
1519                 if (correct_delta) {
1520                         float dquat[4];
1521                         eulO_to_quat(dquat, ob->drot, ob->rotmode);
1522
1523                         mul_qt_qtqt(rbo->orn, dquat, qt);
1524                 }
1525                 else {
1526                         copy_qt_qt(rbo->orn, qt);
1527                 }
1528
1529                 copy_v3_v3(ob->rot, rot);
1530         }
1531         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1532                 float qt[4];
1533                 axis_angle_to_quat(qt, ob->rotAxis, ob->rotAngle);
1534
1535                 if (correct_delta) {
1536                         float dquat[4];
1537                         axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
1538
1539                         mul_qt_qtqt(rbo->orn, dquat, qt);
1540                 }
1541                 else {
1542                         copy_qt_qt(rbo->orn, qt);
1543                 }
1544
1545                 copy_v3_v3(ob->rotAxis, rotAxis);
1546                 ob->rotAngle = rotAngle;
1547         }
1548         else {
1549                 if (correct_delta) {
1550                         mul_qt_qtqt(rbo->orn, ob->dquat, ob->quat);
1551                 }
1552                 else {
1553                         copy_qt_qt(rbo->orn, ob->quat);
1554                 }
1555
1556                 copy_qt_qt(ob->quat, quat);
1557         }
1558
1559         if (rbo->physics_object) {
1560                 /* allow passive objects to return to original transform */
1561                 if (rbo->type == RBO_TYPE_PASSIVE)
1562                         RB_body_set_kinematic_state(rbo->physics_object, true);
1563                 RB_body_set_loc_rot(rbo->physics_object, rbo->pos, rbo->orn);
1564         }
1565         // RB_TODO update rigid body physics object's loc/rot for dynamic objects here as well (needs to be done outside bullet's update loop)
1566 }
1567
1568 void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw)
1569 {
1570         if (rbw) {
1571                 rbw->pointcache->flag |= PTCACHE_OUTDATED;
1572         }
1573 }
1574
1575 /* ------------------ */
1576
1577 /* Rebuild rigid body world */
1578 /* NOTE: this needs to be called before frame update to work correctly */
1579 void BKE_rigidbody_rebuild_world(Scene *scene, float ctime)
1580 {
1581         RigidBodyWorld *rbw = scene->rigidbody_world;
1582         PointCache *cache;
1583         PTCacheID pid;
1584         int startframe, endframe;
1585
1586         BKE_ptcache_id_from_rigidbody(&pid, NULL, rbw);
1587         BKE_ptcache_id_time(&pid, scene, ctime, &startframe, &endframe, NULL);
1588         cache = rbw->pointcache;
1589
1590         /* flag cache as outdated if we don't have a world or number of objects in the simulation has changed */
1591         if (rbw->physics_world == NULL || rbw->numbodies != BLI_listbase_count(&rbw->group->gobject)) {
1592                 cache->flag |= PTCACHE_OUTDATED;
1593         }
1594
1595         if (ctime == startframe + 1 && rbw->ltime == startframe) {
1596                 if (cache->flag & PTCACHE_OUTDATED) {
1597                         BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
1598                         rigidbody_update_simulation(scene, rbw, true);
1599                         BKE_ptcache_validate(cache, (int)ctime);
1600                         cache->last_exact = 0;
1601                         cache->flag &= ~PTCACHE_REDO_NEEDED;
1602                 }
1603         }
1604 }
1605
1606 /* Run RigidBody simulation for the specified physics world */
1607 void BKE_rigidbody_do_simulation(Scene *scene, float ctime)
1608 {
1609         float timestep;
1610         RigidBodyWorld *rbw = scene->rigidbody_world;
1611         PointCache *cache;
1612         PTCacheID pid;
1613         int startframe, endframe;
1614
1615         BKE_ptcache_id_from_rigidbody(&pid, NULL, rbw);
1616         BKE_ptcache_id_time(&pid, scene, ctime, &startframe, &endframe, NULL);
1617         cache = rbw->pointcache;
1618
1619         if (ctime <= startframe) {
1620                 rbw->ltime = startframe;
1621                 return;
1622         }
1623         /* make sure we don't go out of cache frame range */
1624         else if (ctime > endframe) {
1625                 ctime = endframe;
1626         }
1627
1628         /* don't try to run the simulation if we don't have a world yet but allow reading baked cache */
1629         if (rbw->physics_world == NULL && !(cache->flag & PTCACHE_BAKED))
1630                 return;
1631         else if (rbw->objects == NULL)
1632                 rigidbody_update_ob_array(rbw);
1633
1634         /* try to read from cache */
1635         // RB_TODO deal with interpolated, old and baked results
1636         bool can_simulate = (ctime == rbw->ltime + 1) && !(cache->flag & PTCACHE_BAKED);
1637
1638         if (BKE_ptcache_read(&pid, ctime, can_simulate) == PTCACHE_READ_EXACT) {
1639                 BKE_ptcache_validate(cache, (int)ctime);
1640                 rbw->ltime = ctime;
1641                 return;
1642         }
1643
1644         /* advance simulation, we can only step one frame forward */
1645         if (can_simulate) {
1646                 /* write cache for first frame when on second frame */
1647                 if (rbw->ltime == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact == 0)) {
1648                         BKE_ptcache_write(&pid, startframe);
1649                 }
1650
1651                 /* update and validate simulation */
1652                 rigidbody_update_simulation(scene, rbw, false);
1653
1654                 /* calculate how much time elapsed since last step in seconds */
1655                 timestep = 1.0f / (float)FPS * (ctime - rbw->ltime) * rbw->time_scale;
1656                 /* step simulation by the requested timestep, steps per second are adjusted to take time scale into account */
1657                 RB_dworld_step_simulation(rbw->physics_world, timestep, INT_MAX, 1.0f / (float)rbw->steps_per_second * min_ff(rbw->time_scale, 1.0f));
1658
1659                 rigidbody_update_simulation_post_step(rbw);
1660
1661                 /* write cache for current frame */
1662                 BKE_ptcache_validate(cache, (int)ctime);
1663                 BKE_ptcache_write(&pid, (unsigned int)ctime);
1664
1665                 rbw->ltime = ctime;
1666         }
1667 }
1668 /* ************************************** */
1669
1670 #else  /* WITH_BULLET */
1671
1672 /* stubs */
1673 #if defined(__GNUC__) || defined(__clang__)
1674 #  pragma GCC diagnostic push
1675 #  pragma GCC diagnostic ignored "-Wunused-parameter"
1676 #endif
1677
1678 struct RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob, const int flag) { return NULL; }
1679 struct RigidBodyCon *BKE_rigidbody_copy_constraint(const Object *ob, const int flag) { return NULL; }
1680 void BKE_rigidbody_validate_sim_world(Scene *scene, RigidBodyWorld *rbw, bool rebuild) {}
1681 void BKE_rigidbody_calc_volume(Object *ob, float *r_vol) { if (r_vol) *r_vol = 0.0f; }
1682 void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3]) { zero_v3(r_center); }
1683 struct RigidBodyWorld *BKE_rigidbody_create_world(Scene *scene) { return NULL; }
1684 struct RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw, const int flag) { return NULL; }
1685 void BKE_rigidbody_world_groups_relink(struct RigidBodyWorld *rbw) {}
1686 void BKE_rigidbody_world_id_loop(struct RigidBodyWorld *rbw, RigidbodyWorldIDFunc func, void *userdata) {}
1687 struct RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type) { return NULL; }
1688 struct RigidBodyCon *BKE_rigidbody_create_constraint(Scene *scene, Object *ob, short type) { return NULL; }
1689 struct RigidBodyWorld *BKE_rigidbody_get_world(Scene *scene) { return NULL; }
1690 void BKE_rigidbody_remove_object(Scene *scene, Object *ob) {}
1691 void BKE_rigidbody_remove_constraint(Scene *scene, Object *ob) {}
1692 void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime) {}
1693 void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle) {}
1694 bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime) { return false; }
1695 void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw) {}
1696 void BKE_rigidbody_rebuild_world(Scene *scene, float ctime) {}
1697 void BKE_rigidbody_do_simulation(Scene *scene, float ctime) {}
1698
1699 #if defined(__GNUC__) || defined(__clang__)
1700 #  pragma GCC diagnostic pop
1701 #endif
1702
1703 #endif  /* WITH_BULLET */
1704
1705 /* -------------------- */
1706 /* Depsgraph evaluation */
1707
1708 void BKE_rigidbody_rebuild_sim(EvaluationContext *UNUSED(eval_ctx),
1709                                Scene *scene)
1710 {
1711         float ctime = BKE_scene_frame_get(scene);
1712         DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
1713         /* rebuild sim data (i.e. after resetting to start of timeline) */
1714         if (BKE_scene_check_rigidbody_active(scene)) {
1715                 BKE_rigidbody_rebuild_world(scene, ctime);
1716         }
1717 }
1718
1719 void BKE_rigidbody_eval_simulation(EvaluationContext *UNUSED(eval_ctx),
1720                                    Scene *scene)
1721 {
1722         float ctime = BKE_scene_frame_get(scene);
1723         DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
1724         /* evaluate rigidbody sim */
1725         if (BKE_scene_check_rigidbody_active(scene)) {
1726                 BKE_rigidbody_do_simulation(scene, ctime);
1727         }
1728 }
1729
1730 void BKE_rigidbody_object_sync_transforms(EvaluationContext *UNUSED(eval_ctx),
1731                                           Scene *scene,
1732                                           Object *ob)
1733 {
1734         RigidBodyWorld *rbw = scene->rigidbody_world;
1735         float ctime = BKE_scene_frame_get(scene);
1736         DEG_debug_print_eval_time(__func__, ob->id.name, ob, ctime);
1737         /* read values pushed into RBO from sim/cache... */
1738         BKE_rigidbody_sync_transforms(rbw, ob, ctime);
1739 }