Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/blenkernel/intern/rigidbody.c')
-rw-r--r--source/blender/blenkernel/intern/rigidbody.c171
1 files changed, 114 insertions, 57 deletions
diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index 78639b4ddb9..b5960962e17 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -109,8 +109,9 @@ void BKE_rigidbody_free_world(Scene *scene)
scene->rigidbody_world = NULL;
/* sanity check */
- if (!rbw)
+ if (!rbw) {
return;
+ }
if (is_orig && rbw->shared->physics_world) {
/* free physics references, we assume that all physics objects in will have been added to the world */
@@ -135,8 +136,9 @@ void BKE_rigidbody_free_world(Scene *scene)
/* free dynamics world */
RB_dworld_delete(rbw->shared->physics_world);
}
- if (rbw->objects)
+ if (rbw->objects) {
free(rbw->objects);
+ }
if (is_orig) {
/* free cache */
@@ -147,8 +149,9 @@ void BKE_rigidbody_free_world(Scene *scene)
}
/* free effector weights */
- if (rbw->effector_weights)
+ if (rbw->effector_weights) {
MEM_freeN(rbw->effector_weights);
+ }
/* free rigidbody world itself */
MEM_freeN(rbw);
@@ -161,8 +164,9 @@ void BKE_rigidbody_free_object(Object *ob, RigidBodyWorld *rbw)
RigidBodyOb *rbo = ob->rigidbody_object;
/* sanity check */
- if (rbo == NULL)
+ if (rbo == NULL) {
return;
+ }
/* free physics references */
if (is_orig) {
@@ -198,8 +202,9 @@ void BKE_rigidbody_free_constraint(Object *ob)
RigidBodyCon *rbc = (ob) ? ob->rigidbody_constraint : NULL;
/* sanity check */
- if (rbc == NULL)
+ if (rbc == NULL) {
return;
+ }
/* free physics reference */
if (rbc->physics_constraint) {
@@ -332,8 +337,9 @@ static rbCollisionShape *rigidbody_get_shape_trimesh_from_mesh(Object *ob)
mesh = rigidbody_get_mesh(ob);
/* ensure mesh validity, then grab data */
- if (mesh == NULL)
+ if (mesh == NULL) {
return NULL;
+ }
mvert = mesh->mvert;
totvert = mesh->totvert;
@@ -415,12 +421,14 @@ static void rigidbody_validate_sim_shape(Object *ob, bool rebuild)
bool has_volume;
/* sanity check */
- if (rbo == NULL)
+ if (rbo == NULL) {
return;
+ }
/* don't create a new shape if we already have one and don't want to rebuild it */
- if (rbo->shared->physics_shape && !rebuild)
+ if (rbo->shared->physics_shape && !rebuild) {
return;
+ }
/* if automatically determining dimensions, use the Object's boundbox
* - assume that all quadrics are standing upright on local z-axis
@@ -472,13 +480,15 @@ static void rigidbody_validate_sim_shape(Object *ob, bool rebuild)
/* try to emged collision margin */
has_volume = (MIN3(size[0], size[1], size[2]) > 0.0f);
- if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && has_volume)
+ if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && has_volume) {
hull_margin = 0.04f;
+ }
new_shape = rigidbody_get_shape_convexhull_from_mesh(ob, hull_margin, &can_embed);
- if (!(rbo->flag & RBO_FLAG_USE_MARGIN))
+ if (!(rbo->flag & RBO_FLAG_USE_MARGIN)) {
rbo->margin = (can_embed && has_volume) ?
0.04f :
0.0f; /* RB_TODO ideally we shouldn't directly change the margin here */
+ }
break;
case RB_SHAPE_TRIMESH:
new_shape = rigidbody_get_shape_trimesh_from_mesh(ob);
@@ -561,8 +571,9 @@ void BKE_rigidbody_calc_volume(Object *ob, float *r_vol)
const MLoop *mloop = NULL;
/* ensure mesh validity, then grab data */
- if (mesh == NULL)
+ if (mesh == NULL) {
return;
+ }
mvert = mesh->mvert;
totvert = mesh->totvert;
@@ -584,8 +595,9 @@ void BKE_rigidbody_calc_volume(Object *ob, float *r_vol)
}
/* return the volume calculated */
- if (r_vol)
+ if (r_vol) {
*r_vol = volume;
+ }
}
void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3])
@@ -633,8 +645,9 @@ void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3])
const MLoop *mloop;
/* ensure mesh validity, then grab data */
- if (mesh == NULL)
+ if (mesh == NULL) {
return;
+ }
mvert = mesh->mvert;
totvert = mesh->totvert;
@@ -667,13 +680,15 @@ static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool
/* sanity checks:
* - object doesn't have RigidBody info already: then why is it here?
*/
- if (rbo == NULL)
+ if (rbo == NULL) {
return;
+ }
/* make sure collision shape exists */
/* FIXME we shouldn't always have to rebuild collision shapes when rebuilding objects, but it's needed for constraints to update correctly */
- if (rbo->shared->physics_shape == NULL || rebuild)
+ if (rbo->shared->physics_shape == NULL || rebuild) {
rigidbody_validate_sim_shape(ob, true);
+ }
if (rbo->shared->physics_object) {
RB_dworld_remove_body(rbw->shared->physics_world, rbo->shared->physics_object);
@@ -697,8 +712,9 @@ static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool
RB_body_set_activation_state(rbo->shared->physics_object,
rbo->flag & RBO_FLAG_USE_DEACTIVATION);
- if (rbo->type == RBO_TYPE_PASSIVE || rbo->flag & RBO_FLAG_START_DEACTIVATED)
+ if (rbo->type == RBO_TYPE_PASSIVE || rbo->flag & RBO_FLAG_START_DEACTIVATED) {
RB_body_deactivate(rbo->shared->physics_object);
+ }
RB_body_set_linear_factor(rbo->shared->physics_object,
(ob->protectflag & OB_LOCK_LOCX) == 0,
@@ -714,8 +730,9 @@ static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool
rbo->flag & RBO_FLAG_KINEMATIC || rbo->flag & RBO_FLAG_DISABLED);
}
- if (rbw && rbw->shared->physics_world)
+ if (rbw && rbw->shared->physics_world) {
RB_dworld_add_body(rbw->shared->physics_world, rbo->shared->physics_object, rbo->col_groups);
+ }
}
/* --------------------- */
@@ -753,41 +770,53 @@ static void rigidbody_constraint_init_spring(RigidBodyCon *rbc,
static void rigidbody_constraint_set_limits(RigidBodyCon *rbc,
void (*set_limits)(rbConstraint *, int, float, float))
{
- if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
+ 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
+ }
+ else {
set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
+ }
- if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
+ 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
+ }
+ else {
set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
+ }
- if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
+ 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
+ }
+ else {
set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
+ }
- if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
+ 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
+ }
+ else {
set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
+ }
- if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
+ 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
+ }
+ else {
set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
+ }
- if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
+ 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
+ }
+ else {
set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
+ }
}
/**
@@ -851,16 +880,19 @@ static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, b
RB_constraint_set_limits_hinge(
rbc->physics_constraint, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
}
- else
+ else {
RB_constraint_set_limits_hinge(rbc->physics_constraint, 0.0f, -1.0f);
+ }
break;
case RBC_TYPE_SLIDER:
rbc->physics_constraint = RB_constraint_new_slider(loc, rot, rb1, rb2);
- if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
+ if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X) {
RB_constraint_set_limits_slider(
rbc->physics_constraint, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
- else
+ }
+ else {
RB_constraint_set_limits_slider(rbc->physics_constraint, 0.0f, -1.0f);
+ }
break;
case RBC_TYPE_PISTON:
rbc->physics_constraint = RB_constraint_new_piston(loc, rot, rb1, rb2);
@@ -934,15 +966,19 @@ static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, b
RB_constraint_set_enabled(rbc->physics_constraint, rbc->flag & RBC_FLAG_ENABLED);
- if (rbc->flag & RBC_FLAG_USE_BREAKING)
+ if (rbc->flag & RBC_FLAG_USE_BREAKING) {
RB_constraint_set_breaking_threshold(rbc->physics_constraint, rbc->breaking_threshold);
- else
+ }
+ else {
RB_constraint_set_breaking_threshold(rbc->physics_constraint, FLT_MAX);
+ }
- if (rbc->flag & RBC_FLAG_OVERRIDE_SOLVER_ITERATIONS)
+ if (rbc->flag & RBC_FLAG_OVERRIDE_SOLVER_ITERATIONS) {
RB_constraint_set_solver_iterations(rbc->physics_constraint, rbc->num_solver_iterations);
- else
+ }
+ else {
RB_constraint_set_solver_iterations(rbc->physics_constraint, -1);
+ }
}
if (rbw && rbw->shared->physics_world && rbc->physics_constraint) {
@@ -959,13 +995,15 @@ static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, b
void BKE_rigidbody_validate_sim_world(Scene *scene, RigidBodyWorld *rbw, bool rebuild)
{
/* sanity checks */
- if (rbw == NULL)
+ if (rbw == NULL) {
return;
+ }
/* create new sim world */
if (rebuild || rbw->shared->physics_world == NULL) {
- if (rbw->shared->physics_world)
+ if (rbw->shared->physics_world) {
RB_dworld_delete(rbw->shared->physics_world);
+ }
rbw->shared->physics_world = RB_dworld_new(scene->physics_settings.gravity);
}
@@ -986,8 +1024,9 @@ RigidBodyWorld *BKE_rigidbody_create_world(Scene *scene)
* - there must be a valid scene to add world to
* - there mustn't be a sim world using this group already
*/
- if (scene == NULL)
+ if (scene == NULL) {
return NULL;
+ }
/* create a new sim world */
rbw = MEM_callocN(sizeof(RigidBodyWorld), "RigidBodyWorld");
@@ -1068,8 +1107,9 @@ RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type)
* - object must exist
* - cannot add rigid body if it already exists
*/
- if (ob == NULL || (ob->rigidbody_object != NULL))
+ if (ob == NULL || (ob->rigidbody_object != NULL)) {
return NULL;
+ }
/* create new settings data, and link it up */
rbo = MEM_callocN(sizeof(RigidBodyOb), "RigidBodyOb");
@@ -1096,10 +1136,12 @@ RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type)
/* use triangle meshes for passive objects
* use convex hulls for active objects since dynamic triangle meshes are very unstable
*/
- if (type == RBO_TYPE_ACTIVE)
+ if (type == RBO_TYPE_ACTIVE) {
rbo->shape = RB_SHAPE_CONVEXH;
- else
+ }
+ else {
rbo->shape = RB_SHAPE_TRIMESH;
+ }
rbo->mesh_source = RBO_MESH_DEFORM;
@@ -1125,8 +1167,9 @@ RigidBodyCon *BKE_rigidbody_create_constraint(Scene *scene, Object *ob, short ty
* - object must exist
* - cannot add constraint if it already exists
*/
- if (ob == NULL || (ob->rigidbody_constraint != NULL))
+ if (ob == NULL || (ob->rigidbody_constraint != NULL)) {
return NULL;
+ }
/* create new settings data, and link it up */
rbc = MEM_callocN(sizeof(RigidBodyCon), "RigidBodyCon");
@@ -1240,8 +1283,9 @@ void BKE_rigidbody_main_collection_object_add(Main *bmain, Collection *collectio
RigidBodyWorld *BKE_rigidbody_get_world(Scene *scene)
{
/* sanity check */
- if (scene == NULL)
+ if (scene == NULL) {
return NULL;
+ }
return scene->rigidbody_world;
}
@@ -1363,8 +1407,9 @@ static void rigidbody_update_sim_ob(
float scale[3];
/* only update if rigid body exists */
- if (rbo->shared->physics_object == NULL)
+ if (rbo->shared->physics_object == NULL) {
return;
+ }
if (rbo->shape == RB_SHAPE_TRIMESH && rbo->flag & RBO_FLAG_USE_DEFORM) {
Mesh *mesh = ob->runtime.mesh_deform_eval;
@@ -1387,9 +1432,10 @@ static void rigidbody_update_sim_ob(
/* update scale for all objects */
RB_body_set_scale(rbo->shared->physics_object, scale);
/* compensate for embedded convex hull collision margin */
- if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && rbo->shape == RB_SHAPE_CONVEXH)
+ if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && rbo->shape == RB_SHAPE_CONVEXH) {
RB_shape_set_margin(rbo->shared->physics_shape,
RBO_GET_MARGIN(rbo) * MIN3(scale[0], scale[1], scale[2]));
+ }
/* make transformed objects temporarily kinmatic so that they can be moved by the user during simulation */
if (ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ) {
@@ -1427,19 +1473,22 @@ static void rigidbody_update_sim_ob(
* - we use 'central force' since apply force requires a "relative position" which we don't have...
*/
BKE_effectors_apply(effectors, NULL, effector_weights, &epoint, eff_force, NULL);
- if (G.f & G_DEBUG)
+ if (G.f & G_DEBUG) {
printf("\tapplying force (%f,%f,%f) to '%s'\n",
eff_force[0],
eff_force[1],
eff_force[2],
ob->id.name + 2);
+ }
/* activate object in case it is deactivated */
- if (!is_zero_v3(eff_force))
+ if (!is_zero_v3(eff_force)) {
RB_body_activate(rbo->shared->physics_object);
+ }
RB_body_apply_central_force(rbo->shared->physics_object, eff_force);
}
- else if (G.f & G_DEBUG)
+ else if (G.f & G_DEBUG) {
printf("\tno forces to apply to '%s'\n", ob->id.name + 2);
+ }
/* cleanup */
BKE_effectors_free(effectors);
@@ -1464,8 +1513,9 @@ static void rigidbody_update_simulation(Depsgraph *depsgraph,
float ctime = DEG_get_ctime(depsgraph);
/* update world */
- if (rebuild)
+ if (rebuild) {
BKE_rigidbody_validate_sim_world(scene, rbw, true);
+ }
rigidbody_update_sim_world(scene, rbw);
/* XXX TODO For rebuild: remove all constraints first.
@@ -1538,8 +1588,9 @@ static void rigidbody_update_simulation(Depsgraph *depsgraph,
FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
/* update constraints */
- if (rbw->constraints == NULL) /* no constraints, move on */
+ if (rbw->constraints == NULL) { /* no constraints, move on */
return;
+ }
FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN (rbw->constraints, ob) {
/* validate that we've got valid object set up here... */
@@ -1587,8 +1638,9 @@ static void rigidbody_update_simulation_post_step(Depsgraph *depsgraph, RigidBod
rbo->flag & RBO_FLAG_KINEMATIC || rbo->flag & RBO_FLAG_DISABLED);
RB_body_set_mass(rbo->shared->physics_object, RBO_GET_MASS(rbo));
/* Deactivate passive objects so they don't interfere with deactivation of active objects. */
- if (rbo->type == RBO_TYPE_PASSIVE)
+ if (rbo->type == RBO_TYPE_PASSIVE) {
RB_body_deactivate(rbo->shared->physics_object);
+ }
}
}
FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
@@ -1605,8 +1657,9 @@ void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime)
RigidBodyOb *rbo = ob->rigidbody_object;
/* keep original transform for kinematic and passive objects */
- if (ELEM(NULL, rbw, rbo) || rbo->flag & RBO_FLAG_KINEMATIC || rbo->type == RBO_TYPE_PASSIVE)
+ if (ELEM(NULL, rbw, rbo) || rbo->flag & RBO_FLAG_KINEMATIC || rbo->type == RBO_TYPE_PASSIVE) {
return;
+ }
/* use rigid body transform after cache start frame if objects is not being transformed */
if (BKE_rigidbody_check_sim_running(rbw, ctime) &&
@@ -1690,8 +1743,9 @@ void BKE_rigidbody_aftertrans_update(
if (rbo->shared->physics_object) {
/* allow passive objects to return to original transform */
- if (rbo->type == RBO_TYPE_PASSIVE)
+ if (rbo->type == RBO_TYPE_PASSIVE) {
RB_body_set_kinematic_state(rbo->shared->physics_object, true);
+ }
RB_body_set_loc_rot(rbo->shared->physics_object, rbo->pos, rbo->orn);
}
// RB_TODO update rigid body physics object's loc/rot for dynamic objects here as well (needs to be done outside bullet's update loop)
@@ -1765,10 +1819,12 @@ void BKE_rigidbody_do_simulation(Depsgraph *depsgraph, Scene *scene, float ctime
}
/* don't try to run the simulation if we don't have a world yet but allow reading baked cache */
- if (rbw->shared->physics_world == NULL && !(cache->flag & PTCACHE_BAKED))
+ if (rbw->shared->physics_world == NULL && !(cache->flag & PTCACHE_BAKED)) {
return;
- else if (rbw->objects == NULL)
+ }
+ else if (rbw->objects == NULL) {
rigidbody_update_ob_array(rbw);
+ }
/* try to read from cache */
// RB_TODO deal with interpolated, old and baked results
@@ -1836,8 +1892,9 @@ void BKE_rigidbody_validate_sim_world(Scene *scene, RigidBodyWorld *rbw, bool re
}
void BKE_rigidbody_calc_volume(Object *ob, float *r_vol)
{
- if (r_vol)
+ if (r_vol) {
*r_vol = 0.0f;
+ }
}
void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3])
{