rigidbody: Add generic spring constraint
authorSergej Reich <sergej.reich@googlemail.com>
Wed, 23 Jan 2013 05:57:01 +0000 (05:57 +0000)
committerSergej Reich <sergej.reich@googlemail.com>
Wed, 23 Jan 2013 05:57:01 +0000 (05:57 +0000)
Behaves like the generic constraint but has optional spring on each axis.

TODO: Add option to set rest length.

Patch by Markus Kasten (markus111), thanks!

release/scripts/startup/bl_operators/rigidbody.py
source/blender/blenkernel/intern/rigidbody.c
source/blender/makesdna/DNA_rigidbody_types.h
source/blender/makesrna/intern/rna_rigidbody.c

index 5363eba..d5e6148 100644 (file)
@@ -206,6 +206,7 @@ class ConnectRigidBodies(Operator):
                ('SLIDER', "Slider", "Restricts rigid boddy translation to one axis"),
                ('PISTON', "Piston", "Restricts rigid boddy translation and rotation to one axis"),
                ('GENERIC', "Generic", "Restricts translation and rotation to specified axes"),
+               ('GENERIC_SPRING', "Generic Spring", "Restricts translation and rotation to specified axes with springs")),
         default='FIXED',)
 
     pivot_type = EnumProperty(
index d4a7013..4f154bc 100644 (file)
@@ -569,8 +569,26 @@ void BKE_rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, shor
                                        }
                                        RB_constraint_set_limits_piston(rbc->physics_constraint, lin_lower, lin_upper, ang_lower, ang_upper);
                                        break;
+                               case RBC_TYPE_6DOF_SPRING:
+                                       rbc->physics_constraint = RB_constraint_new_6dof_spring(loc, rot, rb1, rb2);
+
+                                       RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->flag & RBC_FLAG_USE_SPRING_X);
+                                       RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->spring_stiffness_x);
+                                       RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->spring_damping_x);
+
+                                       RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->flag & RBC_FLAG_USE_SPRING_Y);
+                                       RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->spring_stiffness_y);
+                                       RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->spring_damping_y);
+
+                                       RB_constraint_set_spring_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->flag & RBC_FLAG_USE_SPRING_Z);
+                                       RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->spring_stiffness_z);
+                                       RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->spring_damping_z);
+
+                                       RB_constraint_set_equilibrium_6dof_spring(rbc->physics_constraint);
+                               /* fall through */
                                case RBC_TYPE_6DOF:
-                                       rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
+                                       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);
@@ -775,6 +793,13 @@ RigidBodyCon *BKE_rigidbody_create_constraint(Scene *scene, Object *ob, short ty
        rbc->limit_ang_z_lower = -M_PI_4;
        rbc->limit_ang_z_upper = M_PI_4;
 
+       rbc->spring_damping_x = 0.5f;
+       rbc->spring_damping_y = 0.5f;
+       rbc->spring_damping_z = 0.5f;
+       rbc->spring_stiffness_x = 10.0f;
+       rbc->spring_stiffness_y = 10.0f;
+       rbc->spring_stiffness_z = 10.0f;
+
        /* flag cache as outdated */
        BKE_rigidbody_cache_reset(rbw);
 
index 5b7ffe4..e24b24d 100644 (file)
@@ -207,6 +207,15 @@ typedef struct RigidBodyCon {
        float limit_ang_z_lower;        /* lower limit for z axis rotation */
        float limit_ang_z_upper;        /* upper limit for z axis rotation */
 
+       /* spring settings */
+       /* RB_TODO document spring properties */
+       float spring_stiffness_x;
+       float spring_stiffness_y;
+       float spring_stiffness_z;
+       float spring_damping_x;
+       float spring_damping_y;
+       float spring_damping_z;
+
        /* References to Physics Sim object. Exist at runtime only */
        void *physics_constraint;       /* Physics object representation (i.e. btTypedConstraint) */
 } RigidBodyCon;
@@ -257,6 +266,10 @@ typedef enum eRigidBodyCon_Flag {
        RBC_FLAG_USE_LIMIT_ANG_X                        = (1<<8),
        RBC_FLAG_USE_LIMIT_ANG_Y                        = (1<<9),
        RBC_FLAG_USE_LIMIT_ANG_Z                        = (1<<10),
+       /* springs */
+       RBC_FLAG_USE_SPRING_X                           = (1<<11),
+       RBC_FLAG_USE_SPRING_Y                           = (1<<12),
+       RBC_FLAG_USE_SPRING_Z                           = (1<<13)
 } eRigidBodyCon_Flag;
 
 /* ******************************** */
index 206c6e1..2c8eafb 100644 (file)
@@ -67,6 +67,7 @@ EnumPropertyItem rigidbody_con_type_items[] = {
        {RBC_TYPE_SLIDER, "SLIDER", ICON_FORCE_FORCE, "Slider", "Restricts rigid boddy translation to one axis"},
        {RBC_TYPE_PISTON, "PISTON", ICON_FORCE_FORCE, "Piston", "Restricts rigid boddy translation and rotation to one axis"},
        {RBC_TYPE_6DOF, "GENERIC", ICON_FORCE_FORCE, "Generic", "Restricts translation and rotation to specified axes"},
+       {RBC_TYPE_6DOF_SPRING, "GENERIC_SPRING", ICON_FORCE_FORCE, "Generic Spring", "Restricts translation and rotation to specified axes with springs"},
        {0, NULL, 0, NULL, NULL}};
 
 
@@ -381,6 +382,66 @@ static void rna_RigidBodyCon_num_solver_iterations_set(PointerRNA *ptr, int valu
                RB_constraint_set_solver_iterations(rbc->physics_constraint, value);
 }
 
+static void rna_RigidBodyCon_spring_stiffness_x_set(PointerRNA *ptr, float value)
+{
+       RigidBodyCon *rbc = (RigidBodyCon *)ptr->data;
+
+       rbc->spring_stiffness_x = value;
+
+       if (rbc->physics_constraint && rbc->type == RBC_TYPE_6DOF_SPRING && (rbc->flag & RBC_FLAG_USE_SPRING_X))
+               RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, value);
+}
+
+static void rna_RigidBodyCon_spring_stiffness_y_set(PointerRNA *ptr, float value)
+{
+       RigidBodyCon *rbc = (RigidBodyCon *)ptr->data;
+
+       rbc->spring_stiffness_y = value;
+
+       if (rbc->physics_constraint && rbc->type == RBC_TYPE_6DOF_SPRING && (rbc->flag & RBC_FLAG_USE_SPRING_Y))
+               RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, value);
+}
+
+static void rna_RigidBodyCon_spring_stiffness_z_set(PointerRNA *ptr, float value)
+{
+       RigidBodyCon *rbc = (RigidBodyCon *)ptr->data;
+
+       rbc->spring_stiffness_z = value;
+
+       if (rbc->physics_constraint && rbc->type == RBC_TYPE_6DOF_SPRING && (rbc->flag & RBC_FLAG_USE_SPRING_Z))
+               RB_constraint_set_stiffness_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, value);
+}
+
+static void rna_RigidBodyCon_spring_damping_x_set(PointerRNA *ptr, float value)
+{
+       RigidBodyCon *rbc = (RigidBodyCon *)ptr->data;
+
+       rbc->spring_damping_x = value;
+
+       if (rbc->physics_constraint && rbc->type == RBC_TYPE_6DOF_SPRING && (rbc->flag & RBC_FLAG_USE_SPRING_X))
+               RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_X, value);
+}
+
+static void rna_RigidBodyCon_spring_damping_y_set(PointerRNA *ptr, float value)
+{
+       RigidBodyCon *rbc = (RigidBodyCon *)ptr->data;
+
+       rbc->spring_damping_y = value;
+
+       if (rbc->physics_constraint && rbc->type == RBC_TYPE_6DOF_SPRING && (rbc->flag & RBC_FLAG_USE_SPRING_Y))
+               RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Y, value);
+}
+
+static void rna_RigidBodyCon_spring_damping_z_set(PointerRNA *ptr, float value)
+{
+       RigidBodyCon *rbc = (RigidBodyCon *)ptr->data;
+
+       rbc->spring_damping_z = value;
+
+       if (rbc->physics_constraint && rbc->type == RBC_TYPE_6DOF_SPRING && (rbc->flag & RBC_FLAG_USE_SPRING_Z))
+               RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_LIN_Z, value);
+}
+
 #else
 
 static void rna_def_rigidbody_world(BlenderRNA *brna)
@@ -710,6 +771,21 @@ static void rna_def_rigidbody_constraint(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Z Angle", "Limits rotation around z axis");
        RNA_def_property_update(prop, NC_OBJECT | ND_DRAW, "rna_RigidBodyOb_reset");
 
+       prop = RNA_def_property(srna, "use_spring_x", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "flag", RBC_FLAG_USE_SPRING_X);
+       RNA_def_property_ui_text(prop, "X Spring", "Enables spring on X axis");
+       RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
+
+       prop = RNA_def_property(srna, "use_spring_y", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "flag", RBC_FLAG_USE_SPRING_Y);
+       RNA_def_property_ui_text(prop, "Y Spring", "Enables spring on Y axis");
+       RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
+
+       prop = RNA_def_property(srna, "use_spring_z", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "flag", RBC_FLAG_USE_SPRING_Z);
+       RNA_def_property_ui_text(prop, "Z Spring", "Enables spring on Z axis");
+       RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
+
        prop = RNA_def_property(srna, "limit_lin_x_lower", PROP_FLOAT, PROP_UNIT_LENGTH);
        RNA_def_property_float_sdna(prop, NULL, "limit_lin_x_lower");
        RNA_def_property_float_default(prop, -1.0f);
@@ -787,6 +863,57 @@ static void rna_def_rigidbody_constraint(BlenderRNA *brna)
        RNA_def_property_float_default(prop, M_PI_4);
        RNA_def_property_ui_text(prop, "Upper Z Angle Limit", "Upper limit of z axis rotation");
        RNA_def_property_update(prop, NC_OBJECT | ND_DRAW, "rna_RigidBodyOb_reset");
+
+       prop = RNA_def_property(srna, "spring_stiffness_x", PROP_FLOAT, PROP_UNIT_LENGTH);
+       RNA_def_property_float_sdna(prop, NULL, "spring_stiffness_x");
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
+       RNA_def_property_ui_range(prop, 0.0f, 100.0f, 1, 3);
+       RNA_def_property_float_default(prop, 10.0f);
+       RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_stiffness_x_set", NULL);
+       RNA_def_property_ui_text(prop, "X Axis Stiffness", "Stiffness on the X axis");
+       RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
+
+       prop = RNA_def_property(srna, "spring_stiffness_y", PROP_FLOAT, PROP_UNIT_LENGTH);
+       RNA_def_property_float_sdna(prop, NULL, "spring_stiffness_y");
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
+       RNA_def_property_ui_range(prop, 0.0f, 100.0f, 1, 3);
+       RNA_def_property_float_default(prop, 10.0f);
+       RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_stiffness_y_set", NULL);
+       RNA_def_property_ui_text(prop, "Y Axis Stiffness", "Stiffness on the Y axis");
+       RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
+
+       prop = RNA_def_property(srna, "spring_stiffness_z", PROP_FLOAT, PROP_UNIT_LENGTH);
+       RNA_def_property_float_sdna(prop, NULL, "spring_stiffness_z");
+       RNA_def_property_range(prop, 0.0f, FLT_MAX);
+       RNA_def_property_ui_range(prop, 0.0f, 100.0f, 1, 3);
+       RNA_def_property_float_default(prop, 10.0f);
+       RNA_def_property_float_funcs(prop, NULL, "rna_RigidBodyCon_spring_stiffness_z_set", NULL);
+       RNA_def_property_ui_text(prop, "Z Axis Stiffness", "Stiffness on the Z axis");
+       RNA_def_property_update(prop, NC_OBJECT, "rna_RigidBodyOb_reset");
+
+       prop = RNA_def_property(srna, "spring_damping_x", PROP_FLOAT, PROP_FACTOR);
+       RNA_def_property_float_sdna(prop, NULL, "spring_damping_x");
+       RNA_def_property_range(prop, 0.0f, 1.0f);
+       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);
+       RNA_def_property_float_sdna(prop, NULL, "spring_damping_y");
+       RNA_def_property_range(prop, 0.0f, 1.0f);
+       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);
+       RNA_def_property_float_sdna(prop, NULL, "spring_damping_z");
+       RNA_def_property_range(prop, 0.0f, 1.0f);
+       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");
 }
 
 void RNA_def_rigidbody(BlenderRNA *brna)