sweep body test in blender 3d

pildanovak
Posts: 50
Joined: Thu Jul 14, 2005 1:55 pm

sweep body test in blender 3d

Post by pildanovak »

Hello, I am curious if the sweep test can be done for many positions with an efficient way.

I am using blender to create paths for a cnc machine with a new addon for bleder:http://blendercam.blogspot.cz/ ,
and I would love to use bullet for collision.

It would be mainly testing the same primitive against same mesh x thousands to millions of times. So, I would like to have a function to which I would pass an array of all positions to get tested, then get the results, or test them one by one if performance is similar.

Can anybody hint me how to start? Blender allready uses bullet for rigidbody simulation, and I would like to make this optimally an python API function, so that it is versatile for various uses with blenderCAM.

Thanks for any answer.
pildanovak
Posts: 50
Joined: Thu Jul 14, 2005 1:55 pm

Re: sweep body test in blender 3d

Post by pildanovak »

Good news is I got it working inside blender.
Bad news is, for my purpose the performance is not stellar and I am sometimes getting inprecise results.

My use case is - I am developing a CAM addon, where I need to check a convex primitive(Sphere, Cone, Cylinder, Capsule), against a (sometimes very complex) mesh collider. (Imagine a cutter milling a sculpture, running exactly along it's surface)

I have two questions:
1
Is there a way to optimize such case with Bullet ? If I have now e.g. a 60000 faces mesh, and I test it with the primitive, it can now take up to 10 seconds to take 1000 tests. In Unity engine, I can get up to 10000 tests done per second on a similar case. I am quite sure bullet can run much faster, but don't know where to start...I tried to split the mesh in various ways, but so far i didn't find out any way to do it.

2.
what is "optimal" scale for bullet - I noticed that when I run the objects in small sizes (e.g. I test a 3mm primitive against a 40cm mesh), I get imprecisions. I am curious at what sizes I would get best possible precision, which is what interests me.

Thanks
pildanovak
Posts: 50
Joined: Thu Jul 14, 2005 1:55 pm

Re: sweep body test in blender 3d

Post by pildanovak »

I decided to post the code, maybe someone can help? I am thinking if the fact that I have to create a convex shape from the collision shape can be a problem? I don't know how not to do this.
I will be glad for any help...

Code: Select all

void RB_body_convex_sweep_test(rbDynamicsWorld *world, rbRigidBody *object, const float loc_start[3], const float loc_end[3], float v_location[3],  float v_hitpoint[3],  float v_normal[3])
{
	btRigidBody *body = object->body;
	btCollisionShape *collisionShape = body->getCollisionShape();
	if (collisionShape->isConvex())
	{
		btConvexShape* convexShape = (btConvexShape*) collisionShape;		
		btCollisionWorld::ClosestConvexResultCallback result(btVector3(loc_start[0], loc_start[1], loc_start[2]), btVector3(loc_end[0], loc_end[1], loc_end[2]));

		btQuaternion obRot= body->getWorldTransform().getRotation();
		
		btTransform rayFromTrans;
		rayFromTrans.setIdentity();
		rayFromTrans.setRotation(obRot);
		rayFromTrans.setOrigin(btVector3(loc_start[0], loc_start[1], loc_start[2]));

		btTransform rayToTrans;
		rayToTrans.setIdentity();
		rayToTrans.setRotation(obRot);
		
		rayToTrans.setOrigin(btVector3(loc_end[0], loc_end[1], loc_end[2]));
		
		
		world->dynamicsWorld->convexSweepTest(convexShape, rayFromTrans, rayToTrans, result);
		if (result.hasHit())
		{	
			
			v_location[0]=result.m_convexFromWorld[0]+(result.m_convexToWorld[0]-result.m_convexFromWorld[0])*result.m_closestHitFraction;
			v_location[1]=result.m_convexFromWorld[1]+(result.m_convexToWorld[1]-result.m_convexFromWorld[1])*result.m_closestHitFraction;
			v_location[2]=result.m_convexFromWorld[2]+(result.m_convexToWorld[2]-result.m_convexFromWorld[2])*result.m_closestHitFraction;
			
			v_hitpoint[0]=result.m_hitPointWorld[0];
			v_hitpoint[1]=result.m_hitPointWorld[1];
			v_hitpoint[2]=result.m_hitPointWorld[2];
			
			v_normal[0]=result.m_hitNormalWorld[0];
			v_normal[1]=result.m_hitNormalWorld[1];
			v_normal[2]=result.m_hitNormalWorld[2];

		}
	}
}
pildanovak
Posts: 50
Joined: Thu Jul 14, 2005 1:55 pm

Re: sweep body test in blender 3d

Post by pildanovak »

I still didn't get any answer here :)

and allraedy have a different question:
Can the convex sweep test be faster when searching just for first hit instead of all hits? I didn't find out if and how.

but - I realized an interesting thing. Convex sweep against a btGimpactMeshShape is actually a bit faster than against btBvhTriangleMeshShape...
and, build time for such shape is many times faster, almost nothing compared to the bvh version...

all measurments have been done inside of blender, so it may be blender specific, I have to still find what kind of broadphase blender uses.

I will be glad for ANY answer :) since It seems to me I am talking to myself on this forum. Maybe I am asking wrong questions?