Depsgraph: Move evaluation debug prints to depsgraph
[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 /**
704  * Create physics sim representation of constraint given rigid body constraint settings
705  *
706  * \param rebuild Even if an instance already exists, replace it
707  */
708 static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, bool rebuild)
709 {
710         RigidBodyCon *rbc = (ob) ? ob->rigidbody_constraint : NULL;
711         float loc[3];
712         float rot[4];
713         float lin_lower;
714         float lin_upper;
715         float ang_lower;
716         float ang_upper;
717
718         /* sanity checks:
719          *      - object should have a rigid body constraint
720          *  - rigid body constraint should have at least one constrained object
721          */
722         if (rbc == NULL) {
723                 return;
724         }
725
726         if (ELEM(NULL, rbc->ob1, rbc->ob1->rigidbody_object, rbc->ob2, rbc->ob2->rigidbody_object)) {
727                 if (rbc->physics_constraint) {
728                         RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
729                         RB_constraint_delete(rbc->physics_constraint);
730                         rbc->physics_constraint = NULL;
731                 }
732                 return;
733         }
734
735         if (rbc->physics_constraint && rebuild == false) {
736                 RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
737         }
738         if (rbc->physics_constraint == NULL || rebuild) {
739                 rbRigidBody *rb1 = rbc->ob1->rigidbody_object->physics_object;
740                 rbRigidBody *rb2 = rbc->ob2->rigidbody_object->physics_object;
741
742                 /* remove constraint if it already exists before creating a new one */
743                 if (rbc->physics_constraint) {
744                         RB_constraint_delete(rbc->physics_constraint);
745                         rbc->physics_constraint = NULL;
746                 }
747
748                 mat4_to_loc_quat(loc, rot, ob->obmat);
749
750                 if (rb1 && rb2) {
751                         switch (rbc->type) {
752                                 case RBC_TYPE_POINT:
753                                         rbc->physics_constraint = RB_constraint_new_point(loc, rb1, rb2);
754                                         break;
755                                 case RBC_TYPE_FIXED:
756                                         rbc->physics_constraint = RB_constraint_new_fixed(loc, rot, rb1, rb2);
757                                         break;
758                                 case RBC_TYPE_HINGE:
759                                         rbc->physics_constraint = RB_constraint_new_hinge(loc, rot, rb1, rb2);
760                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z) {
761                                                 RB_constraint_set_limits_hinge(rbc->physics_constraint, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
762                                         }
763                                         else
764                                                 RB_constraint_set_limits_hinge(rbc->physics_constraint, 0.0f, -1.0f);
765                                         break;
766                                 case RBC_TYPE_SLIDER:
767                                         rbc->physics_constraint = RB_constraint_new_slider(loc, rot, rb1, rb2);
768                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
769                                                 RB_constraint_set_limits_slider(rbc->physics_constraint, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
770                                         else
771                                                 RB_constraint_set_limits_slider(rbc->physics_constraint, 0.0f, -1.0f);
772                                         break;
773                                 case RBC_TYPE_PISTON:
774                                         rbc->physics_constraint = RB_constraint_new_piston(loc, rot, rb1, rb2);
775                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X) {
776                                                 lin_lower = rbc->limit_lin_x_lower;
777                                                 lin_upper = rbc->limit_lin_x_upper;
778                                         }
779                                         else {
780                                                 lin_lower = 0.0f;
781                                                 lin_upper = -1.0f;
782                                         }
783                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X) {
784                                                 ang_lower = rbc->limit_ang_x_lower;
785                                                 ang_upper = rbc->limit_ang_x_upper;
786                                         }
787                                         else {
788                                                 ang_lower = 0.0f;
789                                                 ang_upper = -1.0f;
790                                         }
791                                         RB_constraint_set_limits_piston(rbc->physics_constraint, lin_lower, lin_upper, ang_lower, ang_upper);
792                                         break;
793                                 case RBC_TYPE_6DOF_SPRING:
794                                         rbc->physics_constraint = RB_constraint_new_6dof_spring(loc, rot, rb1, rb2);
795
796                                         RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->flag & RBC_FLAG_USE_SPRING_X);
797                                         RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->spring_stiffness_x);
798                                         RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->spring_damping_x);
799
800                                         RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->flag & RBC_FLAG_USE_SPRING_Y);
801                                         RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->spring_stiffness_y);
802                                         RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->spring_damping_y);
803
804                                         RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->flag & RBC_FLAG_USE_SPRING_Z);
805                                         RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->spring_stiffness_z);
806                                         RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->spring_damping_z);
807
808                                         RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->flag & RBC_FLAG_USE_SPRING_ANG_X);
809                                         RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->spring_stiffness_ang_x);
810                                         RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->spring_damping_ang_x);
811
812                                         RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->flag & RBC_FLAG_USE_SPRING_ANG_Y);
813                                         RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->spring_stiffness_ang_y);
814                                         RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->spring_damping_ang_y);
815
816                                         RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->flag & RBC_FLAG_USE_SPRING_ANG_Z);
817                                         RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->spring_stiffness_ang_z);
818                                         RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->spring_damping_ang_z);
819
820                                         RB_constraint_set_equilibrium_6dof_spring(rbc->physics_constraint);
821                                         ATTR_FALLTHROUGH;
822                                 case RBC_TYPE_6DOF:
823                                         if (rbc->type == RBC_TYPE_6DOF) /* a litte awkward but avoids duplicate code for limits */
824                                                 rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
825
826                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
827                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
828                                         else
829                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
830
831                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
832                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
833                                         else
834                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
835
836                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
837                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
838                                         else
839                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
840
841                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
842                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
843                                         else
844                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
845
846                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
847                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
848                                         else
849                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
850
851                                         if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
852                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
853                                         else
854                                                 RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
855                                         break;
856                                 case RBC_TYPE_MOTOR:
857                                         rbc->physics_constraint = RB_constraint_new_motor(loc, rot, rb1, rb2);
858
859                                         RB_constraint_set_enable_motor(rbc->physics_constraint, rbc->flag & RBC_FLAG_USE_MOTOR_LIN, rbc->flag & RBC_FLAG_USE_MOTOR_ANG);
860                                         RB_constraint_set_max_impulse_motor(rbc->physics_constraint, rbc->motor_lin_max_impulse, rbc->motor_ang_max_impulse);
861                                         RB_constraint_set_target_velocity_motor(rbc->physics_constraint, rbc->motor_lin_target_velocity, rbc->motor_ang_target_velocity);
862                                         break;
863                         }
864                 }
865                 else { /* can't create constraint without both rigid bodies */
866                         return;
867                 }
868
869                 RB_constraint_set_enabled(rbc->physics_constraint, rbc->flag & RBC_FLAG_ENABLED);
870
871                 if (rbc->flag & RBC_FLAG_USE_BREAKING)
872                         RB_constraint_set_breaking_threshold(rbc->physics_constraint, rbc->breaking_threshold);
873                 else
874                         RB_constraint_set_breaking_threshold(rbc->physics_constraint, FLT_MAX);
875
876                 if (rbc->flag & RBC_FLAG_OVERRIDE_SOLVER_ITERATIONS)
877                         RB_constraint_set_solver_iterations(rbc->physics_constraint, rbc->num_solver_iterations);
878                 else
879                         RB_constraint_set_solver_iterations(rbc->physics_constraint, -1);
880         }
881
882         if (rbw && rbw->physics_world && rbc->physics_constraint) {
883                 RB_dworld_add_constraint(rbw->physics_world, rbc->physics_constraint, rbc->flag & RBC_FLAG_DISABLE_COLLISIONS);
884         }
885 }
886
887 /* --------------------- */
888
889 /* Create physics sim world given RigidBody world settings */
890 // NOTE: this does NOT update object references that the scene uses, in case those aren't ready yet!
891 void BKE_rigidbody_validate_sim_world(Scene *scene, RigidBodyWorld *rbw, bool rebuild)
892 {
893         /* sanity checks */
894         if (rbw == NULL)
895                 return;
896
897         /* create new sim world */
898         if (rebuild || rbw->physics_world == NULL) {
899                 if (rbw->physics_world)
900                         RB_dworld_delete(rbw->physics_world);
901                 rbw->physics_world = RB_dworld_new(scene->physics_settings.gravity);
902         }
903
904         RB_dworld_set_solver_iterations(rbw->physics_world, rbw->num_solver_iterations);
905         RB_dworld_set_split_impulse(rbw->physics_world, rbw->flag & RBW_FLAG_USE_SPLIT_IMPULSE);
906 }
907
908 /* ************************************** */
909 /* Setup Utilities - Create Settings Blocks */
910
911 /* Set up RigidBody world */
912 RigidBodyWorld *BKE_rigidbody_create_world(Scene *scene)
913 {
914         /* try to get whatever RigidBody world that might be representing this already */
915         RigidBodyWorld *rbw;
916
917         /* sanity checks
918          *      - there must be a valid scene to add world to
919          *      - there mustn't be a sim world using this group already
920          */
921         if (scene == NULL)
922                 return NULL;
923
924         /* create a new sim world */
925         rbw = MEM_callocN(sizeof(RigidBodyWorld), "RigidBodyWorld");
926
927         /* set default settings */
928         rbw->effector_weights = BKE_add_effector_weights(NULL);
929
930         rbw->ltime = PSFRA;
931
932         rbw->time_scale = 1.0f;
933
934         rbw->steps_per_second = 60; /* Bullet default (60 Hz) */
935         rbw->num_solver_iterations = 10; /* 10 is bullet default */
936
937         rbw->pointcache = BKE_ptcache_add(&(rbw->ptcaches));
938         rbw->pointcache->step = 1;
939
940         /* return this sim world */
941         return rbw;
942 }
943
944 RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw, const int flag)
945 {
946         RigidBodyWorld *rbw_copy = MEM_dupallocN(rbw);
947
948         if (rbw->effector_weights) {
949                 rbw_copy->effector_weights = MEM_dupallocN(rbw->effector_weights);
950         }
951         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
952                 id_us_plus((ID *)rbw_copy->group);
953                 id_us_plus((ID *)rbw_copy->constraints);
954         }
955
956         /* XXX Never copy caches here? */
957         rbw_copy->pointcache = BKE_ptcache_copy_list(&rbw_copy->ptcaches, &rbw->ptcaches, flag & ~LIB_ID_COPY_CACHES);
958
959         rbw_copy->objects = NULL;
960         rbw_copy->physics_world = NULL;
961         rbw_copy->numbodies = 0;
962
963         return rbw_copy;
964 }
965
966 void BKE_rigidbody_world_groups_relink(RigidBodyWorld *rbw)
967 {
968         ID_NEW_REMAP(rbw->group);
969         ID_NEW_REMAP(rbw->constraints);
970         ID_NEW_REMAP(rbw->effector_weights->group);
971 }
972
973 void BKE_rigidbody_world_id_loop(RigidBodyWorld *rbw, RigidbodyWorldIDFunc func, void *userdata)
974 {
975         func(rbw, (ID **)&rbw->group, userdata, IDWALK_CB_NOP);
976         func(rbw, (ID **)&rbw->constraints, userdata, IDWALK_CB_NOP);
977         func(rbw, (ID **)&rbw->effector_weights->group, userdata, IDWALK_CB_NOP);
978
979         if (rbw->objects) {
980                 int i;
981                 for (i = 0; i < rbw->numbodies; i++) {
982                         func(rbw, (ID **)&rbw->objects[i], userdata, IDWALK_CB_NOP);
983                 }
984         }
985 }
986
987 /* Add rigid body settings to the specified object */
988 RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type)
989 {
990         RigidBodyOb *rbo;
991         RigidBodyWorld *rbw = scene->rigidbody_world;
992
993         /* sanity checks
994          *      - rigidbody world must exist
995          *      - object must exist
996          *      - cannot add rigid body if it already exists
997          */
998         if (ob == NULL || (ob->rigidbody_object != NULL))
999                 return NULL;
1000
1001         /* create new settings data, and link it up */
1002         rbo = MEM_callocN(sizeof(RigidBodyOb), "RigidBodyOb");
1003
1004         /* set default settings */
1005         rbo->type = type;
1006
1007         rbo->mass = 1.0f;
1008
1009         rbo->friction = 0.5f; /* best when non-zero. 0.5 is Bullet default */
1010         rbo->restitution = 0.0f; /* best when zero. 0.0 is Bullet default */
1011
1012         rbo->margin = 0.04f; /* 0.04 (in meters) is Bullet default */
1013
1014         rbo->lin_sleep_thresh = 0.4f; /* 0.4 is half of Bullet default */
1015         rbo->ang_sleep_thresh = 0.5f; /* 0.5 is half of Bullet default */
1016
1017         rbo->lin_damping = 0.04f; /* 0.04 is game engine default */
1018         rbo->ang_damping = 0.1f; /* 0.1 is game engine default */
1019
1020         rbo->col_groups = 1;
1021
1022         /* use triangle meshes for passive objects
1023          * use convex hulls for active objects since dynamic triangle meshes are very unstable
1024          */
1025         if (type == RBO_TYPE_ACTIVE)
1026                 rbo->shape = RB_SHAPE_CONVEXH;
1027         else
1028                 rbo->shape = RB_SHAPE_TRIMESH;
1029
1030         rbo->mesh_source = RBO_MESH_DEFORM;
1031
1032         /* set initial transform */
1033         mat4_to_loc_quat(rbo->pos, rbo->orn, ob->obmat);
1034
1035         /* flag cache as outdated */
1036         BKE_rigidbody_cache_reset(rbw);
1037
1038         /* return this object */
1039         return rbo;
1040 }
1041
1042 /* Add rigid body constraint to the specified object */
1043 RigidBodyCon *BKE_rigidbody_create_constraint(Scene *scene, Object *ob, short type)
1044 {
1045         RigidBodyCon *rbc;
1046         RigidBodyWorld *rbw = scene->rigidbody_world;
1047
1048         /* sanity checks
1049          *      - rigidbody world must exist
1050          *      - object must exist
1051          *      - cannot add constraint if it already exists
1052          */
1053         if (ob == NULL || (ob->rigidbody_constraint != NULL))
1054                 return NULL;
1055
1056         /* create new settings data, and link it up */
1057         rbc = MEM_callocN(sizeof(RigidBodyCon), "RigidBodyCon");
1058
1059         /* set default settings */
1060         rbc->type = type;
1061
1062         rbc->ob1 = NULL;
1063         rbc->ob2 = NULL;
1064
1065         rbc->flag |= RBC_FLAG_ENABLED;
1066         rbc->flag |= RBC_FLAG_DISABLE_COLLISIONS;
1067
1068         rbc->breaking_threshold = 10.0f; /* no good default here, just use 10 for now */
1069         rbc->num_solver_iterations = 10; /* 10 is Bullet default */
1070
1071         rbc->limit_lin_x_lower = -1.0f;
1072         rbc->limit_lin_x_upper = 1.0f;
1073         rbc->limit_lin_y_lower = -1.0f;
1074         rbc->limit_lin_y_upper = 1.0f;
1075         rbc->limit_lin_z_lower = -1.0f;
1076         rbc->limit_lin_z_upper = 1.0f;
1077         rbc->limit_ang_x_lower = -M_PI_4;
1078         rbc->limit_ang_x_upper = M_PI_4;
1079         rbc->limit_ang_y_lower = -M_PI_4;
1080         rbc->limit_ang_y_upper = M_PI_4;
1081         rbc->limit_ang_z_lower = -M_PI_4;
1082         rbc->limit_ang_z_upper = M_PI_4;
1083
1084         rbc->spring_damping_x = 0.5f;
1085         rbc->spring_damping_y = 0.5f;
1086         rbc->spring_damping_z = 0.5f;
1087         rbc->spring_damping_ang_x = 0.5f;
1088         rbc->spring_damping_ang_y = 0.5f;
1089         rbc->spring_damping_ang_z = 0.5f;
1090         rbc->spring_stiffness_x = 10.0f;
1091         rbc->spring_stiffness_y = 10.0f;
1092         rbc->spring_stiffness_z = 10.0f;
1093         rbc->spring_stiffness_ang_x = 10.0f;
1094         rbc->spring_stiffness_ang_y = 10.0f;
1095         rbc->spring_stiffness_ang_z = 10.0f;
1096
1097         rbc->motor_lin_max_impulse = 1.0f;
1098         rbc->motor_lin_target_velocity = 1.0f;
1099         rbc->motor_ang_max_impulse = 1.0f;
1100         rbc->motor_ang_target_velocity = 1.0f;
1101
1102         /* flag cache as outdated */
1103         BKE_rigidbody_cache_reset(rbw);
1104
1105         /* return this object */
1106         return rbc;
1107 }
1108
1109 /* ************************************** */
1110 /* Utilities API */
1111
1112 /* Get RigidBody world for the given scene, creating one if needed
1113  *
1114  * \param scene Scene to find active Rigid Body world for
1115  */
1116 RigidBodyWorld *BKE_rigidbody_get_world(Scene *scene)
1117 {
1118         /* sanity check */
1119         if (scene == NULL)
1120                 return NULL;
1121
1122         return scene->rigidbody_world;
1123 }
1124
1125 void BKE_rigidbody_remove_object(Scene *scene, Object *ob)
1126 {
1127         RigidBodyWorld *rbw = scene->rigidbody_world;
1128         RigidBodyOb *rbo = ob->rigidbody_object;
1129         RigidBodyCon *rbc;
1130         GroupObject *go;
1131         int i;
1132
1133         if (rbw) {
1134                 /* remove from rigidbody world, free object won't do this */
1135                 if (rbw->physics_world && rbo->physics_object)
1136                         RB_dworld_remove_body(rbw->physics_world, rbo->physics_object);
1137
1138                 /* remove object from array */
1139                 if (rbw && rbw->objects) {
1140                         for (i = 0; i < rbw->numbodies; i++) {
1141                                 if (rbw->objects[i] == ob) {
1142                                         rbw->objects[i] = NULL;
1143                                         break;
1144                                 }
1145                         }
1146                 }
1147
1148                 /* remove object from rigid body constraints */
1149                 if (rbw->constraints) {
1150                         for (go = rbw->constraints->gobject.first; go; go = go->next) {
1151                                 Object *obt = go->ob;
1152                                 if (obt && obt->rigidbody_constraint) {
1153                                         rbc = obt->rigidbody_constraint;
1154                                         if (ELEM(ob, rbc->ob1, rbc->ob2)) {
1155                                                 BKE_rigidbody_remove_constraint(scene, obt);
1156                                         }
1157                                 }
1158                         }
1159                 }
1160         }
1161
1162         /* remove object's settings */
1163         BKE_rigidbody_free_object(ob);
1164
1165         /* flag cache as outdated */
1166         BKE_rigidbody_cache_reset(rbw);
1167 }
1168
1169 void BKE_rigidbody_remove_constraint(Scene *scene, Object *ob)
1170 {
1171         RigidBodyWorld *rbw = scene->rigidbody_world;
1172         RigidBodyCon *rbc = ob->rigidbody_constraint;
1173
1174         /* remove from rigidbody world, free object won't do this */
1175         if (rbw && rbw->physics_world && rbc->physics_constraint) {
1176                 RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
1177         }
1178         /* remove object's settings */
1179         BKE_rigidbody_free_constraint(ob);
1180
1181         /* flag cache as outdated */
1182         BKE_rigidbody_cache_reset(rbw);
1183 }
1184
1185
1186 /* ************************************** */
1187 /* Simulation Interface - Bullet */
1188
1189 /* Update object array and rigid body count so they're in sync with the rigid body group */
1190 static void rigidbody_update_ob_array(RigidBodyWorld *rbw)
1191 {
1192         GroupObject *go;
1193         int i, n;
1194
1195         n = BLI_listbase_count(&rbw->group->gobject);
1196
1197         if (rbw->numbodies != n) {
1198                 rbw->numbodies = n;
1199                 rbw->objects = realloc(rbw->objects, sizeof(Object *) * rbw->numbodies);
1200         }
1201
1202         for (go = rbw->group->gobject.first, i = 0; go; go = go->next, i++) {
1203                 Object *ob = go->ob;
1204                 rbw->objects[i] = ob;
1205         }
1206 }
1207
1208 static void rigidbody_update_sim_world(Scene *scene, RigidBodyWorld *rbw)
1209 {
1210         float adj_gravity[3];
1211
1212         /* adjust gravity to take effector weights into account */
1213         if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
1214                 copy_v3_v3(adj_gravity, scene->physics_settings.gravity);
1215                 mul_v3_fl(adj_gravity, rbw->effector_weights->global_gravity * rbw->effector_weights->weight[0]);
1216         }
1217         else {
1218                 zero_v3(adj_gravity);
1219         }
1220
1221         /* update gravity, since this RNA setting is not part of RigidBody settings */
1222         RB_dworld_set_gravity(rbw->physics_world, adj_gravity);
1223
1224         /* update object array in case there are changes */
1225         rigidbody_update_ob_array(rbw);
1226 }
1227
1228 static void rigidbody_update_sim_ob(Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo)
1229 {
1230         float loc[3];
1231         float rot[4];
1232         float scale[3];
1233
1234         /* only update if rigid body exists */
1235         if (rbo->physics_object == NULL)
1236                 return;
1237
1238         if (rbo->shape == RB_SHAPE_TRIMESH && rbo->flag & RBO_FLAG_USE_DEFORM) {
1239                 DerivedMesh *dm = ob->derivedDeform;
1240                 if (dm) {
1241                         MVert *mvert = dm->getVertArray(dm);
1242                         int totvert = dm->getNumVerts(dm);
1243                         BoundBox *bb = BKE_object_boundbox_get(ob);
1244
1245                         RB_shape_trimesh_update(rbo->physics_shape, (float *)mvert, totvert, sizeof(MVert), bb->vec[0], bb->vec[6]);
1246                 }
1247         }
1248
1249         mat4_decompose(loc, rot, scale, ob->obmat);
1250
1251         /* update scale for all objects */
1252         RB_body_set_scale(rbo->physics_object, scale);
1253         /* compensate for embedded convex hull collision margin */
1254         if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && rbo->shape == RB_SHAPE_CONVEXH)
1255                 RB_shape_set_margin(rbo->physics_shape, RBO_GET_MARGIN(rbo) * MIN3(scale[0], scale[1], scale[2]));
1256
1257         /* make transformed objects temporarily kinmatic so that they can be moved by the user during simulation */
1258         if (ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ) {
1259                 RB_body_set_kinematic_state(rbo->physics_object, true);
1260                 RB_body_set_mass(rbo->physics_object, 0.0f);
1261         }
1262
1263         /* update rigid body location and rotation for kinematic bodies */
1264         if (rbo->flag & RBO_FLAG_KINEMATIC || (ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ)) {
1265                 RB_body_activate(rbo->physics_object);
1266                 RB_body_set_loc_rot(rbo->physics_object, loc, rot);
1267         }
1268         /* update influence of effectors - but don't do it on an effector */
1269         /* only dynamic bodies need effector update */
1270         else if (rbo->type == RBO_TYPE_ACTIVE && ((ob->pd == NULL) || (ob->pd->forcefield == PFIELD_NULL))) {
1271                 EffectorWeights *effector_weights = rbw->effector_weights;
1272                 EffectedPoint epoint;
1273                 ListBase *effectors;
1274
1275                 /* get effectors present in the group specified by effector_weights */
1276                 effectors = pdInitEffectors(scene, ob, NULL, effector_weights, true);
1277                 if (effectors) {
1278                         float eff_force[3] = {0.0f, 0.0f, 0.0f};
1279                         float eff_loc[3], eff_vel[3];
1280
1281                         /* create dummy 'point' which represents last known position of object as result of sim */
1282                         // XXX: this can create some inaccuracies with sim position, but is probably better than using unsimulated vals?
1283                         RB_body_get_position(rbo->physics_object, eff_loc);
1284                         RB_body_get_linear_velocity(rbo->physics_object, eff_vel);
1285
1286                         pd_point_from_loc(scene, eff_loc, eff_vel, 0, &epoint);
1287
1288                         /* calculate net force of effectors, and apply to sim object
1289                          *      - we use 'central force' since apply force requires a "relative position" which we don't have...
1290                          */
1291                         pdDoEffectors(effectors, NULL, effector_weights, &epoint, eff_force, NULL);
1292                         if (G.f & G_DEBUG)
1293                                 printf("\tapplying force (%f,%f,%f) to '%s'\n", eff_force[0], eff_force[1], eff_force[2], ob->id.name + 2);
1294                         /* activate object in case it is deactivated */
1295                         if (!is_zero_v3(eff_force))
1296                                 RB_body_activate(rbo->physics_object);
1297                         RB_body_apply_central_force(rbo->physics_object, eff_force);
1298                 }
1299                 else if (G.f & G_DEBUG)
1300                         printf("\tno forces to apply to '%s'\n", ob->id.name + 2);
1301
1302                 /* cleanup */
1303                 pdEndEffectors(&effectors);
1304         }
1305         /* NOTE: passive objects don't need to be updated since they don't move */
1306
1307         /* NOTE: no other settings need to be explicitly updated here,
1308          * since RNA setters take care of the rest :)
1309          */
1310 }
1311
1312 /**
1313  * Updates and validates world, bodies and shapes.
1314  *
1315  * \param rebuild Rebuild entire simulation
1316  */
1317 static void rigidbody_update_simulation(Scene *scene, RigidBodyWorld *rbw, bool rebuild)
1318 {
1319         GroupObject *go;
1320
1321         /* update world */
1322         if (rebuild)
1323                 BKE_rigidbody_validate_sim_world(scene, rbw, true);
1324         rigidbody_update_sim_world(scene, rbw);
1325
1326         /* XXX TODO For rebuild: remove all constraints first.
1327          * Otherwise we can end up deleting objects that are still
1328          * referenced by constraints, corrupting bullet's internal list.
1329          * 
1330          * Memory management needs redesign here, this is just a dirty workaround.
1331          */
1332         if (rebuild && rbw->constraints) {
1333                 for (go = rbw->constraints->gobject.first; go; go = go->next) {
1334                         Object *ob = go->ob;
1335                         if (ob) {
1336                                 RigidBodyCon *rbc = ob->rigidbody_constraint;
1337                                 if (rbc && rbc->physics_constraint) {
1338                                         RB_dworld_remove_constraint(rbw->physics_world, rbc->physics_constraint);
1339                                         RB_constraint_delete(rbc->physics_constraint);
1340                                         rbc->physics_constraint = NULL;
1341                                 }
1342                         }
1343                 }
1344         }
1345
1346         /* update objects */
1347         for (go = rbw->group->gobject.first; go; go = go->next) {
1348                 Object *ob = go->ob;
1349
1350                 if (ob && ob->type == OB_MESH) {
1351                         /* validate that we've got valid object set up here... */
1352                         RigidBodyOb *rbo = ob->rigidbody_object;
1353                         /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
1354                         BKE_object_where_is_calc(scene, ob);
1355
1356                         if (rbo == NULL) {
1357                                 /* Since this object is included in the sim group but doesn't have
1358                                  * rigid body settings (perhaps it was added manually), add!
1359                                  *      - assume object to be active? That is the default for newly added settings...
1360                                  */
1361                                 ob->rigidbody_object = BKE_rigidbody_create_object(scene, ob, RBO_TYPE_ACTIVE);
1362                                 rigidbody_validate_sim_object(rbw, ob, true);
1363
1364                                 rbo = ob->rigidbody_object;
1365                         }
1366                         else {
1367                                 /* perform simulation data updates as tagged */
1368                                 /* refresh object... */
1369                                 if (rebuild) {
1370                                         /* World has been rebuilt so rebuild object */
1371                                         rigidbody_validate_sim_object(rbw, ob, true);
1372                                 }
1373                                 else if (rbo->flag & RBO_FLAG_NEEDS_VALIDATE) {
1374                                         rigidbody_validate_sim_object(rbw, ob, false);
1375                                 }
1376                                 /* refresh shape... */
1377                                 if (rbo->flag & RBO_FLAG_NEEDS_RESHAPE) {
1378                                         /* mesh/shape data changed, so force shape refresh */
1379                                         rigidbody_validate_sim_shape(ob, true);
1380                                         /* now tell RB sim about it */
1381                                         // XXX: we assume that this can only get applied for active/passive shapes that will be included as rigidbodies
1382                                         RB_body_set_collision_shape(rbo->physics_object, rbo->physics_shape);
1383                                 }
1384                                 rbo->flag &= ~(RBO_FLAG_NEEDS_VALIDATE | RBO_FLAG_NEEDS_RESHAPE);
1385                         }
1386
1387                         /* update simulation object... */
1388                         rigidbody_update_sim_ob(scene, rbw, ob, rbo);
1389                 }
1390         }
1391         
1392         /* update constraints */
1393         if (rbw->constraints == NULL) /* no constraints, move on */
1394                 return;
1395         for (go = rbw->constraints->gobject.first; go; go = go->next) {
1396                 Object *ob = go->ob;
1397
1398                 if (ob) {
1399                         /* validate that we've got valid object set up here... */
1400                         RigidBodyCon *rbc = ob->rigidbody_constraint;
1401                         /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
1402                         BKE_object_where_is_calc(scene, ob);
1403
1404                         if (rbc == NULL) {
1405                                 /* Since this object is included in the group but doesn't have
1406                                  * constraint settings (perhaps it was added manually), add!
1407                                  */
1408                                 ob->rigidbody_constraint = BKE_rigidbody_create_constraint(scene, ob, RBC_TYPE_FIXED);
1409                                 rigidbody_validate_sim_constraint(rbw, ob, true);
1410
1411                                 rbc = ob->rigidbody_constraint;
1412                         }
1413                         else {
1414                                 /* perform simulation data updates as tagged */
1415                                 if (rebuild) {
1416                                         /* World has been rebuilt so rebuild constraint */
1417                                         rigidbody_validate_sim_constraint(rbw, ob, true);
1418                                 }
1419                                 else if (rbc->flag & RBC_FLAG_NEEDS_VALIDATE) {
1420                                         rigidbody_validate_sim_constraint(rbw, ob, false);
1421                                 }
1422                                 rbc->flag &= ~RBC_FLAG_NEEDS_VALIDATE;
1423                         }
1424                 }
1425         }
1426 }
1427
1428 static void rigidbody_update_simulation_post_step(RigidBodyWorld *rbw)
1429 {
1430         GroupObject *go;
1431
1432         for (go = rbw->group->gobject.first; go; go = go->next) {
1433                 Object *ob = go->ob;
1434
1435                 if (ob) {
1436                         RigidBodyOb *rbo = ob->rigidbody_object;
1437                         /* reset kinematic state for transformed objects */
1438                         if (rbo && (ob->flag & SELECT) && (G.moving & G_TRANSFORM_OBJ)) {
1439                                 RB_body_set_kinematic_state(rbo->physics_object, rbo->flag & RBO_FLAG_KINEMATIC || rbo->flag & RBO_FLAG_DISABLED);
1440                                 RB_body_set_mass(rbo->physics_object, RBO_GET_MASS(rbo));
1441                                 /* deactivate passive objects so they don't interfere with deactivation of active objects */
1442                                 if (rbo->type == RBO_TYPE_PASSIVE)
1443                                         RB_body_deactivate(rbo->physics_object);
1444                         }
1445                 }
1446         }
1447 }
1448
1449 bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime)
1450 {
1451         return (rbw && (rbw->flag & RBW_FLAG_MUTED) == 0 && ctime > rbw->pointcache->startframe);
1452 }
1453
1454 /* Sync rigid body and object transformations */
1455 void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime)
1456 {
1457         RigidBodyOb *rbo = ob->rigidbody_object;
1458
1459         /* keep original transform for kinematic and passive objects */
1460         if (ELEM(NULL, rbw, rbo) || rbo->flag & RBO_FLAG_KINEMATIC || rbo->type == RBO_TYPE_PASSIVE)
1461                 return;
1462
1463         /* use rigid body transform after cache start frame if objects is not being transformed */
1464         if (BKE_rigidbody_check_sim_running(rbw, ctime) && !(ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ)) {
1465                 float mat[4][4], size_mat[4][4], size[3];
1466
1467                 normalize_qt(rbo->orn); // RB_TODO investigate why quaternion isn't normalized at this point
1468                 quat_to_mat4(mat, rbo->orn);
1469                 copy_v3_v3(mat[3], rbo->pos);
1470
1471                 mat4_to_size(size, ob->obmat);
1472                 size_to_mat4(size_mat, size);
1473                 mul_m4_m4m4(mat, mat, size_mat);
1474
1475                 copy_m4_m4(ob->obmat, mat);
1476         }
1477         /* otherwise set rigid body transform to current obmat */
1478         else {
1479                 mat4_to_loc_quat(rbo->pos, rbo->orn, ob->obmat);
1480         }
1481 }
1482
1483 /* Used when canceling transforms - return rigidbody and object to initial states */
1484 void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle)
1485 {
1486         RigidBodyOb *rbo = ob->rigidbody_object;
1487         bool correct_delta = !(rbo->flag & RBO_FLAG_KINEMATIC || rbo->type == RBO_TYPE_PASSIVE);
1488
1489         /* return rigid body and object to their initial states */
1490         copy_v3_v3(rbo->pos, ob->loc);
1491         copy_v3_v3(ob->loc, loc);
1492
1493         if (correct_delta) {
1494                 add_v3_v3(rbo->pos, ob->dloc);
1495         }
1496
1497         if (ob->rotmode > 0) {
1498                 float qt[4];
1499                 eulO_to_quat(qt, ob->rot, ob->rotmode);
1500
1501                 if (correct_delta) {
1502                         float dquat[4];
1503                         eulO_to_quat(dquat, ob->drot, ob->rotmode);
1504
1505                         mul_qt_qtqt(rbo->orn, dquat, qt);
1506                 }
1507                 else {
1508                         copy_qt_qt(rbo->orn, qt);
1509                 }
1510
1511                 copy_v3_v3(ob->rot, rot);
1512         }
1513         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1514                 float qt[4];
1515                 axis_angle_to_quat(qt, ob->rotAxis, ob->rotAngle);
1516
1517                 if (correct_delta) {
1518                         float dquat[4];
1519                         axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
1520
1521                         mul_qt_qtqt(rbo->orn, dquat, qt);
1522                 }
1523                 else {
1524                         copy_qt_qt(rbo->orn, qt);
1525                 }
1526
1527                 copy_v3_v3(ob->rotAxis, rotAxis);
1528                 ob->rotAngle = rotAngle;
1529         }
1530         else {
1531                 if (correct_delta) {
1532                         mul_qt_qtqt(rbo->orn, ob->dquat, ob->quat);
1533                 }
1534                 else {
1535                         copy_qt_qt(rbo->orn, ob->quat);
1536                 }
1537
1538                 copy_qt_qt(ob->quat, quat);
1539         }
1540
1541         if (rbo->physics_object) {
1542                 /* allow passive objects to return to original transform */
1543                 if (rbo->type == RBO_TYPE_PASSIVE)
1544                         RB_body_set_kinematic_state(rbo->physics_object, true);
1545                 RB_body_set_loc_rot(rbo->physics_object, rbo->pos, rbo->orn);
1546         }
1547         // 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)
1548 }
1549
1550 void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw)
1551 {
1552         if (rbw) {
1553                 rbw->pointcache->flag |= PTCACHE_OUTDATED;
1554         }
1555 }
1556
1557 /* ------------------ */
1558
1559 /* Rebuild rigid body world */
1560 /* NOTE: this needs to be called before frame update to work correctly */
1561 void BKE_rigidbody_rebuild_world(Scene *scene, float ctime)
1562 {
1563         RigidBodyWorld *rbw = scene->rigidbody_world;
1564         PointCache *cache;
1565         PTCacheID pid;
1566         int startframe, endframe;
1567
1568         BKE_ptcache_id_from_rigidbody(&pid, NULL, rbw);
1569         BKE_ptcache_id_time(&pid, scene, ctime, &startframe, &endframe, NULL);
1570         cache = rbw->pointcache;
1571
1572         /* flag cache as outdated if we don't have a world or number of objects in the simulation has changed */
1573         if (rbw->physics_world == NULL || rbw->numbodies != BLI_listbase_count(&rbw->group->gobject)) {
1574                 cache->flag |= PTCACHE_OUTDATED;
1575         }
1576
1577         if (ctime == startframe + 1 && rbw->ltime == startframe) {
1578                 if (cache->flag & PTCACHE_OUTDATED) {
1579                         BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
1580                         rigidbody_update_simulation(scene, rbw, true);
1581                         BKE_ptcache_validate(cache, (int)ctime);
1582                         cache->last_exact = 0;
1583                         cache->flag &= ~PTCACHE_REDO_NEEDED;
1584                 }
1585         }
1586 }
1587
1588 /* Run RigidBody simulation for the specified physics world */
1589 void BKE_rigidbody_do_simulation(Scene *scene, float ctime)
1590 {
1591         float timestep;
1592         RigidBodyWorld *rbw = scene->rigidbody_world;
1593         PointCache *cache;
1594         PTCacheID pid;
1595         int startframe, endframe;
1596
1597         BKE_ptcache_id_from_rigidbody(&pid, NULL, rbw);
1598         BKE_ptcache_id_time(&pid, scene, ctime, &startframe, &endframe, NULL);
1599         cache = rbw->pointcache;
1600
1601         if (ctime <= startframe) {
1602                 rbw->ltime = startframe;
1603                 return;
1604         }
1605         /* make sure we don't go out of cache frame range */
1606         else if (ctime > endframe) {
1607                 ctime = endframe;
1608         }
1609
1610         /* don't try to run the simulation if we don't have a world yet but allow reading baked cache */
1611         if (rbw->physics_world == NULL && !(cache->flag & PTCACHE_BAKED))
1612                 return;
1613         else if (rbw->objects == NULL)
1614                 rigidbody_update_ob_array(rbw);
1615
1616         /* try to read from cache */
1617         // RB_TODO deal with interpolated, old and baked results
1618         bool can_simulate = (ctime == rbw->ltime + 1) && !(cache->flag & PTCACHE_BAKED);
1619
1620         if (BKE_ptcache_read(&pid, ctime, can_simulate) == PTCACHE_READ_EXACT) {
1621                 BKE_ptcache_validate(cache, (int)ctime);
1622                 rbw->ltime = ctime;
1623                 return;
1624         }
1625
1626         /* advance simulation, we can only step one frame forward */
1627         if (can_simulate) {
1628                 /* write cache for first frame when on second frame */
1629                 if (rbw->ltime == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact == 0)) {
1630                         BKE_ptcache_write(&pid, startframe);
1631                 }
1632
1633                 /* update and validate simulation */
1634                 rigidbody_update_simulation(scene, rbw, false);
1635
1636                 /* calculate how much time elapsed since last step in seconds */
1637                 timestep = 1.0f / (float)FPS * (ctime - rbw->ltime) * rbw->time_scale;
1638                 /* step simulation by the requested timestep, steps per second are adjusted to take time scale into account */
1639                 RB_dworld_step_simulation(rbw->physics_world, timestep, INT_MAX, 1.0f / (float)rbw->steps_per_second * min_ff(rbw->time_scale, 1.0f));
1640
1641                 rigidbody_update_simulation_post_step(rbw);
1642
1643                 /* write cache for current frame */
1644                 BKE_ptcache_validate(cache, (int)ctime);
1645                 BKE_ptcache_write(&pid, (unsigned int)ctime);
1646
1647                 rbw->ltime = ctime;
1648         }
1649 }
1650 /* ************************************** */
1651
1652 #else  /* WITH_BULLET */
1653
1654 /* stubs */
1655 #ifdef __GNUC__
1656 #  pragma GCC diagnostic push
1657 #  pragma GCC diagnostic ignored "-Wunused-parameter"
1658 #endif
1659
1660 struct RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob, const int flag) { return NULL; }
1661 struct RigidBodyCon *BKE_rigidbody_copy_constraint(const Object *ob, const int flag) { return NULL; }
1662 void BKE_rigidbody_validate_sim_world(Scene *scene, RigidBodyWorld *rbw, bool rebuild) {}
1663 void BKE_rigidbody_calc_volume(Object *ob, float *r_vol) { if (r_vol) *r_vol = 0.0f; }
1664 void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3]) { zero_v3(r_center); }
1665 struct RigidBodyWorld *BKE_rigidbody_create_world(Scene *scene) { return NULL; }
1666 struct RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw, const int flag) { return NULL; }
1667 void BKE_rigidbody_world_groups_relink(struct RigidBodyWorld *rbw) {}
1668 void BKE_rigidbody_world_id_loop(struct RigidBodyWorld *rbw, RigidbodyWorldIDFunc func, void *userdata) {}
1669 struct RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type) { return NULL; }
1670 struct RigidBodyCon *BKE_rigidbody_create_constraint(Scene *scene, Object *ob, short type) { return NULL; }
1671 struct RigidBodyWorld *BKE_rigidbody_get_world(Scene *scene) { return NULL; }
1672 void BKE_rigidbody_remove_object(Scene *scene, Object *ob) {}
1673 void BKE_rigidbody_remove_constraint(Scene *scene, Object *ob) {}
1674 void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime) {}
1675 void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle) {}
1676 bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime) { return false; }
1677 void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw) {}
1678 void BKE_rigidbody_rebuild_world(Scene *scene, float ctime) {}
1679 void BKE_rigidbody_do_simulation(Scene *scene, float ctime) {}
1680
1681 #ifdef __GNUC__
1682 #  pragma GCC diagnostic pop
1683 #endif
1684
1685 #endif  /* WITH_BULLET */
1686
1687 /* -------------------- */
1688 /* Depsgraph evaluation */
1689
1690 void BKE_rigidbody_rebuild_sim(EvaluationContext *UNUSED(eval_ctx),
1691                                Scene *scene)
1692 {
1693         float ctime = BKE_scene_frame_get(scene);
1694         DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
1695         /* rebuild sim data (i.e. after resetting to start of timeline) */
1696         if (BKE_scene_check_rigidbody_active(scene)) {
1697                 BKE_rigidbody_rebuild_world(scene, ctime);
1698         }
1699 }
1700
1701 void BKE_rigidbody_eval_simulation(EvaluationContext *UNUSED(eval_ctx),
1702                                    Scene *scene)
1703 {
1704         float ctime = BKE_scene_frame_get(scene);
1705         DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
1706         /* evaluate rigidbody sim */
1707         if (BKE_scene_check_rigidbody_active(scene)) {
1708                 BKE_rigidbody_do_simulation(scene, ctime);
1709         }
1710 }
1711
1712 void BKE_rigidbody_object_sync_transforms(EvaluationContext *UNUSED(eval_ctx),
1713                                           Scene *scene,
1714                                           Object *ob)
1715 {
1716         RigidBodyWorld *rbw = scene->rigidbody_world;
1717         float ctime = BKE_scene_frame_get(scene);
1718         DEG_debug_print_eval_time(__func__, ob->id.name, ob, ctime);
1719         /* read values pushed into RBO from sim/cache... */
1720         BKE_rigidbody_sync_transforms(rbw, ob, ctime);
1721 }