Merge remote-tracking branch 'origin/master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / rigidbody.c
index 1c16d57ab125fc46369357847e84a0198a40dcd0..75018bbe5bde49140c16edbce21fe0aaeb2a4ed4 100644 (file)
@@ -55,9 +55,9 @@
 #include "DNA_scene_types.h"
 
 #include "BKE_cdderivedmesh.h"
+#include "BKE_collection.h"
 #include "BKE_effect.h"
 #include "BKE_global.h"
-#include "BKE_group.h"
 #include "BKE_library.h"
 #include "BKE_library_query.h"
 #include "BKE_mesh.h"
@@ -67,6 +67,7 @@
 #include "BKE_scene.h"
 
 #include "DEG_depsgraph.h"
+#include "DEG_depsgraph_query.h"
 
 /* ************************************** */
 /* Memory Management */
@@ -94,7 +95,7 @@ void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
        if (rbw->physics_world) {
                /* free physics references, we assume that all physics objects in will have been added to the world */
                if (rbw->constraints) {
-                       FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, object)
+                       FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->constraints, object)
                        {
                                if (object->rigidbody_constraint) {
                                        RigidBodyCon *rbc = object->rigidbody_constraint;
@@ -103,11 +104,11 @@ void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
                                        }
                                }
                        }
-                       FOREACH_GROUP_OBJECT_END;
+                       FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
                }
 
                if (rbw->group) {
-                       FOREACH_GROUP_OBJECT_BEGIN(rbw->group, object)
+                       FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, object)
                        {
                                if (object->rigidbody_object) {
                                        RigidBodyOb *rbo = object->rigidbody_object;
@@ -116,7 +117,7 @@ void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
                                        }
                                }
                        }
-                       FOREACH_GROUP_OBJECT_END;
+                       FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
                }
                /* free dynamics world */
                RB_dworld_delete(rbw->physics_world);
@@ -1156,7 +1157,7 @@ void BKE_rigidbody_remove_object(Scene *scene, Object *ob)
 
                /* remove object from rigid body constraints */
                if (rbw->constraints) {
-                       FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, obt)
+                       FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->constraints, obt)
                        {
                                if (obt && obt->rigidbody_constraint) {
                                        rbc = obt->rigidbody_constraint;
@@ -1165,7 +1166,7 @@ void BKE_rigidbody_remove_object(Scene *scene, Object *ob)
                                        }
                                }
                        }
-                       FOREACH_GROUP_OBJECT_END;
+                       FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
                }
        }
 
@@ -1199,9 +1200,10 @@ void BKE_rigidbody_remove_constraint(Scene *scene, Object *ob)
 /* Update object array and rigid body count so they're in sync with the rigid body group */
 static void rigidbody_update_ob_array(RigidBodyWorld *rbw)
 {
+       const ListBase objects = BKE_collection_object_cache_get(rbw->group);
        int i, n;
 
-       n = BLI_listbase_count(&rbw->group->view_layer->object_bases);
+       n = BLI_listbase_count(&objects);
 
        if (rbw->numbodies != n) {
                rbw->numbodies = n;
@@ -1209,12 +1211,12 @@ static void rigidbody_update_ob_array(RigidBodyWorld *rbw)
        }
 
        i = 0;
-       FOREACH_GROUP_OBJECT_BEGIN(rbw->group, object)
+       FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, object)
        {
                rbw->objects[i] = object;
                i++;
        }
-       FOREACH_GROUP_OBJECT_END;
+       FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
 }
 
 static void rigidbody_update_sim_world(Scene *scene, RigidBodyWorld *rbw)
@@ -1340,7 +1342,7 @@ static void rigidbody_update_simulation(struct Depsgraph *depsgraph, Scene *scen
         * Memory management needs redesign here, this is just a dirty workaround.
         */
        if (rebuild && rbw->constraints) {
-               FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, ob)
+               FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->constraints, ob)
                {
                        RigidBodyCon *rbc = ob->rigidbody_constraint;
                        if (rbc && rbc->physics_constraint) {
@@ -1349,11 +1351,11 @@ static void rigidbody_update_simulation(struct Depsgraph *depsgraph, Scene *scen
                                rbc->physics_constraint = NULL;
                        }
                }
-               FOREACH_GROUP_OBJECT_END;
+               FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
        }
 
        /* update objects */
-       FOREACH_GROUP_OBJECT_BEGIN(rbw->group, ob)
+       FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, ob)
        {
                if (ob->type == OB_MESH) {
                        /* validate that we've got valid object set up here... */
@@ -1396,13 +1398,13 @@ static void rigidbody_update_simulation(struct Depsgraph *depsgraph, Scene *scen
                        rigidbody_update_sim_ob(depsgraph, scene, rbw, ob, rbo);
                }
        }
-       FOREACH_GROUP_OBJECT_END;
+       FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
        
        /* update constraints */
        if (rbw->constraints == NULL) /* no constraints, move on */
                return;
 
-       FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, ob)
+       FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->constraints, ob)
        {
                /* validate that we've got valid object set up here... */
                RigidBodyCon *rbc = ob->rigidbody_constraint;
@@ -1430,12 +1432,12 @@ static void rigidbody_update_simulation(struct Depsgraph *depsgraph, Scene *scen
                        rbc->flag &= ~RBC_FLAG_NEEDS_VALIDATE;
                }
        }
-       FOREACH_GROUP_OBJECT_END;
+       FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
 }
 
 static void rigidbody_update_simulation_post_step(RigidBodyWorld *rbw)
 {
-       FOREACH_GROUP_BASE_BEGIN(rbw->group, base)
+       FOREACH_COLLECTION_BASE_RECURSIVE_BEGIN(rbw->group, base)
        {
                Object *ob = base->object;
                RigidBodyOb *rbo = ob->rigidbody_object;
@@ -1448,7 +1450,7 @@ static void rigidbody_update_simulation_post_step(RigidBodyWorld *rbw)
                                RB_body_deactivate(rbo->physics_object);
                }
        }
-       FOREACH_GROUP_BASE_END
+       FOREACH_COLLECTION_BASE_RECURSIVE_END
 }
 
 bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime)
@@ -1575,7 +1577,8 @@ void BKE_rigidbody_rebuild_world(struct Depsgraph *depsgraph, Scene *scene, floa
        cache = rbw->pointcache;
 
        /* flag cache as outdated if we don't have a world or number of objects in the simulation has changed */
-       if (rbw->physics_world == NULL || rbw->numbodies != BLI_listbase_count(&rbw->group->view_layer->object_bases)) {
+       const ListBase objects = BKE_collection_object_cache_get(rbw->group);
+       if (rbw->physics_world == NULL || rbw->numbodies != BLI_listbase_count(&objects)) {
                cache->flag |= PTCACHE_OUTDATED;
        }
 
@@ -1657,7 +1660,7 @@ void BKE_rigidbody_do_simulation(struct Depsgraph *depsgraph, Scene *scene, floa
 #else  /* WITH_BULLET */
 
 /* stubs */
-#ifdef __GNUC__
+#if defined(__GNUC__) || defined(__clang__)
 #  pragma GCC diagnostic push
 #  pragma GCC diagnostic ignored "-Wunused-parameter"
 #endif
@@ -1683,7 +1686,7 @@ void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw) {}
 void BKE_rigidbody_rebuild_world(struct Depsgraph *depsgraph, Scene *scene, float ctime) {}
 void BKE_rigidbody_do_simulation(struct Depsgraph *depsgraph, Scene *scene, float ctime) {}
 
-#ifdef __GNUC__
+#if defined(__GNUC__) || defined(__clang__)
 #  pragma GCC diagnostic pop
 #endif
 
@@ -1695,7 +1698,7 @@ void BKE_rigidbody_do_simulation(struct Depsgraph *depsgraph, Scene *scene, floa
 void BKE_rigidbody_rebuild_sim(struct Depsgraph *depsgraph,
                                Scene *scene)
 {
-       float ctime = BKE_scene_frame_get(scene);
+       float ctime = DEG_get_ctime(depsgraph);
        DEG_debug_print_eval_time(depsgraph, __func__, scene->id.name, scene, ctime);
        /* rebuild sim data (i.e. after resetting to start of timeline) */
        if (BKE_scene_check_rigidbody_active(scene)) {
@@ -1706,7 +1709,7 @@ void BKE_rigidbody_rebuild_sim(struct Depsgraph *depsgraph,
 void BKE_rigidbody_eval_simulation(struct Depsgraph *depsgraph,
                                    Scene *scene)
 {
-       float ctime = BKE_scene_frame_get(scene);
+       float ctime = DEG_get_ctime(depsgraph);
        DEG_debug_print_eval_time(depsgraph, __func__, scene->id.name, scene, ctime);
        /* evaluate rigidbody sim */
        if (BKE_scene_check_rigidbody_active(scene)) {
@@ -1719,7 +1722,7 @@ void BKE_rigidbody_object_sync_transforms(struct Depsgraph *depsgraph,
                                           Object *ob)
 {
        RigidBodyWorld *rbw = scene->rigidbody_world;
-       float ctime = BKE_scene_frame_get(scene);
+       float ctime = DEG_get_ctime(depsgraph);
        DEG_debug_print_eval_time(depsgraph, __func__, ob->id.name, ob, ctime);
        /* read values pushed into RBO from sim/cache... */
        BKE_rigidbody_sync_transforms(rbw, ob, ctime);