[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [30065] branches/soc-2010-aligorith-2/ source/blender: Bullet SoC - 'Mesh' (i.e.

Joshua Leung aligorith at gmail.com
Wed Jul 7 03:11:50 CEST 2010


Revision: 30065
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=30065
Author:   aligorith
Date:     2010-07-07 03:11:49 +0200 (Wed, 07 Jul 2010)

Log Message:
-----------
Bullet SoC - 'Mesh' (i.e. Convex Hull) Collision Shapes

Collision shapes can now be arbitrary shapes defined by the mesh of the Rigid Body object. 

A few warnings:
* Don't use meshes with too many verts. Bullet docs recommend < 100 verts for reasonable performance still.
* The collision shape does not get updated if the mesh data gets updated. It will need to be manually updated for now. I'll investigate ways to make this update automatically.

Modified Paths:
--------------
    branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c
    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/source/blender/blenkernel/intern/rigidbody.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c	2010-07-07 00:26:31 UTC (rev 30064)
+++ branches/soc-2010-aligorith-2/source/blender/blenkernel/intern/rigidbody.c	2010-07-07 01:11:49 UTC (rev 30065)
@@ -49,6 +49,7 @@
 #include "BKE_animsys.h"
 #include "BKE_group.h"
 #include "BKE_object.h"
+#include "BKE_mesh.h"
 #include "BKE_rigidbody.h"
 #include "BKE_global.h"
 #include "BKE_utildefines.h"
@@ -140,6 +141,34 @@
 /* ************************************** */
 /* Setup Utilities - Validate Sim Instances */
 
+/* create collision shape of mesh */
+static rbCollisionShape *rigidbody_get_shape_from_mesh (Object *ob)
+{
+	rbCollisionShape *shape = rbShapeNewConvexHull();
+	float (*vertCos)[3] = NULL;
+	int numVerts = 0;
+	
+	/* get vertex coordinates - for meshes only since we've only got a mesh version for now... */
+	if ((ob->type == OB_MESH) && (ob->data))
+		vertCos = mesh_getVertexCos(ob->data, &numVerts);
+	else
+		printf("ERROR: cannot make Convex Hull collision shape for non-Mesh object\n");
+	
+	if (vertCos && numVerts) {
+		int i;
+		
+		/* just need to add all verts to the convex hull 
+		 * NOTE: for best results, we should have < 100 verts
+		 */
+		for (i = 0; i < numVerts; i++)
+			rbShapeAddVert(shape, vertCos[i]);
+	}
+	else
+		printf("ERROR: no vertices to define Convex Hull collision shape with\n");
+	
+	return shape;
+}
+
 /* Create new physics sim collision shape for object and store it,
  * or remove the existing one first and replace...
  */
@@ -203,8 +232,11 @@
 			break;
 		
 		case RB_SHAPE_TRIMESH:
+			// TODO: this may fail if the object is wrong type...
+			rbo->physics_shape = rigidbody_get_shape_from_mesh(ob);
 			break;
 		case RB_SHAPE_COMPOUND:
+			rbo->physics_shape = NULL; // xxx!
 			break;
 	}
 }

Modified: branches/soc-2010-aligorith-2/source/blender/makesrna/intern/rna_rigidbody.c
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/makesrna/intern/rna_rigidbody.c	2010-07-07 00:26:31 UTC (rev 30064)
+++ branches/soc-2010-aligorith-2/source/blender/makesrna/intern/rna_rigidbody.c	2010-07-07 01:11:49 UTC (rev 30065)
@@ -44,12 +44,15 @@
 	{0, NULL, 0, NULL, NULL}};
 
 /* collision shapes of objects in RigidBody Sims */
+// TODO: need checking for the 'mesh' option...
 EnumPropertyItem rigidbody_ob_shape_items[] = {
 	{RB_SHAPE_BOX, "BOX", ICON_MESH_CUBE, "Box", "Box-like shapes (i.e. cubes), including planes (i.e. ground planes)"},
 	{RB_SHAPE_SPHERE, "SPHERE", ICON_MESH_UVSPHERE, "Sphere", ""},
 	{RB_SHAPE_CAPSULE, "CAPSULE", ICON_OUTLINER_OB_META, "Capsule", ""},
 	{RB_SHAPE_CYLINDER, "CYLINDER", ICON_MESH_TUBE, "Cylinder", ""},
 	{RB_SHAPE_CONE, "CONE", ICON_MESH_CONE, "Cone", ""},
+	{0, "", 0, "", ""},
+	{RB_SHAPE_TRIMESH, "MESH", ICON_MESH_MONKEY, "Mesh", "Convex Hull defined from vertices of mesh only. Best results with fewer vertoces"},
 	// XXX: compound shapes have yet to be implemented
 	{0, NULL, 0, NULL, NULL}};
 

Modified: branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h
===================================================================
--- branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h	2010-07-07 00:26:31 UTC (rev 30064)
+++ branches/soc-2010-aligorith-2/source/blender/rigidbody/RBI_api.h	2010-07-07 01:11:49 UTC (rev 30065)
@@ -194,7 +194,8 @@
 
 /* Setup (Special Shapes) ------------ */
 
-// TODO: how to setup the mesh collision shapes?
+extern rbCollisionShape *rbShapeNewConvexHull();
+extern void rbShapeAddVert(rbCollisionShape *cshape, const float co[3]);
 
 /* Cleanup --------------------------- */
 

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-07-07 00:26:31 UTC (rev 30064)
+++ branches/soc-2010-aligorith-2/source/blender/rigidbody/rb_bullet_api.cpp	2010-07-07 01:11:49 UTC (rev 30065)
@@ -17,7 +17,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * The Original Code is Copyright (C) 2010 Blender Foundation, Joshua Leung
+ * The Original Code is Copyright (C) 2010 Blender Foundation
  * All rights reserved.
  *
  * The Original Code is: all of this file.
@@ -26,12 +26,30 @@
  *
  * ***** END GPL LICENSE BLOCK *****
  */
+
+/*
+Bullet Continuous Collision Detection and Physics Library
+Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
+
+This software is provided 'as-is', without any express or implied warranty.
+In no event will the authors be held liable for any damages arising from the use of this software.
+Permission is granted to anyone to use this software for any purpose, 
+including commercial applications, and to alter it and redistribute it freely, 
+subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
+2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
+3. This notice may not be removed or altered from any source distribution.
+*/
  
 /* This file defines the "RigidBody interface" for the 
  * Bullet Physics Engine. This API is designed to be used
  * from C-code in Blender as part of the Rigid Body simulation
  * system.
  *
+ * It is based on the Bullet C-API, but is heavily modified to 
+ * give access to more data types and to offer a nicer interface.
+ *
  * -- Joshua Leung, June 2010
  */
  
@@ -53,11 +71,6 @@
 #include "LinearMath/btMatrix3x3.h"
 #include "LinearMath/btTransform.h"
 
-#include "LinearMath/btSerializer.h"
-
-#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
-#include "BulletCollision/CollisionShapes/btTriangleShape.h"
-
 /* ********************************** */
 /* Dynamics World Methods */
 
@@ -450,7 +463,6 @@
 	trans.setFromOpenGLMatrix((const btScalar*)m_in);
 	
 	ms->setWorldTransform(trans);
-	//body->getWorldTransform() = trans; // xxx?
 }
 
 /* ********************************** */
@@ -490,8 +502,18 @@
 
 /* Setup (Special Shapes) ------------ */
 
-// TODO: figure out how to do mesh shapes properly
+rbCollisionShape *rbShapeNewConvexHull()
+{
+	void *mem = btAlignedAlloc(sizeof(btConvexHullShape), 16);
+	return (rbCollisionShape*) new(mem) btConvexHullShape();
+}
 
+void rbShapeAddVert(rbCollisionShape *cshape, const float co[3])
+{
+	btConvexHullShape *convexHullShape = reinterpret_cast<btConvexHullShape*>(cshape);
+	convexHullShape->addPoint(btVector3(co[0], co[1], co[2]));
+}
+
 /* Cleanup --------------------------- */
 
 void rbShapeDelete(rbCollisionShape *cshape)





More information about the Bf-blender-cvs mailing list