Physics - Bullet Collected random advice

From Wikiid
Jump to: navigation, search

This page collects random snippets of advice from the Bullet guru's.

Minimum object sizes - by Erwin

The minimum objectsize is mostly for dynamic objects, so I wouldn't worry about static triangle meshes. It's more about dynamic convex hull/box/sphere/cylinder/cone.

This 'minimum size requirement' is due to the collision margin and contact point reduction. Those (independent) values are currently default set at around 0.04 units.

  1. collision margin (collisionShape->SetMargin()): this value is to avoid doing costly penetration depth calculation, and also to guarantee a valid normal (otherwise touching objects have undefined contact normal). Don't make the value too small, otherwise performance degrades (due to slow penetration depth approximation), make it not too big, because objects will be 'rounded'. Effectively, objects can never be smaller then this margin.
  2. gContactBreakingTreshold, this value is related to contact reduction (points closer then this value are regarded 'the same', and also contact breaking (if contacts are further away then this distance, they are removed). Those values are tuned, so it works nice with default gravity and timestep and object sizes. Make them too small, and you loose points and introduce jitter. Make it too big, and all points get reduced into 1.

So it's best not to touch those values unless necessary.

A long thin box below the collision margins is not well supported, so best avoided if possible: having any side of smaller then say 0.20 units is highly unrecommended. Ignoring that will result in jitter and failing simulation, unfortunately.

The 'floating' is likely because the trianglemesh has a margin. There is some improvements possible, like setting the margin for static trianglemeshes to zero (setMargin). It will hurt performance for penetrating cases. Please don't do this for dynamic objects. Box has compensation for the margin, which means that there is no gap between stacking boxes. In the future this will also be added for convex hull objects. Sphere's use their radius for margin, so no gap either. Cylinders and Cone have some gap.

Time steps - by Erwin

kend wrote:

 Regarding step size. What is the best time step in Bullet?
 I'm currently using 0.01.
 Would 0.02 be better? Does it matter?

A small fixed timestep is best. Most of my demos use 60 Hertz (1/60). 0.01 is smaller then 0.02, so it's better. Then, choose the number of iterations for the constraint solver in the range 4 to 10.

Static geometry - by Erwin

 I have a LOT of static geometry in the scene.  Is it better to keep this in one giant
 triangle mesh - or to divide it up into a bunch of smaller mesh objects within one
 CompoundShape - or should I perhaps have a ton of tiny separate static objects.

Good question, it really depends on a lot of factors. But in general, it's good to use big static triangle meshes, and use a couple of them. Each static triangle mesh will automatically get an entry in the broadphase for culling. Avoid inserting hundreds of static triangle meshes in the broadphase. Many small triangle meshes should be avoided, and compounds are currently not very well optimized for this task.

Triangle sizes

 I've seen comments elsewhere that large triangles are a bad idea (and also very
 small ones) - somewhere I saw a lower limit of maybe 20cm - what kind of upper
 limit are we talking about here?   There was also a comment that long, thin
 triangles would be bad.  How long/thin is that? 

Not easy to convert this into an actual number. The triangle is processed by GJK collision detection, together with another convex object. If possible, try to avoid ratios 'much larger' then 10 between triangle sides.

If your units are in meters, try to keep the minimum length of a triangle side above 10 centimeter, and maximum length below 5 meter. This might not always be possible, but its a general guideline.

From the blender/bullet game contest page

Keep masses for dynamic objects similar
If an object of 100 kg rests on an object of 0.1 kg, the simulation will have difficulties. In order to avoid large mass ratios, keep mass of objects similar (in the order of a few kg)
Assign the right bounds type
For a cylinder, choose cylinder, even for non-moving objects. Similar for a box etc. Convex Hull can appoximate meshes for moving objects and static objects.
Don't use too many vertices in Convex Hull meshes
About 4 to 32 vertices should be fine.
Leave the center of the object in the middle of the mesh
The center (where the axis are) needs to be inside the mesh, not near to the boundary or outside!
Leave the gravity to 10, don't make it too large
The physics simulation works better with smaller gravity, so if possible don't use large gravity.
Avoid very small dynamic objects (< 0.2 units)
Don't make dynamic objects smaller then 0.2 units if possible. For the default gravity, 1 unit equals 1 meter, so any 'side' of the objects should be bigger then that.
No large objects
Don't use large objects, or large triangles.
Don't use degenerate triangles
Triangles that are have extreme long sides as well as extreme short sides can lead to problems.
After a few seconds, the object doesn't move anymore
It doesn't interact with moving platforms etc. You can manually activate an object, using python command 'object.restoreDynamics'. Or use the 'no sleeping' button. But don't use the 'no sleeping' button too much, perhaps just for the main character/car etc.
How to setup a vehicle
Use the PhysicsConstraints module in Python, and create a special constraint on a rigid body. This constraint allows to add wheels, set tyre friction, suspension, steering and so on. There is a demo vehicle3_Steering fix.blend in the physics demos.
  • For collision Bounds it is best to use Convex Hull Polytope. Remember to switch on collision for the chassis polygons!
  • Don't use box, because shifting the center of the box doesn't work.
  • Give the vehicle enough space to move: too narrow roads, too sharp corners and high street curbs and other obstacles that are difficult to avoid are very frequent mistakes which makes driving frustrating, rather then fun.

See the Python Vehicle Script for the code.

  • To improve (vehicle) simulation quality add this line to the python scrips:
  • don't set friction of track and ground to zero, better lower the friction of car chassis. This way you can add traffic cones and other interesting dynamic objects. Ground friction of 0 means bad physics quality.
  • Try to tweak the form factor (under No sleeping button) a little bit, but keep it close to 0.4. For example 0.45
  • For debugging to see that the raycast for each wheel has correct position and direction use:
 #draw the raycast lines
  • Make sure you don't use overlays scenes and other camera effects (split screen/viewports) during debugging. The debug lines will not properly show up.
Adding dynamic objects as children doesn't work
Dynamic objects should not have a parent. If you need to add a batch of rigid bodies, check out the addObject2.blend demo, it uses the 'instantAddObject' on the AddObjectActuator. If you need a complex setup with constaints, like a ragdoll or vehicle, you cannot create that group using an AddObjectActuator. Either use Python for this, to setup the constraints after adding the objects, or wait for future 'constraint userinterface' in Blender.
There is a small gap between the ground and my object
Except for boxes and sphere, there is indeed a tiny gap (0.04 by default) between objects. This gap allows the simulation to run smoother and more stable. It is not recommended, but you can reduce the gap by using python:
 controller = GameLogic.getCurrentController()
 owner = controller.getOwner()

You can also consider using margin of zero for static/non moving objects, and leave the default margin for dynamics objects.

I want higher quality simulation to create IPO keyframes for animation
If you are just rendering / creating keyframes, and don't worry about realtime you can increate the substeps to improve quality of simulation, using a python script
 import PhysicsConstraints

default is 1. 2 or more make simulation more accurate.

How do you set the friction and restitution ? 
Using the material buttons, select 'DYN' for dynamics friction and restitution sliders.

Wikiid Pages relating to the Bullet physics engine (edit)
Main articles: Physics - Bullet
On the Bullet website: Forum, Doxygen docs, FAQ, A Module diagram, Draft user manual, CCD & Physics Paper
Annotated example programs: HelloWorld, Tutorial2
Other: Collected advice snippets
See also : Intersection tests, GameTools Sabot