Skip to content

Commit 9ea1d69

Browse files
committed
Updated the collision callbacks so the callback only occurs if the manifold has contact points.
Added the Bullet manual Improved the readme documentation.
1 parent 05a85e0 commit 9ea1d69

15 files changed

+81
-120
lines changed
Binary file not shown.

UnityProject/Assets/BulletUnity/Documentation/Bullet_User_Manual.pdf.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,66 @@
11
GETTING STARTED WITH BULLET PHYSICS
22
===================================
33

4+
BULLET UNITY CONTAINS TWO API'S
5+
6+
1) BulletSharp - Located in Plugins/BulletSharp is a low level set of C# wrappers for the native bullet libraries. These wrappers are not integrated with Unity in
7+
any way. Simulations can be run that are not synchronized with Unity's game loop. The demos in BulletUnity/Examples/Scenes/BulletSharpDemos use this API.
8+
9+
2) BulletUnity - Located in BulletUnity/Scripts is a set of Unity Components similar to the PhysX components. These components use the lower level BulletSharp API.
10+
11+
======================
412
SOURCES OF INFORMATION
513

6-
The Bullet Physics Manual. Download it from the Bullet Physics project on github. It is short (can be read in 1 hour) and will set you up well for working with
14+
The BULLET PHYSICS MANUAL. Download it from the Bullet Physics project on github. It is short (can be read in 1 hour) and will set you up well for working with
715
Bullet Physics.
816

917
The Bullet Physics Wiki has a lot of good information http://bulletphysics.org/mediawiki-1.5.8/index.php/Main_Page.
1018

1119
The Bullet Physics Forums http://www.bulletphysics.org/Bullet/phpBB3/
1220

13-
The Bullet Physics Examples (ported to C#, then ported to Unity) located in the BulletUnity/BulletSharpExamples folder. At the time of writing not all of these
14-
have been ported. More are available in the https://github.com/Phong13/BulletSharpPInvoke project.
21+
The Bullet Physics Examples (ported to C#, then ported to Unity) located in the BulletUnity/Examples/Scenes/BulletSharpDemos folder. More are available in the https://github.com/Phong13/BulletSharpPInvoke project.
1522

1623
DON'T BE AFRAID TO LOOK AT THE BULLET PHYSICS SOURCE CODE. I know it sounds intimidating, but it is much easier than you think. If you are not sure what an API
1724
call or class does or what a member variable is for, then search for it in the bullet sourcecode. Even if you are not a C++ programmer you can probably deduce
1825
what it does. I recently spent a few hours on google trying to find good information explaining the difference between btGhostObject and btPairCachingGhostObject.
1926
Eventually I opened the btGhostObject.cpp source file and had a look. The entire sourcecode for both classes is only 170 lines. In about 10 minutes I had a complete
2027
understanding how both classes worked. Much better than an online tutorial.
2128

22-
It is possible to debug from Unity into the Bullet Physics library with Visual Studio (probably possible with other IDEs but I havn't tried). To do this you need
23-
to clone the https://github.com/Phong13/BulletSharpPInvoke project. Build a debug, x64 version of libbulletc for windows. Copy the .dll and .pdb file to Unity project.
29+
It is possible to debug from Unity into the Bullet Physics library native code with Visual Studio (probably possible with other IDEs but I havn't tried). To do this you need
30+
to clone the https://github.com/Phong13/BulletSharpPInvoke project. Build a debug, x64 version of libbulletc for windows. Copy the .dll and .pdb file to Unity project (Plugins/Native/x64.
2431
Then launch Unity from the Visual Studio as described here https://msdn.microsoft.com/en-us/library/605a12zt.aspx.
2532

26-
IMPORTANT DIFFERENCES WITH UNITY'S PHYSICS
33+
================================================
34+
IMPORTANT DIFFERENCES WITH UNITY'S PHYSX PHYSICS
2735

2836
Don't try to move the rigid bodies by writing to myRigidBody.transform.position or .rotation. Bullet Physics is not as tightly integrated with Unity as PhysX.
29-
Consider the transform to be completely under the control of Bullet Physics and translate/rotate your rigidBodies using the Bullet Physics API calls.
37+
Consider the transform to be completely under the control of Bullet Physics (for non-kinematic RigidBodies) and translate/rotate your rigidBodies using the Bullet Physics API calls.
3038

3139
Be careful of localScale. It is almost completely ignored by Bullet Physics. There are only a few CollisionShapes that can be scaled in the Bullet API (at the time
32-
of writing these have not been implemented in Unity). Modify the shape of the CollisionShape and leave localScale at 1,1,1.
40+
of writing these have not been implemented in Unity). Modify the shape of the CollisionShape and leave localScale at 1,1,1. You can add your MeshRender as a child of
41+
the CollisionShape and scale that.
42+
43+
Don't try to nest Rigid Bodies. Bullet Unity has no control over the order that bullet updates the transforms of objects each simulation step. If the child RigidBodies get
44+
updated before the parent RigidBodies then the child will jitter terribly.
45+
46+
WRONG
47+
RigidBodyGameObjectA
48+
-RigidBodyGameObjectB
49+
-RigidBodyGameObjectC
50+
51+
CORRECT
52+
RigidBodyGameObjectA
53+
RigidBodyGameObjectB
54+
RigidBodyGameObjectC
55+
56+
======================================
57+
FEEL FREE TO CONTRIBUTE TO THE PROJECT
58+
59+
Bullet Unity is an open source project in GitHub. Please feel free to clone the github repository and contribute:
60+
61+
https://github.com/Phong13/BulletUnity
62+
https://github.com/Phong13/BulletSharpPInvoke
63+
64+
3365

3466

Binary file not shown.

UnityProject/Assets/BulletUnity/Scripts/BCharacterController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void FixedUpdate()
117117
transform.rotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref trans);
118118
transform.localScale = BSExtensionMethods2.ExtractScaleFromMatrix(ref trans);
119119
}
120-
120+
/*
121121
public void OldUpdate()
122122
{
123123
float FrameDelta = Time.fixedDeltaTime; // todo put a variable in the physics world for this.
@@ -177,5 +177,6 @@ public void OldUpdate()
177177
transform.position = trans.Origin.ToUnity();
178178
179179
}
180+
*/
180181
}
181182
}

UnityProject/Assets/BulletUnity/Scripts/BCollisionCallbacksDefault.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class PersistentManifoldList
1818
List<PersistentManifoldList> newContacts = new List<PersistentManifoldList>();
1919
List<CollisionObject> objectsToRemove = new List<CollisionObject>();
2020

21-
public void Start()
21+
public virtual void Start()
2222
{
2323

2424
BCollisionObject co = GetComponent<BCollisionObject>();
@@ -32,6 +32,10 @@ public void Start()
3232

3333
public override void OnVisitPersistentManifold(PersistentManifold pm)
3434
{
35+
if (pm.NumContacts == 0)
36+
{
37+
return;
38+
}
3539
CollisionObject other;
3640
if (pm.Body0 == myCollisionObject)
3741
{

UnityProject/Assets/BulletUnity/Scripts/BCollisionObject.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public virtual CollisionObject GetCollisionObject()
122122
return m_collisionObject;
123123
}
124124

125+
//Don't try to call functions on other objects such as the Physics world since they may not exit.
125126
protected virtual void Awake()
126127
{
127128
m_collisionShape = GetComponent<BCollisionShape>();
@@ -141,12 +142,22 @@ protected virtual void RemoveObjectFromBulletWorld()
141142
BPhysicsWorld.Get().RemoveCollisionObject(m_collisionObject);
142143
}
143144

145+
146+
//Add this object to the world on Start. We are doing this so that scripts which add this componnet to
147+
//game objects have a chance to configure them before the object is added to the bullet world.
148+
//Be aware that Start is not affected by script execution order so objects such as constraints should
149+
//make sure that objects they depend on have been added to the world before they add themselves.
144150
internal virtual void Start()
145151
{
146152
m_startHasBeenCalled = true;
147153
AddObjectToBulletWorld();
148154
}
149155

156+
//OnEnable and OnDisable are called when a game object is Activated and Deactivated.
157+
//Unfortunately the first call comes before Awake and Start. We suppress this call so that the component
158+
//has a chance to initialize itself. Objects that depend on other objects such as constraints should make
159+
//sure those objects have been added to the world first.
160+
//don't try to call functions on world before Start is called. It may not exist.
150161
protected virtual void OnEnable()
151162
{
152163
if (!isInWorld && m_startHasBeenCalled)
@@ -155,6 +166,9 @@ protected virtual void OnEnable()
155166
}
156167
}
157168

169+
// when scene is closed objects, including the physics world, are destroyed in random order.
170+
// There is no way to distinquish between scene close destruction and normal gameplay destruction.
171+
// Objects cannot depend on world existing when they Dispose of themselves. World may have been destroyed first.
158172
protected virtual void OnDisable()
159173
{
160174
if (isInWorld)

UnityProject/Assets/BulletUnity/Scripts/BGhostObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal override bool _BuildCollisionObject()
6969
HashSet<CollisionObject> objsCurrentlyInContactWith = new HashSet<CollisionObject>();
7070
void FixedUpdate()
7171
{
72-
//TODO what if objects are destroyed. How will TriggerExit be called for that object? Should be tracking InstanceIDs
72+
//TODO should do two passes like with collisions
7373
objsCurrentlyInContactWith.Clear();
7474
for (int i = 0; i < m_ghostObject.NumOverlappingObjects; i++)
7575
{

UnityProject/Assets/BulletUnity/Scripts/BPhysicsWorld.cs

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
using BulletUnity.Debugging;
88

99

10-
// TODO order of destruction is an issue
11-
// TODO all constraints need to be removed from a rigid body before the rigid body is destroyed / removed
12-
1310
namespace BulletUnity {
1411
public class BPhysicsWorld : MonoBehaviour, IDisposable {
1512

@@ -667,109 +664,4 @@ public void OnPhysicsStep(CollisionWorld world)
667664
}
668665
}
669666
}
670-
671-
/*
672-
public class BDefaultCollisionHandler{
673-
public struct CollisionPair
674-
{
675-
public int obj0ID;
676-
public int obj1ID;
677-
678-
public CollisionPair(CollisionObject a, CollisionObject b)
679-
{
680-
int idA = a.BroadphaseHandle.UniqueId;
681-
int idB = b.BroadphaseHandle.UniqueId;
682-
if (idA < idB) {
683-
obj0ID = idA;
684-
obj1ID = idB;
685-
} else
686-
{
687-
obj0ID = idB;
688-
obj1ID = idB;
689-
}
690-
}
691-
692-
public override int GetHashCode()
693-
{
694-
return obj0ID ^ obj1ID;
695-
}
696-
697-
public override bool Equals(object obj)
698-
{
699-
if (obj is CollisionPair) return false;
700-
else
701-
{
702-
if (((CollisionPair) obj).obj0ID == obj0ID &&
703-
((CollisionPair) obj).obj1ID == obj1ID)
704-
{
705-
return true;
706-
}
707-
return false;
708-
}
709-
}
710-
}
711-
712-
public class ManifoldPoints
713-
{
714-
public int frameLastVisited = 0;
715-
public List<PersistentManifold> manifolds = List<PersistentManifold>();
716-
}
717-
718-
CollisionWorld m_world;
719-
Dictionary<CollisionPair, ManifoldPoints> pair2ManifoldMap = new Dictionary<CollisionPair, List<ManifoldPoints>>();
720-
721-
public void OnPhysicsStep()
722-
{
723-
int numManifolds = m_world.Dispatcher.NumManifolds;
724-
// collect manifolds
725-
pair2ManifoldMap.Clear();
726-
for (int i = 0; i < numManifolds; i++)
727-
{
728-
PersistentManifold contactManifold = m_world.Dispatcher.GetManifoldByIndexInternal(i);
729-
CollisionObject a = contactManifold.Body0;
730-
CollisionObject b = contactManifold.Body1;
731-
bool hasCallback = false;
732-
if (a is CollisionObject && a.UserObject is BCollisionObject && ((BCollisionObject)a.UserObject).onCollisionCallback != null){
733-
hasCallback = true;
734-
}
735-
if (b is CollisionObject && b.UserObject is BCollisionObject && ((BCollisionObject)b.UserObject).onCollisionCallback != null)
736-
{
737-
hasCallback = true;
738-
}
739-
if (hasCallback)
740-
{
741-
CollisionPair pair = new CollisionPair(contactManifold.Body0, contactManifold.Body1);
742-
ManifoldPoints pms;
743-
if (!pair2ManifoldMap.TryGetValue(pair, out pms))
744-
{
745-
//TODO implement pool
746-
pms = new ManifoldPoints();
747-
pair2ManifoldMap.Add(pair, pms);
748-
}
749-
pms.Add(contactManifold);
750-
}
751-
}
752-
753-
//second call the callbacks
754-
foreach (CollisionPair p in pair2ManifoldMap.Keys)
755-
{
756-
List<PersistentManifold> pms = pair2ManifoldMap[p];
757-
for (int i = 0; i < pms.Count; i++)
758-
{
759-
760-
CollisionObject a = pms[i].Body0;
761-
CollisionObject b = pms[i].Body1;
762-
if (a is CollisionObject && a.UserObject is BCollisionObject && ((BCollisionObject)a.UserObject).onCollisionCallback != null)
763-
{
764-
((BCollisionObject)a.UserObject).onCollisionCallback(pms[i]);
765-
}
766-
if (b is CollisionObject && b.UserObject is BCollisionObject && ((BCollisionObject)b.UserObject).onCollisionCallback != null)
767-
{
768-
((BCollisionObject)b.UserObject).onCollisionCallback(pms[i]);
769-
}
770-
}
771-
}
772-
}
773-
}
774-
*/
775667
}

UnityProject/Assets/BulletUnity/Scripts/Constraints/B6DOFConstraint.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace BulletUnity {
88
[System.Serializable]
99
public class B6DOFConstraint : BTypedConstraint {
1010
//Todo not sure if this is working
11-
//Todo breaking strength
1211
//todo should be properties so can capture changes and propagate to scene
1312
public static string HelpMessage = "\n" +
1413
"\nTIP: To see constraint limits:\n" +
@@ -95,6 +94,8 @@ internal override bool _BuildConstraint() {
9594
sl.AngularUpperLimit = m_angularLimitUpperRadians.ToBullet();
9695
sl.TranslationalLimitMotor.TargetVelocity = m_motorLinearTargetVelocity.ToBullet();
9796
sl.TranslationalLimitMotor.MaxMotorForce = m_motorLinearMaxMotorForce.ToBullet();
97+
sl.BreakingImpulseThreshold = m_breakingImpulseThreshold;
98+
sl.DebugDrawSize = m_debugDrawSize;
9899
return true;
99100
}
100101
}

0 commit comments

Comments
 (0)