[Bf-blender-cvs] [ceb2430] master: Rigidbody: Allow triangle mesh shapes to deform during simulation

Sergej Reich noreply at git.blender.org
Thu Dec 26 18:39:49 CET 2013


Commit: ceb2430dd7b54c31d267eb2be8d412e6d7f1b13a
Author: Sergej Reich
Date:   Thu Dec 26 18:15:56 2013 +0100
https://developer.blender.org/rBceb2430dd7b54c31d267eb2be8d412e6d7f1b13a

Rigidbody: Allow triangle mesh shapes to deform during simulation

Only supported when using the "Deform" mesh source.

===================================================================

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

===================================================================

diff --git a/intern/rigidbody/RBI_api.h b/intern/rigidbody/RBI_api.h
index 97e8e68..2e7a996 100644
--- a/intern/rigidbody/RBI_api.h
+++ b/intern/rigidbody/RBI_api.h
@@ -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 */
 
diff --git a/intern/rigidbody/rb_bullet_api.cpp b/intern/rigidbody/rb_bullet_api.cpp
index 31f7f38..ef35d3b 100644
--- a/intern/rigidbody/rb_bullet_api.cpp
+++ b/intern/rigidbody/rb_bullet_api.cpp
@@ -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;
diff --git a/release/scripts/startup/bl_ui/properties_physics_rigidbody.py b/release/scripts/startup/bl_ui/properties_physics_rigidbody.py
index 7a6c8e0..5f589c4 100644
--- a/release/scripts/startup/bl_ui/properties_physics_rigidbody.py
+++ b/release/scripts/startup/bl_ui/properties_physics_rigidbody.py
@@ -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()
diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index 22126b7..868fa41 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -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 */
diff --git a/source/blender/makesdna/DNA_rigidbody_types.h b/source/blender/makesdna/DNA_rigidbody_types.h
index de23a3c..5d76ffe 100644
--- a/source/blender/makesdna/DNA_rigidbody_types.h
+++ b/source/blender/makesdna/DNA_rigidbody_types.h
@@ -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 */
diff --git a/source/blender/makesrna/intern/rna_rigidbody.c b/source/blender/makesrna/intern/rna_rigidbody.c
index 34b0f6a..58fc9ab 100644
--- a/source/blender/makesrna/intern/rna_rigidbody.c
+++ b/source/blender/makesrna/intern/rna_rigidbody.c
@@ -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");




More information about the Bf-blender-cvs mailing list