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