[Bf-blender-cvs] [e2d7123] particles_refactor: Separation of concerns: Moved functions for building object rigid bodies and applying Bullet results to them into a dedicated file.

Lukas Tönne noreply at git.blender.org
Tue Apr 22 12:07:09 CEST 2014


Commit: e2d7123674528a816fcd5bc11374aa85c505600d
Author: Lukas Tönne
Date:   Tue Apr 1 17:11:22 2014 +0200
https://developer.blender.org/rBe2d7123674528a816fcd5bc11374aa85c505600d

Separation of concerns: Moved functions for building object rigid bodies
and applying Bullet results to them into a dedicated file.

The same should happen for particles, shapes, constraints etc. later.

The purpose of this is to keep the main Bullet world API limited to
dealing with Bullet primarily and avoid fixating it on Object DNA data.

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

M	source/blender/blenkernel/BKE_rigidbody.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/rigidbody.c
A	source/blender/blenkernel/intern/rigidbody_objects.c

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

diff --git a/source/blender/blenkernel/BKE_rigidbody.h b/source/blender/blenkernel/BKE_rigidbody.h
index 53d03c1..1b5f390 100644
--- a/source/blender/blenkernel/BKE_rigidbody.h
+++ b/source/blender/blenkernel/BKE_rigidbody.h
@@ -36,6 +36,7 @@
 
 struct RigidBodyWorld;
 struct RigidBodyOb;
+struct rbRigidBody;
 
 struct Scene;
 struct Object;
@@ -70,6 +71,9 @@ struct RigidBodyWorld *BKE_rigidbody_world_copy(struct RigidBodyWorld *rbw);
 void BKE_rigidbody_world_groups_relink(struct RigidBodyWorld *rbw);
 
 /* 'validate' (i.e. make new or replace old) Physics-Engine objects */
+struct rbRigidBody *BKE_rigidbody_body_ensure_alloc(struct RigidBodyWorld *rbw, struct rbRigidBody *body, bool rebuild);
+void BKE_rigidbody_body_tag_used(struct rbRigidBody *body);
+void BKE_rigidbody_validate_sim_shape(Object *ob, bool rebuild);
 void BKE_rigidbody_validate_sim_world(struct Scene *scene, struct RigidBodyWorld *rbw, bool rebuild);
 
 /* -------------- */
@@ -99,4 +103,11 @@ void BKE_rigidbody_cache_reset(struct RigidBodyWorld *rbw);
 void BKE_rigidbody_rebuild_world(struct Scene *scene, float ctime);
 void BKE_rigidbody_do_simulation(struct Scene *scene, float ctime);
 
+
+/* -------------- */
+/* rigidbody_objects.c */
+
+void BKE_rigidbody_objects_build(struct Scene *scene, struct RigidBodyWorld *rbw, bool rebuild);
+void BKE_rigidbody_objects_apply(struct Scene *scene, struct RigidBodyWorld *rbw);
+
 #endif /* __BKE_RIGIDBODY_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 46287d2..a2eed19 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -139,6 +139,7 @@ set(SRC
 	intern/property.c
 	intern/report.c
 	intern/rigidbody.c
+	intern/rigidbody_objects.c
 	intern/sca.c
 	intern/scene.c
 	intern/screen.c
diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index c26886d..a5e2e70 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -261,6 +261,31 @@ void BKE_rigidbody_relink_constraint(RigidBodyCon *rbc)
 /* ************************************** */
 /* Setup Utilities - Validate Sim Instances */
 
+rbRigidBody *BKE_rigidbody_body_ensure_alloc(RigidBodyWorld *rbw, rbRigidBody *body, bool rebuild)
+{
+	if (!body || rebuild) {
+		if (body) {
+			/* free the existing rigid body, memory reused below */
+			RB_body_free(body);
+		}
+		else {
+			/* only allocate if no rigid body exists yet,
+			 * otherwise previous memory is reused
+			 */
+			body = BLI_mempool_alloc(rbw->body_pool);
+		}
+	}
+	else
+		RB_dworld_remove_body(rbw->physics_world, body);
+	return body;
+}
+
+void BKE_rigidbody_body_tag_used(rbRigidBody *body)
+{
+	if (body)
+		RB_body_set_flag(body, RB_BODY_USED);
+}
+
 /* get the appropriate DerivedMesh based on rigid body mesh source */
 static DerivedMesh *rigidbody_get_mesh(Object *ob)
 {
@@ -400,7 +425,7 @@ static rbCollisionShape *rigidbody_get_shape_trimesh_from_mesh(Object *ob)
 /* Create new physics sim collision shape for object and store it,
  * or remove the existing one first and replace...
  */
-static void rigidbody_validate_sim_shape(Object *ob, bool rebuild)
+void BKE_rigidbody_validate_sim_shape(Object *ob, bool rebuild)
 {
 	RigidBodyOb *rbo = ob->rigidbody_object;
 	rbCollisionShape *new_shape = NULL;
@@ -491,84 +516,12 @@ static void rigidbody_validate_sim_shape(Object *ob, bool rebuild)
 	/* use box shape if we can't fall back to old shape */
 	else if (rbo->physics_shape == NULL) {
 		rbo->shape = RB_SHAPE_BOX;
-		rigidbody_validate_sim_shape(ob, true);
+		BKE_rigidbody_validate_sim_shape(ob, true);
 	}
 }
 
 /* --------------------- */
 
-/* Create physics sim representation of object given RigidBody settings
- * < rebuild: even if an instance already exists, replace it
- */
-static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool rebuild)
-{
-	RigidBodyOb *rbo = (ob) ? ob->rigidbody_object : NULL;
-	rbRigidBody *body;
-	float loc[3];
-	float rot[4];
-
-	/* sanity checks:
-	 *	- object doesn't have RigidBody info already: then why is it here?
-	 */
-	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->physics_shape == NULL || rebuild)
-		rigidbody_validate_sim_shape(ob, true);
-
-	if (!rbo->physics_object || rebuild) {
-		if (rbo->physics_object) {
-			/* remove rigid body if it already exists before creating a new one
-			 * no need to call BLI_mempool_free, we can reuse the memory right away
-			 */
-			RB_body_free(rbo->physics_object);
-		}
-		else {
-			/* only allocate if no rigid body exists yet,
-			 * otherwise previous memory is reused
-			 */
-			rbo->physics_object = BLI_mempool_alloc(rbw->body_pool);
-		}
-		body = rbo->physics_object;
-
-		mat4_to_loc_quat(loc, rot, ob->obmat);
-
-		RB_body_init(body, rbo->physics_shape, loc, rot);
-
-		RB_body_set_friction(body, rbo->friction);
-		RB_body_set_restitution(body, rbo->restitution);
-
-		RB_body_set_damping(body, rbo->lin_damping, rbo->ang_damping);
-		RB_body_set_sleep_thresh(body, rbo->lin_sleep_thresh, rbo->ang_sleep_thresh);
-		RB_body_set_activation_state(body, rbo->flag & RBO_FLAG_USE_DEACTIVATION);
-
-		if (rbo->type == RBO_TYPE_PASSIVE || rbo->flag & RBO_FLAG_START_DEACTIVATED)
-			RB_body_deactivate(body);
-
-
-		RB_body_set_linear_factor(body,
-		                          (ob->protectflag & OB_LOCK_LOCX) == 0,
-		                          (ob->protectflag & OB_LOCK_LOCY) == 0,
-		                          (ob->protectflag & OB_LOCK_LOCZ) == 0);
-		RB_body_set_angular_factor(body,
-		                           (ob->protectflag & OB_LOCK_ROTX) == 0,
-		                           (ob->protectflag & OB_LOCK_ROTY) == 0,
-		                           (ob->protectflag & OB_LOCK_ROTZ) == 0);
-
-		RB_body_set_mass(body, RBO_GET_MASS(rbo));
-		RB_body_set_kinematic_state(body, rbo->flag & RBO_FLAG_KINEMATIC || rbo->flag & RBO_FLAG_DISABLED);
-	}
-	else
-		RB_dworld_remove_body(rbw->physics_world, rbo->physics_object);
-
-	if (rbw && rbw->physics_world)
-		RB_dworld_add_body(rbw->physics_world, rbo->physics_object, rbo->col_groups);
-}
-
-/* --------------------- */
-
 static rbRigidBody *rigidbody_validate_particle(RigidBodyWorld *rbw, Object *UNUSED(ob),
                                                 NParticleDisplayDupliObject *dupli, int totdupli,
                                                 NParticleIterator *iter, bool rebuild)
@@ -590,7 +543,7 @@ static rbRigidBody *rigidbody_validate_particle(RigidBodyWorld *rbw, Object *UNU
 	/* 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 (dob->object->rigidbody_object->physics_shape == NULL || rebuild)
-		rigidbody_validate_sim_shape(dob->object, true);
+		BKE_rigidbody_validate_sim_shape(dob->object, true);
 	shape = dob->object->rigidbody_object->physics_shape;
 	
 	BKE_nparticle_iter_get_vector(iter, "position", loc);
@@ -1191,91 +1144,7 @@ static void rigidbody_sync_world(Scene *scene, RigidBodyWorld *rbw)
 	rigidbody_update_ob_array(rbw);
 }
 
-static void rigidbody_sync_object(Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo)
-{
-	float loc[3];
-	float rot[4];
-	float scale[3];
-
-	/* only update if rigid body exists */
-	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 */
-	RB_body_set_scale(rbo->physics_object, scale);
-	/* compensate for embedded convex hull collision margin */
-	if (!(rbo->flag & RBO_FLAG_USE_MARGIN) && rbo->shape == RB_SHAPE_CONVEXH)
-		RB_shape_set_margin(rbo->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) {
-		RB_body_set_kinematic_state(rbo->physics_object, true);
-		RB_body_set_mass(rbo->physics_object, 0.0f);
-	}
-
-	/* update rigid body location and rotation for kinematic bodies */
-	if (rbo->flag & RBO_FLAG_KINEMATIC || (ob->flag & SELECT && G.moving & G_TRANSFORM_OBJ)) {
-		RB_body_activate(rbo->physics_object);
-		RB_body_set_loc_rot(rbo->physics_object, loc, rot);
-	}
-	/* update influence of effectors - but don't do it on an effector */
-	/* only dynamic bodies need effector update */
-	else if (rbo->type == RBO_TYPE_ACTIVE && ((ob->pd == NULL) || (ob->pd->forcefield == PFIELD_NULL))) {
-		EffectorWeights *effector_weights = rbw->effector_weights;
-		EffectedPoint epoint;
-		ListBase *effectors;
-
-		/* get effectors present in the group specified by effector_weights */
-		effectors = pdInitEffectors(scene, ob, NULL, effector_weights, true);
-		if (effectors) {
-			float eff_force[3] = {0.0f, 0.0f, 0.0f};
-			float eff_loc[3], eff_vel[3];
-
-			/* create dummy 'point' which represents last known position of object as result of sim */
-			// XXX: this can create some inaccuracies with sim position, but is probably better than using unsimulated vals?
-			RB_body_get_position(rbo->physics_object, eff_loc);
-			RB_body_get_linear_velocity(rbo->physics_object, eff_vel);
-
-			pd_point_from_loc(scene, eff_loc, eff_vel, 0, &epoint);
-
-			/* calculate net force of effectors, and apply to sim object
-			 *	- we use 'central force' since apply force requires a "relative position" which we don't have...
-			 */
-			pdDoEffectors(effectors, NULL, effector_weights, &epoint, eff_force, NULL);
-			if (G.f & G_DEBUG)
-				printf("\tapplying force (%f,%f,%f) to '%s'\n", eff_force[0]

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list