[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24454] branches/physics25: Physics: more code (mixed with pseudo-code :)).
Arystanbek Dyussenov
arystan.d at gmail.com
Tue Nov 10 17:44:03 CET 2009
Revision: 24454
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24454
Author: kazanbas
Date: 2009-11-10 17:44:00 +0100 (Tue, 10 Nov 2009)
Log Message:
-----------
Physics: more code (mixed with pseudo-code :)).
Corrected object evaluation order in scene_update() as Brecht noted. Pseudo-code on the wiki http://wiki.blender.org/index.php/User:Kazanbas/Bullet_Proposal#Depsgraph
Modified Paths:
--------------
branches/physics25/extern/bullet2/src/Bullet-C-Api.h
branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-API.cpp
branches/physics25/source/blender/blenkernel/intern/rigidbody.c
branches/physics25/source/blender/blenkernel/intern/scene.c
branches/physics25/source/blender/makesdna/DNA_object_types.h
Property Changed:
----------------
branches/physics25/source/blender/blenkernel/intern/rigidbody.c
Modified: branches/physics25/extern/bullet2/src/Bullet-C-Api.h
===================================================================
--- branches/physics25/extern/bullet2/src/Bullet-C-Api.h 2009-11-10 16:18:54 UTC (rev 24453)
+++ branches/physics25/extern/bullet2/src/Bullet-C-Api.h 2009-11-10 16:44:00 UTC (rev 24454)
@@ -166,6 +166,13 @@
// needed for source/blender/blenkernel/intern/collision.c
double plNearestPoints(float p1[3], float p2[3], float p3[3], float q1[3], float q2[3], float q3[3], float *pa, float *pb, float normal[3]);
+
+ /* These funcs were added during the Bullet integration project (see http://wiki.blender.org/index.php/User:Kazanbas/Bullet_Proposal) (Arystan) */
+ typedef void *(GetTransformFunc)(void *data, btScalar m[4][4]);
+ typedef void *(SetTransformFunc)(void *data, btScalar m[4][4]);
+
+ void plSetMotionState(void *data, GetTransformFunc *get, SetTransformFunc *set);
+
#ifdef __cplusplus
}
#endif
Modified: branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-API.cpp
===================================================================
--- branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-API.cpp 2009-11-10 16:18:54 UTC (rev 24453)
+++ branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-API.cpp 2009-11-10 16:44:00 UTC (rev 24454)
@@ -394,34 +394,43 @@
return -1.0f;
}
-typedef void *(GetTransformFunc)(btScalar m[4][4]);
-typedef void *(SetTransformFunc)(btScalar m[4][4]);
-void set_motion_state(GetTransformFunc *get, SetTransformFunc *set);
-void set_motion_state(plRigidBodyHandle r, GetTransformFunc *get, SetTransformFunc *set)
-{
-
-}
-
class CustomMotionState : public btMotionState {
private:
+ void *data;
GetTransformFunc *get;
SetTransformFunc *set;
public:
- CustomMotionState(GetTransformFunc *get, SetTransformFunc *set) : get(get), set(set) { }
+ CustomMotionState(void *data, GetTransformFunc *get, SetTransformFunc *set) : data(data), get(get), set(set) { }
virtual void getWorldTransform(btTransform& t) const
{
btScalar m[4][4];
- this->get(m);
- t.setFromOpenGLSubMatrix((btScalar*)m);
+ this->get(data, m);
+ t.setFromOpenGLMatrix((btScalar*)m);
}
virtual void setWorldTransform(const btTransform& t)
{
btScalar m[4][4];
t.getOpenGLMatrix((btScalar*)m);
- this->set(m);
+ this->set(data, m);
}
};
+
+void plDeleteMotionState(plRigidBodyHandle b)
+{
+ btRigidBody* body= (btRigidBody*)b;
+ CustomMotionState* ms= (CustomMotionState*)body->getMotionState();
+ if (ms)
+ delete ms;
+ body->setMotionState(NULL);
+}
+
+void plSetMotionState(plRigidBodyHandle b, void *data, GetTransformFunc *get, SetTransformFunc *set)
+{
+ btRigidBody* body= (btRigidBody*)b;
+ plDeleteMotionState(b);
+ body->setMotionState(new CustomMotionState(data, get, set));
+}
Modified: branches/physics25/source/blender/blenkernel/intern/rigidbody.c
===================================================================
--- branches/physics25/source/blender/blenkernel/intern/rigidbody.c 2009-11-10 16:18:54 UTC (rev 24453)
+++ branches/physics25/source/blender/blenkernel/intern/rigidbody.c 2009-11-10 16:44:00 UTC (rev 24454)
@@ -28,13 +28,41 @@
* ***** END GPL LICENSE BLOCK *****
*/
+#include <string.h>
+
+#include "MEM_guardedalloc.h"
+#include "BLI_arithb.h"
+
#include "Bullet-C-Api.h"
static plPhysicsSdkHandle bullet_sdk; /* XXX global var */
/* void object_to_rigidbody(); */
-static plCollisionShapeHandle create_shape(Object *ob);
+static RigidBody *make_rigid_body(Object *ob)
+struct RigidBody
+{
+ Object *ob;
+ plRigidBodyHandle body;
+};
+
+void motion_state_get_transform(void *data, float m[4][4])
+{
+ RigidBody *rb= (RigidBody*)data;
+ Mat4CpyMat4(m, rb->ob->obmat);
+ Mat4Transp(m);
+}
+
+void motion_state_set_transform(void *data, float m[4][4])
+{
+ RigidBody *rb= (RigidBody*)data;
+
+ memcpy(rb->ob->loc, m[3], sizeof(rb->ob->loc));
+ Mat4ToEulO(m, rb->ob->rot, EULER_ORDER_XYZ);
+
+ rb->ob->recalc |= OB_RECALC_OB;
+}
+
/*
Q: use motionstates?
Q: how to update rigid body mass as it changes in Object
@@ -61,11 +89,11 @@
for(go= group->gobject.first; go; go= go->next) {
ob= go->ob;
if (!ob->rigid) {
- ob->rigid= plCreateRigidBody(NULL, ob->mass, create_shape(ob));
+ ob->rigid= make_rigid_body(ob);
- plRemoveRigidBody(world, ob->rigid);
+ plRemoveRigidBody(world, ob->rigid->body);
- plAddRigidBody(world, ob->rigid);
+ plAddRigidBody(world, ob->rigid->body);
}
}
@@ -83,7 +111,55 @@
plStepSimulation(world, delta);
}
-static plCollisionShapeHandle create_shape(Object *ob)
+static plCollisionShapeHandle make_collision_shape(Object *ob)
{
- #error write shape code
+ #warning write shape code
+
+ return NULL;
}
+
+static RigidBody *make_rigid_body(Object *ob)
+{
+ RigidBody *rb= MEM_callocN(sizeof(RigidBody), "RigidBody");
+ rb->ob= ob;
+ rb->body= plCreateRigidBody(NULL, ob->mass, make_collision_shape(ob));
+
+ set_motion_state();
+
+ return rb;
+}
+
+void rigidbody_final_free()
+{
+ for (each group) {
+ if (physics group) {
+ for (each object) {
+ free_rigid_body(ob);
+ }
+ }
+
+ plDeleteDynamicsWorld(group->physics_world);
+ }
+
+ /* also handle objects that may have rigid body data although not participating in sim. */
+ for (each scene) {
+ for (each object) {
+ if (ob->rigid) {
+ free_rigid_body(ob);
+ }
+ }
+ }
+
+ plDeletePhysicsSdk(bullet_sdk);
+}
+
+void free_rigid_body(Object *ob)
+{
+ if (ob->rigid) {
+ RigidBody *rb= ob->rigid;
+ plDeleteMotionState(rb->body);
+
+ MEM_freeN(rb);
+ ob->rigid= NULL;
+ }
+}
Property changes on: branches/physics25/source/blender/blenkernel/intern/rigidbody.c
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Modified: branches/physics25/source/blender/blenkernel/intern/scene.c
===================================================================
--- branches/physics25/source/blender/blenkernel/intern/scene.c 2009-11-10 16:18:54 UTC (rev 24453)
+++ branches/physics25/source/blender/blenkernel/intern/scene.c 2009-11-10 16:44:00 UTC (rev 24454)
@@ -91,6 +91,7 @@
#include "BLI_arithb.h"
#include "BLI_blenlib.h"
+#include "BLI_ghash.h"
//XXX #include "nla.h"
@@ -774,9 +775,12 @@
{
Base *base;
Object *ob;
+ float ctime = frame_to_float(sce, sce->r.cfra);
+
Group *group;
GroupObject *go;
- float ctime = frame_to_float(sce, sce->r.cfra);
+ int count[100], i;
+ GHash *physics_group_count;
if(sce->theDag==NULL)
DAG_scene_sort(sce);
@@ -791,17 +795,15 @@
*/
BKE_animsys_evaluate_all_animation(G.main, ctime);
- /* update physics objects first */
- for(group= G.main->group.first; group; group= group->id.next) {
- if(group->flag & GR_PHYSICS) {
+ memset(count, 0, sizeof(count));
+ physics_group_count= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
- /* evaluate transform first */
- for(go= group->gobject.first; go; go= go->next) {
- object_handle_update(sce, go->ob);
+ for (group= G.main->group.first, i= 0; group; group= group->id.next) {
+ BLI_ghash_insert(physics_group_count, group, &count[i++]);
+ if (group->flag & GR_PHYSICS) {
+ for (go= group->gobject.first; go; go= go->next) {
+ (*(int*)BLI_ghash_lookup(physics_group_count, group))++;
}
-
- /* now perform sim. step */
- rigidbody_group_step(group);
}
}
@@ -809,6 +811,16 @@
ob= base->object;
object_handle_update(sce, ob); // bke_object.h
+
+ /* step rigid body physics in groups after all objects (of that group) have been evaluated */
+ for (group= G.main->group.first; group; group= group->id.next) {
+ if (group->flag & GR_PHYSICS) {
+ if (object_in_group(ob, group)) {
+ if (--(*(int*)BLI_ghash_lookup(physics_group_count, group)) == 0)
+ rigidbody_group_step(group);
+ }
+ }
+ }
/* only update layer when an ipo */
// XXX old animation system
Modified: branches/physics25/source/blender/makesdna/DNA_object_types.h
===================================================================
--- branches/physics25/source/blender/makesdna/DNA_object_types.h 2009-11-10 16:18:54 UTC (rev 24453)
+++ branches/physics25/source/blender/makesdna/DNA_object_types.h 2009-11-10 16:44:00 UTC (rev 24454)
@@ -249,7 +249,7 @@
ListBase pc_ids;
ListBase *duplilist; /* for temporary dupli list storage, only for use by RNA API */
- void *rigid; /* rigid body pointer, not saved to file */
+ struct RigidBody *rigid; /* rigid body pointer, not saved to file */
} Object;
/* Warning, this is not used anymore because hooks are now modifiers */
More information about the Bf-blender-cvs
mailing list