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