[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31128] branches/soc-2010-aligorith-2: Bullet SoC - Force Fields Support
Joshua Leung
aligorith at gmail.com
Sat Aug 7 01:27:31 CEST 2010
Revision: 31128
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31128
Author: aligorith
Date: 2010-08-07 01:27:31 +0200 (Sat, 07 Aug 2010)
Log Message:
-----------
Bullet SoC - Force Fields Support
This commit adds a third type of Rigid Body participant: "Effector". This type of Rigid Body only serves to act as a way for Blender Force Fields to be included in a Rigid Body Simulation; that is, only Force Fields which have been added to a Rigid Body sim will be considered.
Unfortunately, getting these effectors to do what you want still seems a bit fickle, though I have had a few successes so far. It probably needs a few more tweaks before it works correctly. We'll see...
Modified Paths:
--------------
branches/soc-2010-aligorith-2/release/scripts/ui/properties_physics_rigidbody.py
branches/soc-2010-aligorith-2/release/scripts/ui/space_view3d_toolbar.py
branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c
branches/soc-2010-aligorith-2/source/blender/blenloader/intern/readfile.c
branches/soc-2010-aligorith-2/source/blender/blenloader/intern/writefile.c
branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_object.c
branches/soc-2010-aligorith-2/source/blender/makesdna/DNA_rigidbody_types.h
branches/soc-2010-aligorith-2/source/blender/makesrna/intern/rna_rigidbody.c
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/release/scripts/ui/properties_physics_rigidbody.py
===================================================================
--- branches/soc-2010-aligorith-2/release/scripts/ui/properties_physics_rigidbody.py 2010-08-06 23:16:31 UTC (rev 31127)
+++ branches/soc-2010-aligorith-2/release/scripts/ui/properties_physics_rigidbody.py 2010-08-06 23:27:31 UTC (rev 31128)
@@ -73,7 +73,8 @@
bl_label = "Rigid Body Collisions"
def poll(self, context):
- return (context.object and context.object.rigid_body)
+ return (context.object and context.object.rigid_body and
+ context.object.rigid_body.type != 'EFFECTOR')
def draw(self, context):
layout = self.layout
Modified: branches/soc-2010-aligorith-2/release/scripts/ui/space_view3d_toolbar.py
===================================================================
--- branches/soc-2010-aligorith-2/release/scripts/ui/space_view3d_toolbar.py 2010-08-06 23:16:31 UTC (rev 31127)
+++ branches/soc-2010-aligorith-2/release/scripts/ui/space_view3d_toolbar.py 2010-08-06 23:27:31 UTC (rev 31128)
@@ -78,6 +78,8 @@
row.operator("rigidbody.objects_add", text="Add Active").type = 'ACTIVE'
row.operator("rigidbody.objects_add", text="Add Passive").type = 'PASSIVE'
row = col.row()
+ row.operator("rigidbody.objects_add", text="Add Effector").type = 'EFFECTOR'
+ row = col.row()
row.operator("rigidbody.objects_remove", text="Remove")
col = layout.column(align=True)
Modified: branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c 2010-08-06 23:16:31 UTC (rev 31127)
+++ branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c 2010-08-06 23:27:31 UTC (rev 31128)
@@ -45,12 +45,14 @@
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
+#include "DNA_object_force.h"
#include "DNA_rigidbody_types.h"
#include "DNA_scene_types.h"
#include "BKE_animsys.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_DerivedMesh.h"
+#include "BKE_effect.h"
#include "BKE_group.h"
#include "BKE_object.h"
#include "BKE_mesh.h"
@@ -89,6 +91,10 @@
}
rbDWorldDelete(rbw->physics_world);
+ /* free effector weights */
+ if (rbw->effector_weights)
+ MEM_freeN(rbw->effector_weights);
+
/* free rigidbody world itself */
BLI_freelinkN(&scene->rigidbody_worlds, rbw);
}
@@ -120,11 +126,15 @@
return;
/* free physics references */
- rbBodyDelete(rbo->physics_object);
- rbo->physics_object = NULL;
+ if (rbo->physics_object) {
+ rbBodyDelete(rbo->physics_object);
+ rbo->physics_object = NULL;
+ }
- rbShapeDelete(rbo->physics_shape);
- rbo->physics_shape = NULL;
+ if (rbo->physics_shape) {
+ rbShapeDelete(rbo->physics_shape);
+ rbo->physics_shape = NULL;
+ }
/* free data itself */
MEM_freeN(rbo);
@@ -326,9 +336,11 @@
{
RigidBodyOb *rbo = (ob) ? ob->rigidbodySettings : NULL;
- /* sanity checks */
- // TODO: should effectors also be added just like everything else? how will they work?
- if (rbo == NULL)
+ /* sanity checks:
+ * - object doesn't have RigidBody info already: then why is it here?
+ * - "effectors" shouldn't actually be represented
+ */
+ if ((rbo == NULL) || (rbo->type == RBO_TYPE_EFFECTOR))
return;
/* make sure collision shape exists */
@@ -400,6 +412,7 @@
BLI_addtail(&scene->rigidbody_worlds, rbw);
/* set default settings */
+ rbw->effector_weights = BKE_add_effector_weights(group);
rbw->group = group;
rbw->ltime = CFRA - 1;
@@ -535,19 +548,53 @@
static void rigidbody_update_sim_world (Scene *scene, RigidBodyWorld *rbw)
{
/* update gravity, since this RNA setting is not part of RigidBody settings */
- // TODO: this might change cache status if we stepped backwards then did this...
rbSetGravity(rbw->physics_world, scene->physics_settings.gravity);
+
+ /* update effector weights group */
+ // NOTE: this is the easiest way we can update on group changing and also file reload
+ if (rbw->effector_weights)
+ rbw->effector_weights->group = rbw->group;
}
-static void rigidbody_update_sim_ob (Object *ob, RigidBodyOb *rbo)
+static void rigidbody_update_sim_ob (Scene *scene, RigidBodyWorld *rbw, 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);
+ /* update influence of effectors - but don't do it on an effector */
+ if ((ob->pd == NULL) || (ob->pd->forcefield == PFIELD_NULL)) {
+ EffectorWeights *effector_weights = rbw->effector_weights;
+ EffectedPoint epoint;
+ ListBase *effectors = pdInitEffectors(scene, ob, NULL, effector_weights);
+ float loc[3], vel[3];
+ float force[3];
+
+ /* create dummy 'point' which represents last known position of object as result of sim */
+ // WARN: this can create some inaccuracies with sim position, but is probably better than using unsimulated vals?
+#if 1 // XXX: this is the 'safe' version for now (using object info), but really should try grabbing from sim... need api calls
+ VECCOPY(loc, ob->obmat[3]);
+ vel[0]=vel[1]=vel[2]= 1.0f;
+#endif
+ pd_point_from_loc(scene, loc, 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, force, NULL);
+ printf("\tapplying force (%f,%f,%f) to '%s'\n", force[0],force[1],force[2],ob->id.name+2);
+ rbBodyApplyCentralForce(rbo->physics_object, force);
+
+ /* cleanup */
+ pdEndEffectors(&effectors);
+ }
+ /* update transform but only if there is rigidbody data */
+ if (rbo->physics_object) {
+ /* 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);
+ }
+
/* NOTE: no other settings need to be explicitly updated here,
* since RNA setters take care of the rest :)
*/
@@ -674,6 +721,7 @@
* RigidBody settings (perhaps it was added manually), add!
* - assume object to be active? That is the default for newly added settings...
*/
+ // XXX: this doesn't hold for effectors...
BKE_rigidbody_create_object(rbw, ob, RBO_TYPE_ACTIVE);
BKE_rigidbody_validate_sim_object(rbw, ob, 1);
@@ -685,7 +733,7 @@
}
/* update simulation object... */
- rigidbody_update_sim_ob(ob, rbo);
+ rigidbody_update_sim_ob(scene, rbw, ob, rbo);
}
}
@@ -708,15 +756,17 @@
rbStepSimulationAdv(rbw->physics_world, timestep, rbw->max_substeps, rbw->fixed_timestep);
/* grab current scene state 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;
if (ob) {
RigidBodyOb *rbo = ob->rigidbodySettings;
- /* store new transforms, and also write to cache */
- rigidbody_update_blender_ob(scene, rbw, ob, rbo);
+ /* store new transforms, and also write to cache
+ * but only if rigidbody is 'active' (i.e. something changed)
+ */
+ if (rbo->type == RBO_TYPE_ACTIVE)
+ rigidbody_update_blender_ob(scene, rbw, ob, rbo);
// 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]);
Modified: branches/soc-2010-aligorith-2/source/blender/blenloader/intern/readfile.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/blenloader/intern/readfile.c 2010-08-06 23:16:31 UTC (rev 31127)
+++ branches/soc-2010-aligorith-2/source/blender/blenloader/intern/readfile.c 2010-08-06 23:27:31 UTC (rev 31128)
@@ -4222,6 +4222,9 @@
for (rbw= sce->rigidbody_worlds.first; rbw; rbw= rbw->next) {
if (rbw->group)
rbw->group= newlibadr(fd, sce->id.lib, rbw->group);
+
+ if (rbw->effector_weights)
+ rbw->effector_weights->group= newlibadr(fd, sce->id.lib, rbw->effector_weights->group);
}
if(sce->ed)
@@ -4465,6 +4468,11 @@
*/
rbw->physics_world = NULL;
+ /* set effector weights */
+ rbw->effector_weights = newdataadr(fd, rbw->effector_weights);
+ if (!rbw->effector_weights)
+ rbw->effector_weights = BKE_add_effector_weights(rbw->group);
+
/* also, set flag to have world rebuilt on first refresh... */
rbw->recalc = RBW_RECALC_REBUILD|RBW_RECALC_REQUIRED;
}
Modified: branches/soc-2010-aligorith-2/source/blender/blenloader/intern/writefile.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/blenloader/intern/writefile.c 2010-08-06 23:16:31 UTC (rev 31127)
+++ branches/soc-2010-aligorith-2/source/blender/blenloader/intern/writefile.c 2010-08-06 23:27:31 UTC (rev 31128)
@@ -1926,8 +1926,10 @@
}
/* writing dynamic list of RigidBodyWorlds to the blend file */
- for(rbw = sce->rigidbody_worlds.first; rbw; rbw = rbw->next)
+ for(rbw = sce->rigidbody_worlds.first; rbw; rbw = rbw->next) {
writestruct(wd, DATA, "RigidBodyWorld", 1, rbw);
+ writestruct(wd, DATA, "EffectorWeights", 1, rbw->effector_weights);
+ }
sce= sce->id.next;
}
Modified: branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_object.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_object.c 2010-08-06 23:16:31 UTC (rev 31127)
+++ branches/soc-2010-aligorith-2/source/blender/editors/physics/rigidbody_object.c 2010-08-06 23:27:31 UTC (rev 31128)
@@ -34,6 +34,7 @@
#include "DNA_group_types.h"
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list