[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24499] branches/physics25: Physics: objects in a physics group move and collide under gravity force.

Arystanbek Dyussenov arystan.d at gmail.com
Wed Nov 11 16:59:30 CET 2009


Revision: 24499
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24499
Author:   kazanbas
Date:     2009-11-11 16:59:29 +0100 (Wed, 11 Nov 2009)

Log Message:
-----------
Physics: objects in a physics group move and collide under gravity force. Since each group gets a separate world, objects from different groups don't collide with each other.

TODO items:
* update rigid body mass when object mass changes
* update collision shape when mesh changes
* correct simulation step size (it's slower than realtime currently)
* free physics data
* and many more...

Modified Paths:
--------------
    branches/physics25/extern/bullet2/src/Bullet-C-Api.h
    branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-API.cpp
    branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp
    branches/physics25/source/blender/blenkernel/intern/object.c
    branches/physics25/source/blender/blenkernel/intern/rigidbody.c
    branches/physics25/source/blender/blenkernel/intern/scene.c
    branches/physics25/source/blender/blenloader/intern/readfile.c
    branches/physics25/source/blender/makesdna/DNA_group_types.h
    branches/physics25/source/blender/python/intern/bpy_interface.c
    branches/physics25/source/blender/render/CMakeLists.txt

Added Paths:
-----------
    branches/physics25/source/blender/blenkernel/BKE_rigidbody.h

Modified: branches/physics25/extern/bullet2/src/Bullet-C-Api.h
===================================================================
--- branches/physics25/extern/bullet2/src/Bullet-C-Api.h	2009-11-11 15:50:49 UTC (rev 24498)
+++ branches/physics25/extern/bullet2/src/Bullet-C-Api.h	2009-11-11 15:59:29 UTC (rev 24499)
@@ -168,11 +168,15 @@
 
 
 	/* 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]);
+	typedef void (*GetTransformFunc)(void *data, float m[][4]);
+	typedef void (*SetTransformFunc)(void *data, float m[][4]);
 
-	void plSetMotionState(void *data, GetTransformFunc *get, SetTransformFunc *set);
+	void plSetMotionState(plRigidBodyHandle body, void *data, GetTransformFunc get, SetTransformFunc set);
+	void plSetTransform(plRigidBodyHandle body, float m[][4]);
+	void plGetTransform(plRigidBodyHandle body, float m[][4]);
 
+	void plWorldSetGravity(plDynamicsWorldHandle world, float v[3]);
+
 #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-11 15:50:49 UTC (rev 24498)
+++ branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/Bullet-C-API.cpp	2009-11-11 15:59:29 UTC (rev 24499)
@@ -398,23 +398,31 @@
 class CustomMotionState : public btMotionState {
 private:
 	void *data;
-	GetTransformFunc *get;
-	SetTransformFunc *set;
+	GetTransformFunc get;
+	SetTransformFunc set;
 	
 public:
-	CustomMotionState(void *data, GetTransformFunc *get, SetTransformFunc *set) : data(data), 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];
+		float m[4][4];
 		this->get(data, m);
+#ifndef BT_USE_DOUBLE_PRECISION
 		t.setFromOpenGLMatrix((btScalar*)m);
+#else
+#error FIXME no double precision support yet
+#endif
 	}
 
 	virtual void setWorldTransform(const btTransform& t)
 	{
-		btScalar m[4][4];
+		float m[4][4];
+#ifndef BT_USE_DOUBLE_PRECISION
 		t.getOpenGLMatrix((btScalar*)m);
+#else
+#error FIXME no double precision support yet
+#endif
 		this->set(data, m);
 	}
 };
@@ -428,9 +436,35 @@
 	body->setMotionState(NULL);
 }
 
-void plSetMotionState(plRigidBodyHandle b, void *data, GetTransformFunc *get, SetTransformFunc *set)
+void plSetMotionState(plRigidBodyHandle b, void *data, GetTransformFunc get, SetTransformFunc set)
 {
 	btRigidBody* body= (btRigidBody*)b;
 	plDeleteMotionState(b);
 	body->setMotionState(new CustomMotionState(data, get, set));
 }
+
+void plSetTransform(plRigidBodyHandle b, float m[4][4])
+{
+	btTransform t;
+#ifndef BT_USE_DOUBLE_PRECISION
+	t.setFromOpenGLMatrix((btScalar*)m);
+#else
+#error FIXME no double precision support yet
+#endif
+	((btRigidBody*)b)->setCenterOfMassTransform(t);
+}
+
+void plGetTransform(plRigidBodyHandle b, float m[4][4])
+{
+	const btTransform& t = ((btRigidBody*)b)->getCenterOfMassTransform();
+#ifndef BT_USE_DOUBLE_PRECISION
+	t.getOpenGLMatrix((btScalar*)m);
+#else
+#error FIXME no double precision support yet
+#endif
+}
+
+void plWorldSetGravity(plDynamicsWorldHandle world, float v[3])
+{
+	((btDynamicsWorld*)world)->setGravity(btVector3(v[0], v[1], v[2]));
+}

Modified: branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp
===================================================================
--- branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp	2009-11-11 15:50:49 UTC (rev 24498)
+++ branches/physics25/extern/bullet2/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp	2009-11-11 15:59:29 UTC (rev 24499)
@@ -358,6 +358,8 @@
 	//else
 	//	synchronizeMotionStates();
 
+	synchronizeMotionStates();	// motion states are used by native Bullet-Blender integration (see rigidbody.c) (Arystan)
+
 	clearForces();
 
 #ifndef BT_NO_PROFILE

Added: branches/physics25/source/blender/blenkernel/BKE_rigidbody.h
===================================================================
--- branches/physics25/source/blender/blenkernel/BKE_rigidbody.h	                        (rev 0)
+++ branches/physics25/source/blender/blenkernel/BKE_rigidbody.h	2009-11-11 15:59:29 UTC (rev 24499)
@@ -0,0 +1,36 @@
+/**
+ * BKE_rigidbody.h 
+ *	
+ * $Id$
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Arystanbek Dyussenov.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef BKE_RIGIDBODY_H
+#define BKE_RIGIDBODY_H
+
+extern void rigidbody_group_step(struct Group *group, struct Scene *scene);
+
+#endif


Property changes on: branches/physics25/source/blender/blenkernel/BKE_rigidbody.h
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Modified: branches/physics25/source/blender/blenkernel/intern/object.c
===================================================================
--- branches/physics25/source/blender/blenkernel/intern/object.c	2009-11-11 15:50:49 UTC (rev 24498)
+++ branches/physics25/source/blender/blenkernel/intern/object.c	2009-11-11 15:59:29 UTC (rev 24499)
@@ -1296,6 +1296,8 @@
 
 	obn->gpulamp.first = obn->gpulamp.last = NULL;
 	obn->pc_ids.first = obn->pc_ids.last = NULL;
+
+	obn->rigid= NULL;
 	
 	return obn;
 }

Modified: branches/physics25/source/blender/blenkernel/intern/rigidbody.c
===================================================================
--- branches/physics25/source/blender/blenkernel/intern/rigidbody.c	2009-11-11 15:50:49 UTC (rev 24498)
+++ branches/physics25/source/blender/blenkernel/intern/rigidbody.c	2009-11-11 15:59:29 UTC (rev 24499)
@@ -31,47 +31,37 @@
 #include <string.h>
 
 #include "MEM_guardedalloc.h"
-#include "BLI_arithb.h"
+#include "BLI_math.h"
+#include "BKE_object.h"
 
+#include "DNA_scene_types.h"
+#include "DNA_object_types.h"
+#include "DNA_group_types.h"
+
 #include "Bullet-C-Api.h"
 
 static plPhysicsSdkHandle bullet_sdk;	/* XXX global var */
 
-/* void object_to_rigidbody(); */
-static RigidBody *make_rigid_body(Object *ob)
-
-struct RigidBody
+typedef struct RigidBody
 {
 	Object *ob;
 	plRigidBodyHandle body;
-};
+	float initm[4][4];	/* transform at start frame */
+} RigidBody;
 
-void motion_state_get_transform(void *data, float m[4][4])
-{
-	RigidBody *rb= (RigidBody*)data;
-	Mat4CpyMat4(m, rb->ob->obmat);
-	Mat4Transp(m);
-}
+/* void object_to_rigidbody(); */
+static void make_rigid_body(Object *ob);
+static void reset_transform(Object *ob);
 
-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
   Q: how to update collision shape when mesh changes?
+  Q: when to free things?
+  Q: what happens if object is in 2 or more physics groups simultaneously. Avoid this?
 */
-void rigidbody_group_step(Group *group)
+void rigidbody_group_step(Group *group, Scene *scene)
 {
 	GroupObject *go;
-	Object *ob;
 	plDynamicsWorldHandle world;
 
 	static int prevframe= 0;
@@ -81,24 +71,30 @@
 	if (!bullet_sdk)
 		bullet_sdk= plNewBulletSdk();
 
-	if (!group->physics_world)
+	if (!group->physics_world) {
+		float gra[]= {0, 0, -9.8f};
 		group->physics_world= plCreateDynamicsWorld(bullet_sdk);
+		plWorldSetGravity(group->physics_world, gra);
+	}
 
 	world= group->physics_world;
 
 	for(go= group->gobject.first; go; go= go->next) {
-		ob= go->ob;
-		if (!ob->rigid) {
-			ob->rigid= make_rigid_body(ob);
+		if (!go->ob->rigid) {
+			make_rigid_body(go->ob);
 
-			plRemoveRigidBody(world, ob->rigid->body);
+			/* plRemoveRigidBody(world, go->ob->rigid->body); */
 
-			plAddRigidBody(world, ob->rigid->body);
+			plAddRigidBody(world, go->ob->rigid->body);
 		}
 	}
 
 	if (frame == startframe) {
 		/* start frame - reset positions */
+		for(go= group->gobject.first; go; go= go->next) {
+			reset_transform(go->ob);
+		}
+		return;
 	}
 
 	delta= FRA2TIME(frame) - FRA2TIME(prevframe);
@@ -111,24 +107,78 @@
 	plStepSimulation(world, delta);
 }
 
+/* currently creates only a box shape */
 static plCollisionShapeHandle make_collision_shape(Object *ob)
 {
-	#warning write shape code
+	BoundBox *bb= object_get_boundbox(ob);
 
-	return NULL;
+	return plNewBoxShape(bb->vec[6][0], bb->vec[6][1], bb->vec[6][2]);
 }
 
-static RigidBody *make_rigid_body(Object *ob)
+/* Bullet calls this when simulation starts */
+static void motion_state_get_transform(void *data, float m[][4])
 {
+	RigidBody *rb= (RigidBody*)data;
+	copy_m4_m4(m, rb->ob->obmat);
+	transpose_m4(m);
+}
+
+/* Bullet calls this when body tranform changes */
+static void motion_state_set_transform(void *data, float m[][4])
+{
+	/* TODO: store transform in RigidBody for blending with animation (instead of applying it immediately) */
+	RigidBody *rb= (RigidBody*)data;
+
+	memcpy(rb->ob->loc, m[3], sizeof(rb->ob->loc));
+	//Mat4ToEulO(m, rb->ob->rot, EULER_ORDER_XYZ);
+	mat4_to_eul(rb->ob->rot, m);
+
+	rb->ob->recalc |= OB_RECALC_OB;
+
+	/* shouldn't object_handle_update() be called here now? */
+}
+
+/* put body back to initial transform */
+static void reset_transform(Object *ob)
+{
+	RigidBody *rb= ob->rigid;
+	float m[4][4];
+
+	/* transform body */

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list