rayCast precision on large shapes

Post Reply
User avatar
mirat
Posts: 16
Joined: Thu May 29, 2008 10:47 am

rayCast precision on large shapes

Post by mirat »

Hi!

I'm making a racer game. Some strange problem appears: when the size of ground ( I tried both btConvexShape and btBoxShape as shapes for the ground) is small (100x100, for example), vehicle normally behaves. But when size becomes big (~1000x1000) wheels begin vibrating. I found the problem in rayCast precision. btDynamicWorld's rayCast becomes too unstable returning values in wide range while small variations of ray start position are given.

Bullet initialization:

Code: Select all

	collision_conf = new btDefaultCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collision_conf);

	broadphase = new btDbvtBroadphase(new btHashedOverlappingPairCache());

	solver = new btSequentialImpulseConstraintSolver();

	world = static_cast<btDynamicsWorld*> (new btDiscreteDynamicsWorld (dispatcher, broadphase, solver, collision_conf));
	world->setGravity(btVector3(0,-9.8,0));

	btVector3 localInertia (0,0,0);
	btCollisionShape* groundShape = new btBoxShape(btVector3(1200,3,1200));   // <--- I played with ground size here
	btRigidBody* ground = new btRigidBody(0,0,groundShape,localInertia);
	ground->setWorldTransform(btTransform(btQuaternion(0,0,0,1), btVector3(0,-5,0)));

	world->addRigidBody(ground);
Vehicle creating is distributed over many classes, but the main idea is taken from VehicleDemo.

Are there any ideas?
irrmich
Posts: 6
Joined: Thu Sep 04, 2014 1:23 pm

Re: rayCast precision on large shapes

Post by irrmich »

I have the same trouble, but i don't even try to look at Raycasting code, did you solved the problem finally? :|
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: rayCast precision on large shapes

Post by drleviathan »

I poked around the Bullet code to see if I could figure it out. This is what I learned:

The raytest code uses the same code as continuous collision detection (CCD). It creates a zero-radius btSphereShape and then sweeps it from rayStart to rayEnd to see if it collides with the other object. Under the hood it uses a btGjkConvexCast by default (it is possible to use a btSubsimplexConvexCast instead by setting a flag somewhere along the way).

The GJK algorithm does some transform math which is where floating point numerical error would probably be introduced, especially when the tiny sphere of the ray is very far from the center of the other shape. It looks like you make the ground's center near the origin (0, -5, 0) so as long as your vehicle is driving somewhere near the origin (i.e. near the center of the box) I wouldn't expect numerical error to be noticeable, but if you were to drive out toward the edge of the ground (500, 0, 500) then I could see how it might happen there. Is this the case for your vehicle + large ground? That is, do the inaccuracies happen everywhere on the ground, or only when you drive far out?

Floating point error may also be noticeable when ray tracing against a small object that is very far from the origin since the GJK algorithm has to use the relative position of objectA to objectB and subtracting two nearly equal large distances from each other is exactly where floating point error is introduced. There may be a way to select double-precision when building Bullet which may solve your problem -- I think I saw another thread where someone was working on a space travel game that used large distances and hence required double-precision.
StabInTheDark
Posts: 29
Joined: Sat May 18, 2013 1:36 am
Location: NY
Contact:

Re: rayCast precision on large shapes

Post by StabInTheDark »

You need to build the Bullet Libraries for double precision.
I am using them with double precision and it solves a lot of problems.
Just make sure you use btScalar, it is defined as a double when using double precision.
Bullet works best when you keep within Bullet's scale.
I scale down my input to Bullet and scale back up in the motion state.

http://www.bulletphysics.org/mediawiki- ... _The_World
Post Reply