Rigidbody: Allow triangle mesh shapes to deform during simulation
Only supported when using the "Deform" mesh source.
This commit is contained in:
parent
c96601138d
commit
ceb2430dd7
|
@ -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 */
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue