[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53019] trunk/blender: Hi there, it has been a while, just curious if my SVN account still works :)

Erwin Coumans blender at erwincoumans.com
Sat Dec 15 02:01:37 CET 2012


Revision: 53019
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53019
Author:   erwin
Date:     2012-12-15 01:01:35 +0000 (Sat, 15 Dec 2012)
Log Message:
-----------
Hi there, it has been a while, just curious if my SVN account still works :)
This commit is an attempt to improve collisions between moving Bullet rigid bodies using (concave) triangle mesh bounds.
Instead of using Gimpact, this we create a btCompoundShape with child shape tetrahedra derived from the surface triangles.
For each triangle, we add a fourth vertex using the centroid, shifting inwards using the triangle normal.
If the centroid hits an internal triangle, we stop. The default depth could be exposed as 'advanced' setting in the user interface.
This solution will be a slower than the original/gimpact solution, but a bit more reliable. 
In the future, it is better to add HACD, convex decomposition to Blender, for moving concave meshes.
See http://kmamou.blogspot.com and the Bullet SDK's Demos/ConvexDecompositionDemo.

Modified Paths:
--------------
    trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.cpp
    trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.h
    trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.cpp
    trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.h
    trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp

Added Paths:
-----------
    trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btCompoundFromGimpact.h

Added: trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btCompoundFromGimpact.h
===================================================================
--- trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btCompoundFromGimpact.h	                        (rev 0)
+++ trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btCompoundFromGimpact.h	2012-12-15 01:01:35 UTC (rev 53019)
@@ -0,0 +1,98 @@
+#ifndef BT_COMPOUND_FROM_GIMPACT
+#define BT_COMPOUND_FROM_GIMPACT
+
+#include "BulletCollision/CollisionShapes/btCompoundShape.h"
+#include "btGImpactShape.h"
+#include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
+
+struct MyCallback : public btTriangleRaycastCallback
+		{
+			int	m_ignorePart;
+			int	m_ignoreTriangleIndex;
+			
+
+			MyCallback(const btVector3& from, const btVector3& to, int ignorePart, int ignoreTriangleIndex)
+			:btTriangleRaycastCallback(from,to),
+			m_ignorePart(ignorePart),
+			m_ignoreTriangleIndex(ignoreTriangleIndex)
+			{
+				
+			}
+			virtual btScalar reportHit(const btVector3& hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex)
+			{
+				if (partId!=m_ignorePart || triangleIndex!=m_ignoreTriangleIndex)
+				{
+					if (hitFraction < m_hitFraction)
+						return hitFraction;
+				}
+
+				return m_hitFraction;
+			}
+		};
+		struct MyInternalTriangleIndexCallback :public btInternalTriangleIndexCallback
+		{
+			const btGImpactMeshShape*		m_gimpactShape;
+			btCompoundShape*			m_colShape;
+			btScalar	m_depth;
+
+			MyInternalTriangleIndexCallback (btCompoundShape* colShape, const btGImpactMeshShape* meshShape, btScalar depth)
+			:m_colShape(colShape),
+			m_gimpactShape(meshShape),
+			m_depth(depth)
+			{
+			}
+			
+			virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int  triangleIndex)
+			{
+				btVector3 scale = m_gimpactShape->getLocalScaling();
+				btVector3 v0=triangle[0]*scale;
+				btVector3 v1=triangle[1]*scale;
+				btVector3 v2=triangle[2]*scale;
+				
+				btVector3 centroid = (v0+v1+v2)/3;
+				btVector3 normal = (v1-v0).cross(v2-v0);
+				normal.normalize();
+				btVector3 rayFrom = centroid;
+				btVector3 rayTo = centroid-normal*m_depth;
+				
+				MyCallback cb(rayFrom,rayTo,partId,triangleIndex);
+				
+				m_gimpactShape->processAllTrianglesRay(&cb,rayFrom, rayTo);
+				if (cb.m_hitFraction<1)
+				{
+					rayTo.setInterpolate3(cb.m_from,cb.m_to,cb.m_hitFraction);
+					//rayTo = cb.m_from;
+					//rayTo = rayTo.lerp(cb.m_to,cb.m_hitFraction);
+					//gDebugDraw.drawLine(tr(centroid),tr(centroid+normal),btVector3(1,0,0));
+				}
+				
+
+
+				
+				btConvexHullShape* tet = new btConvexHullShape();
+				tet->addPoint(v0);
+				tet->addPoint(v1);
+				tet->addPoint(v2);
+				tet->addPoint(rayTo);
+				btTransform ident;
+				ident.setIdentity();
+				m_colShape->addChildShape(ident,tet);
+			}
+		};
+		
+btCompoundShape*	btCreateCompoundFromGimpactShape(const btGImpactMeshShape* gimpactMesh, btScalar depth)
+{
+	btCompoundShape* colShape = new btCompoundShape();
+		
+		btTransform tr;
+		tr.setIdentity();
+		
+		MyInternalTriangleIndexCallback cb(colShape,gimpactMesh, depth);
+		btVector3 aabbMin,aabbMax;
+		gimpactMesh->getAabb(tr,aabbMin,aabbMax);
+		gimpactMesh->getMeshInterface()->InternalProcessAllTriangles(&cb,aabbMin,aabbMax);
+
+	return colShape;	
+}	
+
+#endif //BT_COMPOUND_FROM_GIMPACT
\ No newline at end of file

Modified: trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.cpp
===================================================================
--- trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.cpp	2012-12-14 23:34:29 UTC (rev 53018)
+++ trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.cpp	2012-12-15 01:01:35 UTC (rev 53019)
@@ -384,7 +384,7 @@
 
 
 SIMD_FORCE_INLINE bool _quantized_node_collision(
-	btGImpactQuantizedBvh * boxset0, btGImpactQuantizedBvh * boxset1,
+	const btGImpactQuantizedBvh * boxset0, const btGImpactQuantizedBvh * boxset1,
 	const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
 	int node0 ,int node1, bool complete_primitive_tests)
 {
@@ -402,7 +402,7 @@
 
 //stackless recursive collision routine
 static void _find_quantized_collision_pairs_recursive(
-	btGImpactQuantizedBvh * boxset0, btGImpactQuantizedBvh * boxset1,
+	const btGImpactQuantizedBvh * boxset0, const btGImpactQuantizedBvh * boxset1,
 	btPairSet * collision_pairs,
 	const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
 	int node0, int node1, bool complete_primitive_tests)
@@ -501,8 +501,8 @@
 }
 
 
-void btGImpactQuantizedBvh::find_collision(btGImpactQuantizedBvh * boxset0, const btTransform & trans0,
-		btGImpactQuantizedBvh * boxset1, const btTransform & trans1,
+void btGImpactQuantizedBvh::find_collision(const btGImpactQuantizedBvh * boxset0, const btTransform & trans0,
+		const btGImpactQuantizedBvh * boxset1, const btTransform & trans1,
 		btPairSet & collision_pairs)
 {
 

Modified: trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.h
===================================================================
--- trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.h	2012-12-14 23:34:29 UTC (rev 53018)
+++ trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactQuantizedBvh.h	2012-12-15 01:01:35 UTC (rev 53019)
@@ -363,8 +363,8 @@
 	static float getAverageTreeCollisionTime();
 #endif //TRI_COLLISION_PROFILING
 
-	static void find_collision(btGImpactQuantizedBvh * boxset1, const btTransform & trans1,
-		btGImpactQuantizedBvh * boxset2, const btTransform & trans2,
+	static void find_collision(const btGImpactQuantizedBvh * boxset1, const btTransform & trans1,
+		const btGImpactQuantizedBvh * boxset2, const btTransform & trans2,
 		btPairSet & collision_pairs);
 };
 

Modified: trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.cpp
===================================================================
--- trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.cpp	2012-12-14 23:34:29 UTC (rev 53018)
+++ trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.cpp	2012-12-15 01:01:35 UTC (rev 53019)
@@ -25,6 +25,7 @@
 
 #define CALC_EXACT_INERTIA 1
 
+
 void btGImpactCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
 {
 	lockChildShapes();
@@ -144,7 +145,32 @@
 {
 }
 
+void btGImpactMeshShapePart::processAllTrianglesRay(btTriangleCallback* callback,const btVector3& rayFrom, const btVector3& rayTo) const
+{
+	lockChildShapes();
 
+	btAlignedObjectArray<int> collided;
+	btVector3 rayDir(rayTo - rayFrom);
+	rayDir.normalize();
+	m_box_set.rayQuery(rayDir, rayFrom, collided);
+
+	if(collided.size()==0)
+	{
+		unlockChildShapes();
+		return;
+	}
+
+	int part = (int)getPart();
+	btPrimitiveTriangle triangle;
+	int i = collided.size();
+	while(i--)
+	{
+		getPrimitiveTriangle(collided[i],triangle);
+		callback->processTriangle(triangle.m_vertices,part,collided[i]);
+	}
+	unlockChildShapes();
+}
+
 void btGImpactMeshShapePart::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
 {
 	lockChildShapes();
@@ -182,7 +208,16 @@
 	}
 }
 
+void btGImpactMeshShape::processAllTrianglesRay(btTriangleCallback* callback,const btVector3& rayFrom, const btVector3& rayTo) const
+{
+	int i = m_mesh_parts.size();
+	while(i--)
+	{
+		m_mesh_parts[i]->processAllTrianglesRay(callback, rayFrom, rayTo);
+	}
+}
 
+
 ///fills the dataBuffer and returns the struct name (and 0 on failure)
 const char*	btGImpactMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
 {

Modified: trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.h
===================================================================
--- trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.h	2012-12-14 23:34:29 UTC (rev 53018)
+++ trunk/blender/extern/bullet2/src/BulletCollision/Gimpact/btGImpactShape.h	2012-12-15 01:01:35 UTC (rev 53019)
@@ -51,6 +51,7 @@
 };
 
 
+
 //! Helper class for tetrahedrons
 class btTetrahedronShapeEx:public btBU_Simplex1to4
 {
@@ -192,7 +193,7 @@
 	virtual eGIMPACT_SHAPE_TYPE getGImpactShapeType() const = 0 ;
 
 	//! gets boxset
-	SIMD_FORCE_INLINE btGImpactBoxSet * getBoxSet()
+	SIMD_FORCE_INLINE const btGImpactBoxSet * getBoxSet() const
 	{
 		return &m_box_set;
 	}
@@ -288,6 +289,15 @@
         (void) callback; (void) aabbMin; (void) aabbMax;
 	}
 
+	//! Function for retrieve triangles.
+	/*!
+	It gives the triangles in local space
+	*/
+	virtual void processAllTrianglesRay(btTriangleCallback* /*callback*/,const btVector3& /*rayFrom*/, const btVector3& /*rayTo*/) const
+	{
+		
+	}
+
 	//!@}
 
 };
@@ -635,25 +645,25 @@
 			return (int )numverts;
 		}
 
-		SIMD_FORCE_INLINE void get_indices(int face_index,int &i0,int &i1,int &i2) const
+		SIMD_FORCE_INLINE void get_indices(int face_index,unsigned int &i0,unsigned int &i1,unsigned int &i2) const
 		{
 			if(indicestype == PHY_SHORT)
 			{
-				short * s_indices = (short *)(indexbase + face_index*indexstride);
+				unsigned short* s_indices = (unsigned short *)(indexbase + face_index * indexstride);
 				i0 = s_indices[0];
 				i1 = s_indices[1];
 				i2 = s_indices[2];
 			}
 			else
 			{
-				int * i_indices = (int *)(indexbase + face_index*indexstride);
+				unsigned int * i_indices = (unsigned int *)(indexbase + face_index*indexstride);
 				i0 = i_indices[0];
 				i1 = i_indices[1];
 				i2 = i_indices[2];
 			}
 		}
 
-		SIMD_FORCE_INLINE void get_vertex(int vertex_index, btVector3 & vertex) const
+		SIMD_FORCE_INLINE void get_vertex(unsigned int vertex_index, btVector3 & vertex) const
 		{
 			if(type == PHY_DOUBLE)
 			{
@@ -682,7 +692,7 @@
 
 		virtual void get_primitive_triangle(int prim_index,btPrimitiveTriangle & triangle) const
 		{
-			int indices[3];
+			unsigned int indices[3];
 			get_indices(prim_index,indices[0],indices[1],indices[2]);
 			get_vertex(indices[0],triangle.m_vertices[0]);
 			get_vertex(indices[1],triangle.m_vertices[1]);
@@ -692,7 +702,7 @@
 
 		SIMD_FORCE_INLINE void get_bullet_triangle(int prim_index,btTriangleShapeEx & triangle) const
 		{
-			int indices[3];
+			unsigned int indices[3];
 			get_indices(prim_index,indices[0],indices[1],indices[2]);
 			get_vertex(indices[0],triangle.m_vertices1[0]);
 			get_vertex(indices[1],triangle.m_vertices1[1]);
@@ -885,6 +895,7 @@
     }
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list