Use the newer version of the bullet 6dof spring constraint for rigidbody.
authorAlexander Gavrilov <angavrilov@gmail.com>
Sat, 31 Mar 2018 09:08:16 +0000 (12:08 +0300)
committerAlexander Gavrilov <angavrilov@gmail.com>
Mon, 9 Apr 2018 16:14:35 +0000 (19:14 +0300)
The new constraint is slower and not backward compatible, but should
be better, especially in the damping side. The new constraint also
has a different valid range of the damping coefficient, and a limit
implementation that bounces instead of making the object stationary.

Reviewers: sergof

Differential Revision: https://developer.blender.org/D3125

intern/rigidbody/RBI_api.h
intern/rigidbody/rb_bullet_api.cpp
source/blender/blenkernel/intern/rigidbody.c
source/blender/makesrna/intern/rna_rigidbody.c

index 688ca91..556ca95 100644 (file)
@@ -299,6 +299,7 @@ void RB_constraint_set_limits_piston(rbConstraint *con, float lin_lower, float l
 void RB_constraint_set_limits_6dof(rbConstraint *con, int axis, float lower, float upper);
 
 /* 6dof spring specific */
+void RB_constraint_set_limits_6dof_spring(rbConstraint *con, int axis, float lower, float upper);
 void RB_constraint_set_stiffness_6dof_spring(rbConstraint *con, int axis, float stiffness);
 void RB_constraint_set_damping_6dof_spring(rbConstraint *con, int axis, float damping);
 void RB_constraint_set_spring_6dof_spring(rbConstraint *con, int axis, int enable);
index 17bb381..a9fbcb2 100644 (file)
@@ -954,7 +954,7 @@ rbConstraint *RB_constraint_new_6dof_spring(float pivot[3], float orn[4], rbRigi
        
        make_constraint_transforms(transform1, transform2, body1, body2, pivot, orn);
        
-       btTypedConstraint *con = new btGeneric6DofSpringConstraint(*body1, *body2, transform1, transform2, true);
+       btTypedConstraint *con = new btGeneric6DofSpring2Constraint(*body1, *body2, transform1, transform2);
        
        return (rbConstraint *)con;
 }
@@ -1034,32 +1034,38 @@ void RB_constraint_set_limits_6dof(rbConstraint *con, int axis, float lower, flo
        constraint->setLimit(axis, lower, upper);
 }
 
+void RB_constraint_set_limits_6dof_spring(rbConstraint *con, int axis, float lower, float upper)
+{
+       btGeneric6DofSpring2Constraint *constraint = reinterpret_cast<btGeneric6DofSpring2Constraint*>(con);
+
+       constraint->setLimit(axis, lower, upper);
+}
+
 void RB_constraint_set_stiffness_6dof_spring(rbConstraint *con, int axis, float stiffness)
 {
-       btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
-       
+       btGeneric6DofSpring2Constraint *constraint = reinterpret_cast<btGeneric6DofSpring2Constraint*>(con);
+
        constraint->setStiffness(axis, stiffness);
 }
 
 void RB_constraint_set_damping_6dof_spring(rbConstraint *con, int axis, float damping)
 {
-       btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
-       
-       // invert damping range so that 0 = no damping
-       constraint->setDamping(axis, 1.0f - damping);
+       btGeneric6DofSpring2Constraint *constraint = reinterpret_cast<btGeneric6DofSpring2Constraint*>(con);
+
+       constraint->setDamping(axis, damping);
 }
 
 void RB_constraint_set_spring_6dof_spring(rbConstraint *con, int axis, int enable)
 {
-       btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
-       
+       btGeneric6DofSpring2Constraint *constraint = reinterpret_cast<btGeneric6DofSpring2Constraint*>(con);
+
        constraint->enableSpring(axis, enable);
 }
 
 void RB_constraint_set_equilibrium_6dof_spring(rbConstraint *con)
 {
-       btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
-       
+       btGeneric6DofSpring2Constraint *constraint = reinterpret_cast<btGeneric6DofSpring2Constraint*>(con);
+
        constraint->setEquilibriumPoint();
 }
 
index c10f54a..b8873cc 100644 (file)
@@ -700,6 +700,39 @@ static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool
 
 /* --------------------- */
 
+static void rigidbody_constraint_set_limits(RigidBodyCon *rbc, void (*set_limits)(rbConstraint*,int,float,float))
+{
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
+}
+
 /**
  * Create physics sim representation of constraint given rigid body constraint settings
  *
@@ -818,40 +851,13 @@ static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, b
                                        RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->spring_damping_ang_z);
 
                                        RB_constraint_set_equilibrium_6dof_spring(rbc->physics_constraint);
-                                       ATTR_FALLTHROUGH;
-                               case RBC_TYPE_6DOF:
-                                       if (rbc->type == RBC_TYPE_6DOF) /* a litte awkward but avoids duplicate code for limits */
-                                               rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
 
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
+                                       rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof_spring);
+                                       break;
+                               case RBC_TYPE_6DOF:
+                                       rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
 
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
+                                       rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof);
                                        break;
                                case RBC_TYPE_MOTOR:
                                        rbc->physics_constraint = RB_constraint_new_motor(loc, rot, rb1, rb2);
index 188200e..4c3301d 100644 (file)
@@ -1293,49 +1293,49 @@ static void rna_def_rigidbody_constraint(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Z Angle Stiffness", "Stiffness on the Z rotational axis");
        RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
 
-       prop = RNA_def_property(srna, "spring_damping_x", PROP_FLOAT, PROP_FACTOR);
+       prop = RNA_def_property(srna, "spring_damping_x", PROP_FLOAT, PROP_NONE);
        RNA_def_property_float_sdna(prop, NULL, "spring_damping_x");
-       RNA_def_property_range(prop, 0.0f, 1.0f);
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
        RNA_def_property_float_default(prop, 0.5f);
        RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_damping_x_set", NULL);
        RNA_def_property_ui_text(prop, "Damping X", "Damping on the X axis");
        RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
 
-       prop = RNA_def_property(srna, "spring_damping_y", PROP_FLOAT, PROP_FACTOR);
+       prop = RNA_def_property(srna, "spring_damping_y", PROP_FLOAT, PROP_NONE);
        RNA_def_property_float_sdna(prop, NULL, "spring_damping_y");
-       RNA_def_property_range(prop, 0.0f, 1.0f);
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
        RNA_def_property_float_default(prop, 0.5f);
        RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_damping_y_set", NULL);
        RNA_def_property_ui_text(prop, "Damping Y", "Damping on the Y axis");
        RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
 
-       prop = RNA_def_property(srna, "spring_damping_z", PROP_FLOAT, PROP_FACTOR);
+       prop = RNA_def_property(srna, "spring_damping_z", PROP_FLOAT, PROP_NONE);
        RNA_def_property_float_sdna(prop, NULL, "spring_damping_z");
-       RNA_def_property_range(prop, 0.0f, 1.0f);
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
        RNA_def_property_float_default(prop, 0.5f);
        RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_damping_z_set", NULL);
        RNA_def_property_ui_text(prop, "Damping Z", "Damping on the Z axis");
        RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
 
-       prop = RNA_def_property(srna, "spring_damping_ang_x", PROP_FLOAT, PROP_FACTOR);
+       prop = RNA_def_property(srna, "spring_damping_ang_x", PROP_FLOAT, PROP_NONE);
        RNA_def_property_float_sdna(prop, NULL, "spring_damping_ang_x");
-       RNA_def_property_range(prop, 0.0f, 1.0f);
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
        RNA_def_property_float_default(prop, 0.5f);
        RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_damping_ang_x_set", NULL);
        RNA_def_property_ui_text(prop, "Damping X Angle", "Damping on the X rotational axis");
        RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
 
-       prop = RNA_def_property(srna, "spring_damping_ang_y", PROP_FLOAT, PROP_FACTOR);
+       prop = RNA_def_property(srna, "spring_damping_ang_y", PROP_FLOAT, PROP_NONE);
        RNA_def_property_float_sdna(prop, NULL, "spring_damping_ang_y");
-       RNA_def_property_range(prop, 0.0f, 1.0f);
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
        RNA_def_property_float_default(prop, 0.5f);
        RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_damping_ang_y_set", NULL);
        RNA_def_property_ui_text(prop, "Damping Y Angle", "Damping on the Y rotational axis");
        RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
 
-       prop = RNA_def_property(srna, "spring_damping_ang_z", PROP_FLOAT, PROP_FACTOR);
+       prop = RNA_def_property(srna, "spring_damping_ang_z", PROP_FLOAT, PROP_NONE);
        RNA_def_property_float_sdna(prop, NULL, "spring_damping_ang_z");
-       RNA_def_property_range(prop, 0.0f, 1.0f);
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
        RNA_def_property_float_default(prop, 0.5f);
        RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_damping_ang_z_set", NULL);
        RNA_def_property_ui_text(prop, "Damping Z Angle", "Damping on the Z rotational axis");