[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29764] branches/soc-2010-aligorith-2/ source/blender: Bullet SoC - Some tweaks/fixes

Joshua Leung aligorith at gmail.com
Mon Jun 28 14:14:14 CEST 2010


Revision: 29764
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29764
Author:   aligorith
Date:     2010-06-28 14:14:14 +0200 (Mon, 28 Jun 2010)

Log Message:
-----------
Bullet SoC - Some tweaks/fixes

Still trying to get to the bottom of a few annoying bugs. In the meantime, worked on a few tweaks to diagnose other problems.

* Added handling for resetting sims. Currently isn't working for undiagnosed reasons yet.
* Figured out how to implement a few settings (angular/linear damping and sleeping thresholds). These turned out to only be settable together in Bullet-API, but here I've allowed to set separately too for more consistent API.
* Tweaked way that 'rbSetTransformMatrix' was working, since the old code wasn't actually setting the intended matrices at all

Modified Paths:
--------------
    branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c
    branches/soc-2010-aligorith-2/source/blender/makesdna/DNA_rigidbody_types.h
    branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h
    branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp

Modified: branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c	2010-06-28 12:10:40 UTC (rev 29763)
+++ branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c	2010-06-28 12:14:14 UTC (rev 29764)
@@ -244,6 +244,9 @@
 	/* set default settings */
 	rbw->group = group;
 	
+	rbw->ltime = CFRA - 1;
+	rbw->sframe = PSFRA;
+	
 	// XXX: review these
 	rbw->max_timestep = 10;
 	rbw->substeps = 5;
@@ -361,6 +364,37 @@
 /* ************************************** */
 /* Simulation Interface - Bullet */
 
+static void rigidbody_update_sim_world (Scene *scene, RigidBodyWorld *rbw)
+{
+	/* update gravity */
+	rbSetGravity(rbw->physics_world, scene->physics_settings.gravity);
+}
+
+static void rigidbody_update_sim_ob (Object *ob, RigidBodyOb *rbo)
+{
+	/* give Bullet our current (entire) transform matrix
+	 *	- we seem to use "OpenGL" convention, while Bullet uses Right-Handed coordinate system
+	 *	- Bullet can't cope with scaling in transforms, so need to supply a normalised copy?
+	 */
+	// XXX: the standard object matrix has scaling included... for now, just make sure no scaling goes through...
+	//rbSetTransformMatrix(rbo->physics_object, ob->obmat);
+}
+
+/* .......... */
+
+static void rigidbody_reset_sim_ob (Object *ob, RigidBodyOb *rbo)
+{
+	const float zero_vec[3] = {0,0,0};
+	
+	/* clear velocities */
+	rbBodySetLinearVelocity(rbo->physics_object, zero_vec);
+	rbBodySetAngularVelocity(rbo->physics_object, zero_vec);
+	
+	// TODO: clear forces too?
+}
+
+/* ------------------ */
+
 /* Run RigidBody simulation for the specified physics world */
 void BKE_rigidbody_do_simulation (Scene *scene, RigidBodyWorld *rbw, float ctime)
 {
@@ -394,12 +428,24 @@
 	}
 #endif
 	
+	/* set flag to reset simulation if we haven't reached the start frame yet 
+	 * but don't do it again if last frame already needed reset
+	 */
+	if ((ctime <= rbw->sframe) && (rbw->ltime > rbw->sframe)) {
+		//if (G.f & G_DEBUG)
+			printf("Need to reset sim\n");
+		rbw->recalc |= RBW_RECALC_RESET;
+	}
+	
 	/* if flag to rebuild physics worlds is set, we must clear and then rebuild all relevant links */
-	// TODO: how can we fit the reset into something like this too?
 	if ((rbw->recalc & RBW_RECALC_REBUILD) || (rbw->physics_world == NULL)) {
 		/* rebuild physics world */
 		rigidbody_validate_sim_world(scene, rbw);
 	}
+	else {
+		/* update gravity and other scene-settings that sim uses */
+		rigidbody_update_sim_world(scene, rbw);
+	}
 	
 	/* flush current scene settings to the physics world */
 	for (go = rbw->group->gobject.first; go; go = go->next) {
@@ -421,13 +467,13 @@
 				rigidbody_validate_sim_ob(ob, rbo, 1);
 				rigidbody_add_sim_ob(rbw, rbo);
 			}
+			else if (rbw->recalc & RBW_RECALC_RESET) {
+				/* reset simulation internals to allow a fresh run */
+				rigidbody_reset_sim_ob(ob, rbo);
+			}
 			
-			/* give Bullet our entire transform matrix
-			 *	- we seem to use "OpenGL" convention, while Bullet uses Right-Handed coordinate system
-			 *	- Bullet can't cope with scaling in transforms, so need to supply a normalised copy?
-			 */
-			// XXX: the standard object matrix has scaling included... for now, just make sure no scaling goes through...
-			rbSetTransformMatrix(rbo->physics_object, ob->obmat);
+			/* update simulation object... */
+			rigidbody_update_sim_ob(ob, rbo);
 		}
 	}
 	
@@ -440,28 +486,33 @@
 	printf("\ttimestep = %f \n", timestep);
 	printf("\n");
 	
-	/* perform one step of sim */
-	rbStepSimulation(rbw->physics_world, timestep);
 	
-	
-	/* grab current scene settings from the physics world */
-	// TODO: could some of this be automated using motionstates?
-	for (go = rbw->group->gobject.first; go; go = go->next) {
-		Object *ob = go->ob;
+	/* only update if time changed (to avoid problems with velocities getting distorted) */
+	if (IS_EQ(timestep, 0.0f) == 0) {
+		/* perform one step of sim */
+		rbStepSimulation(rbw->physics_world, timestep);
 		
-		if (ob) {
-			RigidBodyOb *rbo = ob->rigidbodySettings;
+		/* grab current scene settings from the physics world */
+		// TODO: could some of this be automated using motionstates?
+		for (go = rbw->group->gobject.first; go; go = go->next) {
+			Object *ob = go->ob;
 			
-			/* get transform matrix from Bullet */
-			// XXX: if we supplied a normalised copy to Bullet, we'll need to reapply scale here...
-			rbGetTransformMatrix(rbo->physics_object, ob->obmat);
-			
-			// DEBUG
-			printf("\t%s: new pos - %f %f %f \n", ob->id.name+2, ob->obmat[3][0], ob->obmat[3][1], ob->obmat[3][2]);
+			if (ob) {
+				RigidBodyOb *rbo = ob->rigidbodySettings;
+				
+				/* get transform matrix from Bullet */
+				// XXX: if we supplied a normalised copy to Bullet, we'll need to reapply scale here...
+				rbGetTransformMatrix(rbo->physics_object, ob->obmat);
+				
+				// DEBUG
+				printf("\t%s: new pos - %f %f %f \n", ob->id.name+2, ob->obmat[3][0], ob->obmat[3][1], ob->obmat[3][2]);
+			}
+			else
+				printf("\tgroup object invalid %p \n", go->ob);
 		}
-		else
-			printf("\tgroup object invalid %p \n", go->ob);
 	}
+	else
+		printf("\ttimestep hasn't changed... skipping\n");
 	
 	/* perform tidy-up actions */
 	rbw->ltime = ctime;

Modified: branches/soc-2010-aligorith-2/source/blender/makesdna/DNA_rigidbody_types.h
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/makesdna/DNA_rigidbody_types.h	2010-06-28 12:10:40 UTC (rev 29763)
+++ branches/soc-2010-aligorith-2/source/blender/makesdna/DNA_rigidbody_types.h	2010-06-28 12:14:14 UTC (rev 29764)
@@ -80,6 +80,8 @@
 	RBW_RECALC_REQUIRED		= (1<<0),
 	/* sim data needs to be rebuilt (usually after file reload) */
 	RBW_RECALC_REBUILD		= (1<<1),
+	/* sim needs to be reset (usually after jumping to before/on the 'start frame', i.e. when playback ends/loops) */
+	RBW_RECALC_RESET		= (1<<2),
 } eRigidBodyWorld_Recalc;
 
 /* ******************************** */

Modified: branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h	2010-06-28 12:10:40 UTC (rev 29763)
+++ branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h	2010-06-28 12:14:14 UTC (rev 29764)
@@ -126,22 +126,24 @@
 extern float rbBodyGetRestitution(rbRigidBody *body);
 extern void rbBodySetRestitution(rbRigidBody *body, float value);
 
-/* Linear Damping */
+/* Damping */
 extern float rbBodyGetLinearDamping(rbRigidBody *body);
 extern void rbBodySetLinearDamping(rbRigidBody *body, float value);
 
-/* Angular Damping */
 extern float rbBodyGetAngularDamping(rbRigidBody *body);
 extern void rbBodySetAngularDamping(rbRigidBody *body, float value);
 
-/* Linear Sleep Threshold */
+extern void rbBodySetDamping(rbRigidBody *object, float linear, float angular);
+
+/* Sleeping Thresholds */
 extern float rbBodyGetLinearSleepThresh(rbRigidBody *body);
 extern void rbBodySetLinearSleepThresh(rbRigidBody *body, float value);
 
-/* Angular Sleep Threshold */
 extern float rbBodyGetAngularSleepThresh(rbRigidBody *body);
 extern void rbBodySetAngularSleepThresh(rbRigidBody *body, float value);
 
+extern void rbBodySetSleepThresh(rbRigidBody *body, float linear, float angular);
+
 /* ............ */
 
 /* Linear Velocity */

Modified: branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp	2010-06-28 12:10:40 UTC (rev 29763)
+++ branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp	2010-06-28 12:14:14 UTC (rev 29764)
@@ -128,7 +128,8 @@
 	btDynamicsWorld *dynamicsWorld = reinterpret_cast<btDynamicsWorld*>(world);
 	
 	// TODO: allow the other settings to be set too?
-	dynamicsWorld->stepSimulation(timeStep);
+	//dynamicsWorld->stepSimulation(timeStep);
+	dynamicsWorld->stepSimulation(timeStep, 10);
 }
 
 /* ********************************** */
@@ -280,11 +281,10 @@
 
 void rbBodySetLinearDamping(rbRigidBody *object, float value)
 {
-	//btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
-	//body->setLinearDamping(value);
+	btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
+	rbBodySetDamping(object, value, rbBodyGetLinearDamping(object));
 }
 
-
 float rbBodyGetAngularDamping(rbRigidBody *object)
 {
 	btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
@@ -293,11 +293,16 @@
 
 void rbBodySetAngularDamping(rbRigidBody *object, float value)
 {
-	//btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
-	//body->setAngularDamping(value);
+	rbBodySetDamping(object, rbBodyGetLinearDamping(object), value);
 }
 
+void rbBodySetDamping(rbRigidBody *object, float linear, float angular)
+{
+	btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
+	body->setDamping(linear, angular);
+}
 
+
 float rbBodyGetLinearSleepThresh(rbRigidBody *object)
 {
 	btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
@@ -306,11 +311,9 @@
 
 void rbBodySetLinearSleepThresh(rbRigidBody *object, float value)
 {
-	//btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
-	//body->setLinearSleepingThreshold(value);
+	rbBodySetSleepThresh(object, value, rbBodyGetAngularSleepThresh(object));
 }
 
-
 float rbBodyGetAngularSleepThresh(rbRigidBody *object)
 {
 	btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
@@ -319,10 +322,15 @@
 
 void rbBodySetAngularSleepThresh(rbRigidBody *object, float value)
 {
-	//btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
-	//body->setAngularSleepingThreshold(value);
+	rbBodySetSleepThresh(object, rbBodyGetLinearSleepThresh(object), value);
 }
 
+void rbBodySetSleepThresh(rbRigidBody *object, float linear, float angular)
+{
+	btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
+	body->setSleepingThresholds(linear, angular);
+} 
+
 /* ............ */
 
 void rbBodyGetLinearVelocity(rbRigidBody *object, float v_out[3])
@@ -397,10 +405,11 @@
 	btRigidBody *body = reinterpret_cast<btRigidBody*>(object);
 	btMotionState *ms = body->getMotionState();
 	
-	btTransform worldTrans;
-	ms->getWorldTransform(worldTrans);
+	btTransform trans;
+	trans.setFromOpenGLMatrix((const btScalar*)m_in);
 	
-	worldTrans.setFromOpenGLMatrix((const btScalar*)m_in);

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list