Rigidbody: Allow triangle mesh shapes to deform during simulation
authorSergej Reich <sergej.reich@googlemail.com>
Thu, 26 Dec 2013 17:15:56 +0000 (18:15 +0100)
committerSergej Reich <sergej.reich@googlemail.com>
Thu, 26 Dec 2013 17:38:06 +0000 (18:38 +0100)
Only supported when using the "Deform" mesh source.

intern/rigidbody/RBI_api.h
intern/rigidbody/rb_bullet_api.cpp
release/scripts/startup/bl_ui/properties_physics_rigidbody.py
source/blender/blenkernel/intern/rigidbody.c
source/blender/makesdna/DNA_rigidbody_types.h
source/blender/makesrna/intern/rna_rigidbody.c

index 97e8e68..2e7a996 100644 (file)
@@ -247,6 +247,8 @@ extern void RB_shape_delete(rbCollisionShape *shape);
 extern float RB_shape_get_margin(rbCollisionShape *shape);
 extern void RB_shape_set_margin(rbCollisionShape *shape, float value);
 
+extern void RB_shape_trimesh_update(rbCollisionShape *shape, float *vertices, int num_verts, int vert_stride, float min[3], float max[3]);
+
 /* ********************************** */
 /* Constraints */
 
index 31f7f38..ef35d3b 100644 (file)
@@ -763,6 +763,29 @@ rbCollisionShape *RB_shape_new_trimesh(rbMeshData *mesh)
        return shape;
 }
 
+void RB_shape_trimesh_update(rbCollisionShape *shape, float *vertices, int num_verts, int vert_stride, float min[3], float max[3])
+{
+       if (shape->mesh == NULL || num_verts != shape->mesh->num_vertices)
+               return;
+       
+       for (int i = 0; i < num_verts; i++) {
+               float *vert = (float*)(((char*)vertices + i * vert_stride));
+               shape->mesh->vertices[i].x = vert[0];
+               shape->mesh->vertices[i].y = vert[1];
+               shape->mesh->vertices[i].z = vert[2];
+       }
+       
+       if (shape->cshape->getShapeType() == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE) {
+               btScaledBvhTriangleMeshShape *scaled_shape = (btScaledBvhTriangleMeshShape *)shape->cshape;
+               btBvhTriangleMeshShape *mesh_shape = scaled_shape->getChildShape();
+               mesh_shape->refitTree(btVector3(min[0], min[1], min[2]), btVector3(max[0], max[1], max[2]));
+       }
+       else if (shape->cshape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE) {
+               btGImpactMeshShape *mesh_shape = (btGImpactMeshShape*)shape->cshape;
+               mesh_shape->updateBound();
+       }
+}
+
 rbCollisionShape *RB_shape_new_gimpact_mesh(rbMeshData *mesh)
 {
        rbCollisionShape *shape = new rbCollisionShape;
index 7a6c8e0..5f589c4 100644 (file)
@@ -73,6 +73,9 @@ class PHYSICS_PT_rigid_body_collisions(PHYSICS_PT_rigidbody_panel, Panel):
         if rbo.collision_shape in {'MESH', 'CONVEX_HULL'}:
             layout.prop(rbo, "mesh_source", text="Source")
 
+        if rbo.collision_shape == 'MESH' and rbo.mesh_source == 'DEFORM':
+            layout.prop(rbo, "use_deform", text="Deforming")
+
         split = layout.split()
 
         col = split.column()
index 22126b7..868fa41 100644 (file)
@@ -1030,6 +1030,17 @@ static void rigidbody_update_sim_ob(Scene *scene, RigidBodyWorld *rbw, Object *o
        if (rbo->physics_object == NULL)
                return;
 
+       if (rbo->shape == RB_SHAPE_TRIMESH && rbo->flag & RBO_FLAG_USE_DEFORM) {
+               DerivedMesh *dm = ob->derivedDeform;
+               if (dm) {
+                       MVert *mvert = dm->getVertArray(dm);
+                       int totvert = dm->getNumVerts(dm);
+                       BoundBox *bb = BKE_object_boundbox_get(ob);
+
+                       RB_shape_trimesh_update(rbo->physics_shape, (float*)mvert, totvert, sizeof(MVert), bb->vec[0], bb->vec[6]);
+               }
+       }
+
        mat4_decompose(loc, rot, scale, ob->obmat);
 
        /* update scale for all objects */
index de23a3c..5d76ffe 100644 (file)
@@ -149,7 +149,9 @@ typedef enum eRigidBodyOb_Flag {
        /* rigidbody is not dynamically simulated */
        RBO_FLAG_DISABLED                       = (1 << 5),
        /* collision margin is not embedded (only used by convex hull shapes for now) */
-       RBO_FLAG_USE_MARGIN                     = (1 << 6)
+       RBO_FLAG_USE_MARGIN                     = (1 << 6),
+       /* collision shape deforms during simulation (only for passive triangle mesh shapes) */
+       RBO_FLAG_USE_DEFORM                     = (1 << 7)
 } eRigidBodyOb_Flag;
 
 /* RigidBody Collision Shape */
index 34b0f6a..58fc9ab 100644 (file)
@@ -804,6 +804,11 @@ static void rna_def_rigidbody_object(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Kinematic", "Allow rigid body to be controlled by the animation system");
        RNA_def_property_update(prop, NC_OBJECT | ND_POINTCACHE, "rna_RigidBodyOb_reset");
        
+       prop = RNA_def_property(srna, "use_deform", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "flag", RBO_FLAG_USE_DEFORM);
+       RNA_def_property_ui_text(prop, "Deforming", "Rigid body deforms during simulation");
+       RNA_def_property_update(prop, NC_OBJECT | ND_POINTCACHE, "rna_RigidBodyOb_reset");
+       
        /* Physics Parameters */
        prop = RNA_def_property(srna, "mass", PROP_FLOAT, PROP_UNIT_MASS);
        RNA_def_property_float_sdna(prop, NULL, "mass");