Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[RELEASED] Bullet Physics For Unity

Discussion in 'Assets and Asset Store' started by Phong, May 31, 2016.

  1. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Its the ARMv8 that is the problem. According to the manual it is only possible to build for ARMv7 and x86 https://docs.unity3d.com/Manual/PluginsForAndroid.html.

    I don't see anything in the roadmap for updating Android to handle ARMv8.
     
  2. Frank-Yeh

    Frank-Yeh

    Joined:
    Jan 23, 2013
    Posts:
    3
    Hi Phong, thanks a lot, hope Unity will support it soon.
     
  3. Fluffy-Tails

    Fluffy-Tails

    Joined:
    Jun 28, 2009
    Posts:
    120
    With all of that being possible, I am getting a ton of errors when trying out your demo's.
    Most of the errors are related to the demo with WorldImporter, SoftBodies and smcs.rsp, it say's to use mcs.rsp instead.

    I recently downloaded the asset from the Asset Store and I am on 5.5.0f3.

    Is there a way to solve this?
     
  4. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Can you list a few of the errors here? Do you have a class called Demo in your project? I didn't put the Demo class in its own namespace and a few people have run into conflicts with classes called Demo in their projects. If so, if you rename that class to something else it should fix the problem.
     
  5. Fluffy-Tails

    Fluffy-Tails

    Joined:
    Jun 28, 2009
    Posts:
    120
    Here are some errors:

    Assets/Plugins/BulletUnity/BulletSharp/Dynamics/KinematicCharacterController.cs(156,25): warning CS0219: The variable `parallelDir' is assigned but its value is never used
    Assets/Plugins/BulletUnity/BulletSharp/Extras/BulletFile.cs(145,17): warning CS0219: The variable `dataPtrHead' is assigned but its value is never used
    Assets/Plugins/BulletUnity/BulletSharp/Extras/BulletXmlWorldImporter.cs(242,17): warning CS0219: The variable `ptr' is assigned but its value is never used
    Assets/Plugins/BulletUnity/BulletSharp/Extras/WorldImporter.cs(91,37): warning CS0219: The variable `childShapeType' is assigned but its value is never used
    Assets/Plugins/BulletUnity/BulletSharp/Extras/WorldImporter.cs(92,39): warning CS0219: The variable `childMargin' is assigned but its value is never used
    Assets/Plugins/BulletUnity/BulletSharp/Collision/GImpact/GImpactBvh.cs(473,32): warning CS0414: The private field `BulletSharp.GImpactBvh._primitiveManager' is assigned but its value is never used
    Assets/BulletUnity/Scripts/ProceduralMeshes/ProceduralPrimitives.cs(1424,9): warning CS0642: Possible mistaken empty statement
    Assets/BulletUnity/Examples/Scenes/BulletSharpDemos/BulletSharpDemos/BulletExampleRunner.cs(347,28): error CS0029: Cannot implicitly convert type `BulletSharp.SoftBody.SoftBody' to `SoftBody'
    Assets/BulletUnity/Scripts/SoftBody/SBSettings.cs(81,26): error CS1061: Type `SoftBody' does not contain a definition for `RandomizeConstraints' and no extension method `RandomizeConstraints' of type `SoftBody' could be found. Are you missing an assembly reference?

    According to the console, there are exactly 399 error messages with the red "!" sign after I clicked the 'Clear' button.

    I'm not a programmer, but I have looked up the 'class' you mentioned and I am unable to figure out how to make it work.

    Any suggestions?
     
  6. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    I think you have a class in your project called SoftBody. The imported BulletSharp class is conflicting with this one. Try putting your SoftBody class in a namespace or changing the name.
     
  7. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    423
    Hello!
    I am so excited about this asset! Thanks you!

    I read somewhere that it support double precision physics and allows to simulate physics far away from the center of the scene.
    But seems like by default physics works in single precision mode.
    Can you please say how to enable double precision mode?

    Also, there is a feature in the list:
    "Simulate multiple physics worlds simultaneously"
    I am not exactly understood what it means, but seems like exactly what I need : )
    Can you please explain how to enable this feature?
     
  8. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Hi Artaani,

    To get double precision, you will need recompile the libraries with the correct preprocessor flags set for double precision. I haven't tried it so I am not sure what the flags need to be. The github project for compiling the libraries is at: https://github.com/Phong13/BulletSharpPInvoke. There is a page on my website with notes on building the bullet physics library http://digitalopus.ca/site/building-bullet-physics-for-unity/ .

    I think you will also need to edit the wrappers to switch all float references to double in both the library projects and the C# wrappers.

    You will loose some precision reading and writing to the Unity Transforms since there is no way to make them double precision. This could be a problem if you are using kinematic rigid bodies far from the origin.

    You might want to consider a strategy where you use float precision but shift the world origin periodically and choose a scale that is friendly for the physics engine.

    Simulate multiple physics worlds simultaneously, means you can have different rigid bodies in different simulations. These can be stepped independently of each other and independently of the game clock. This is useful for games like angry birds where you want to simulate the effect of a shot many frames into the future without affecting game objects in the current frame. To use multiple simulations simultaneously, you will need to create multiple physics worlds. The BulletSharp API supports this, but the BulletUnity API does not (it currently uses a singleton). There are some offline examples in the UnityPackage that show how to do offline simulations.
     
  9. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    423
    Phong, thanks you for reply.
    It is sad that double precision is not available in this package. But thanks for the information. I will try to figure out how to recompile it.
     
  10. SeanCh

    SeanCh

    Joined:
    Dec 29, 2016
    Posts:
    1
    I found a bug on android that causes the native plugin to not load on some devices (this fixed samsung galaxy s5/s6 for me). in https://github.com/Phong13/BulletSh...ets/Plugins/BulletUnity/BulletSharp/Native.cs you are setting the plugin name to "libbulletc" and it should just be "bulletc". From https://docs.unity3d.com/Manual/PluginsForAndroid.html [DllImport ("PluginName")] "Please note that PluginName should not include the prefix (‘lib’) nor the extension (‘.so’) of the filename."
     
  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Thanks for the heads up. I will fix it.
     
  12. jknight-nc

    jknight-nc

    Joined:
    Jun 10, 2014
    Posts:
    52
    What would be necessary to bring GPU Rigid Body support to this project?
     
  13. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    The bullet physics library would need to be written to include that functionality. Bullet Physics 3 is supposed to include GPU simulating using CUDA. Bullet Unity currently uses Bullet Physics 2.8. However, it appears that development of Bullet Physics 3 is stalled. There is a forum post about it somewhere in the bullet physics forums. If I remember correctly, AMD was supposed to provide a team of coders to help with this but it never happened and no one has stepped up to do the work. Erwin seems to be working on the 2.X branch. The C# wrappers would also need to be updated but this should be fairly easy once the library is working.
     
  14. jknight-nc

    jknight-nc

    Joined:
    Jun 10, 2014
    Posts:
    52
    Thanks for the reply.

    Bullet appears to have some level of GPU support:
    http://bulletphysics.org/wordpress/?p=340

    and

    searching for GPU on the homepage (http://bulletphysics.org/wordpress/) says "Acceleration of the full collision detection and constraint solver on GPU"
     
  15. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    I think that code got rolled into the Bullet 3 the source tree. I am not sure what its status is. You could ask on the Bullet Physics Forums:

    http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=500&start=120
     
  16. makky0825

    makky0825

    Joined:
    Sep 16, 2016
    Posts:
    3
    How can I set Friction and restitution? I change Friction and Restitution in component of BRigidBody, but there are no difference.(when change mass and damping, change movement.)
     
  17. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    @Phong Hi Phong, is it possible to create a collision shape base on a mesh asset?
     
  18. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Changing these should work. I just did a test. Large sloping static plane with friction 0. A dynamic rigidbody with friction 0 falling onto the static plane. The cube falls and slides down the slope as expected. If I change the friction on the cube while it is sliding down the slope then the cube slows and stops.
     
  19. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Yes. You need to use one of:
    • BConvexHullShape (Fastest but not super accurate)
    • BConvexTriangleMeshShape (Good for dynamic triangle meshes)
    • BBvhTrinagleMeshShape (Awesome but only for static objects)
    If you need a dynamic triangle concave shape then it is best to decompose the triangle mesh into convex shapes and combine multiple BConvexTriangleMeshShape using a BCompoundCollider.
     
  20. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Thanks for your reply. BBvhTrinagleMeshShape works perfectly for my situation.I have tried BConvexHullShape and BConvexTriangleMeshShape before I asked you.But they didn't work well.

    Now there's another question about BOnCollisionEnter. I found that when BOnCollisionEnter was called there's still some distance between the two collided object's collider.Is it correct?And can you tell me which function is called to calculate the physic result about the collision?

    Thanks.
     
  21. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    You can check the source code to see what I am doing. I think I am iterating over all collision pairs in the physics world. However, Bullet has a margin so some of these collision pairs have no contact points. I think I considered trying to test for contact points for every collision but decided it was more efficient for the user to do this test as needed in the BOnCollisionEnter. It is only a true collision of there are contact points.

    To get the contact points you need to get your hands on the PersistentManifold object(s). If you dig into that you can get the contact points, colliders, forces etc...
     
  22. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Thanks for your patience and reply.I read the documentation.The default margin is 0.04.In my test it is more than 0.04.I'll do more test to find out the reason.
    Thanks again.
     
  23. makky0825

    makky0825

    Joined:
    Sep 16, 2016
    Posts:
    3
    Thanks, Phong.
     
  24. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    My guess is that it is detecting the collision pair in the Broadphase and noticing an axis aligned bounding box intersection. Then the Nearphase pass does not produce any collision pairs because the objects are not actually intersecting. I think some of the the DebugDraw features will draw gizmos showing when Bullet considers objects to be colliding.
     
  25. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Hi Phong. I found the issue occur when I rotate a oblong shape rigidbody. In DrawWireframe mode wireframe auto scales instead of rotating with the object.
    Can you tell me how to solve the problem?Thanks.
     
  26. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Hi Nosnew. I just pushed a fix for this to github. I modified BCollisionCallbacksDefault.cs to only create collision callbacks if there is actual contact.
     
  27. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Thank you very much.You helped me a lot.
     
  28. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    I have submitted a new version of BulletUnity to the asset store. It includes
    • CollisionCallbacks only produce callbacks if there is contact
    • Fix for CharacterController so it can be destroyed and instantiated multiple times
    • Debug draw will draw contact points
    • Fixes CompoundShape which could sometimes generate errors in console.
    Should be live in a few days.
     
    Inspeinre and Nosnew like this.
  29. Turisas

    Turisas

    Joined:
    Jul 11, 2012
    Posts:
    7
    Good day, Mr.Phong
    Thanks for free Bullet Physics in Unity.

    I've tried it and it looks cool. But also I've faced with roblems while building for HoloLens.
    Actually Unity fail to build empty scene. I've imported Bullet Physics from Asset Store. Then imported HoloToolkit ( this is HoloLens stuff ). Here is erros I've got

    Assets\Plugins\BulletUnity\BulletSharp\Collision\DbvtBroadphase.cs(38,60): error CS0246: The type or namespace name 'SuppressUnmanagedCodeSecurity' could not be found (are you missing a using directive or an assembly reference?)

    upload_2017-2-10_10-18-53.png


    This is Unity 5.5.0f3
    Build settings adjusted for HoloLens:
    Platform: Windows Store
    SDK: Universal 10
    Target device: Any Device
    UWP Build Type: D3D
    Selected Unity C# Project
    At "Other settings" seleted "Virtual Reality Supported"

    upload_2017-2-10_10-16-32.png

    Also I've tried build with this package ( iported in Empty scene)
    http://digitalopus.ca/_uploads/WSA.unitypackage

    But got almoste same result

    upload_2017-2-10_10-31-55.png

    Mr Phong or anyone please give me a suggestion or workaround to solve this issue?
     
  30. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    I believe that issue is caused by trying to use -unsafe in the smcp.rsp file. This is necessary because the BulletSharp code uses pointers to reference unmanaged memory.

    The workaround for this problem is to compile the BulletSharp code into a .dll and replace the BulletSharp source in your project with the .dll. When you compile the .dll. You will probably need to compile multiple versions of the .dll for UWP, and Windows or OSX (so it works in the editor). Each version will have a different command in native.cs for loading the library. You will need to modify native.cs when compiling each .dll so it loads the correct library. When you add these .dlls to your project folder you will need to set which platform they each should be used for in the inspector.

    Sorry about the messiness. This is what I had to do to get UWP to work.
     
    Last edited: Feb 12, 2017
  31. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Hi Phong, it's me again:D.Do you have any ideas to use bullet physics to do fracture?
    I read the Bullet Physic Manual and maybe I should use btCompoundShape.
    Can you give me more details about that? Thanks.
     
  32. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    I haven't explored this at all myself, but I think one of the examples does this. ExampleRunner -> Demos -> Convex Decomposition Demo.
     
  33. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Hi Phong. I tried to understand the demo but I'm such a novice to understand the demo.
    Maybe I should break mesh into smaller pieces and add rigidbody to do fracture.Anyway,thanks.:D
     
    Last edited: Feb 21, 2017
  34. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Hi Phong. Sorry to bother you again and again.I have some trouble with "BOnCollisionEnter".
    Sometimes collision happened but BOnCollisionEnter not be called.
    I have a character object and a ball object.
    Character and ball object's hierarchy is like:
    Hierarchy.PNG
    CharacterBody, CollisionDector and Ball has rigidbody each other.

    CharacterBody's Rigidbody:Control to move character
    CharacterBody.PNG

    CollisionDetector's Rigidbody:Control to kick ball
    CollisionDetector.PNG

    Ball's Rigidbody:Control ball's physics.
    Ball.PNG

    I did some debug work but stuck in "OnPhysicsStep".
    Can you do me a favour? Thanks.
     
  35. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    When you say that the collision happened, are you seeing the objects pass through each other or are the objects bouncing but BOnCollisionEnter is not being called?

    If the objects are passing through each other then the problem probably with the flags. Try setting your three objects to the default flag settings Nothing, Default Filter, All Filter. If it works then carefully modify the flags.

    Be careful setting the "Collision Flags". I see you have added a "ball" flag. These flags are hooked to code in the bullet library, they are not like layers in Unity. It could be that this is messing you up.

    I am a little fuzzy on what the collision detector object is for. Is it on the ball or on the player's foot? For your collision detector you may want to use a Trigger (Ghost object) instead of a full rigid body.

    Which object is your OnCollisionEnter script attached to? Can you post your collision detection script?
     
  36. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Hi Phong.Sorry for my bad description.
    1."collision happened" means the objects are bouncing but BOnCollisionEnter is not being called.
    2.I did create new "Collision Flags".So it's better to use layers in Unity other than "Collision Flags"?Also I created new "Collision Groups" and "Collision Mask".Is that not a good choice,too ?
    3.The "collision detector" is on the player's foot.Only the "collision detector" can collide ball.So I can adjust the shape of "collision detector" to control the ball and it won't affect the move of character.I'll learn how to use Ghost object.
    4.OnCollisionEnter script is being attached to ball.The code is below.
    Code (CSharp):
    1. public override void BOnCollisionEnter(CollisionObject other, PersistentManifoldList manifoldList)
    2.     {
    3.         Debug.LogError("Collision Flag:" + other.CollisionFlags);
    4.         if (other.CollisionFlags == BulletSharp.CollisionFlags.CharacterObject)
    5.         {
    6.             IsShotten = true;
    7.         }
    8.     }
    Thanks for your reply.
     
  37. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    It is probably fine to create your own Collision Groups and Collision Mask. I would be careful creating your own Collision Flags.

    I am not sure why the collision detection wouldn't be working. I would suggest stepping with the debugger. Look in class BPhysicsWorld. There is a class in there called BDefaultCollisionHandler. It steps through the collision manifolds looking for any collision pairs that have a collisionCallbackEventHandler. If the handler is there then it should dispatch an event.
     
    Nosnew likes this.
  38. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Thanks a lot,Phong. I'll have a try.
     
  39. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Hi Phong. I found when the objects are bouncing "OnVisitPersistentManifold" did be called. But inside "OnVisitPersistentManifold" sometimes "pm.NumContacts" is 0 even collision happened. I think I need more time to read the C++ source code to find more info.
    By the way, I also find "OnVisitPersistentManifold" has been called even objects not bounce each other but they're really close to each other.When I enabled "Do Debug Draw" I find "OnVisitPersistentManifold" would be called while objects' wareframe contact others.
     
    Last edited: Mar 1, 2017
  40. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Hi Nosnew, persistent manifolds are created when there is AABB overlap between objects and persist as long as there is AABB intersection. There may not be actual contact. It is worrisome that sometimes there is collision response but no contact. I will have to look into that.
     
    Nosnew likes this.
  41. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    Hi Phong, I found some clues and want to share with you.
    Depend on the documentation:
    When I set m_maxSubsteps(in BPhysicsWorldLateHelper) to 1 and comment out Update(in BPhysicsWorldLateHelper) everything is going well.
    Maybe collision callback should be called when internalSingleStepSimulation(in btDiscreteDynamicsWorld. ) is being executed?
     
  42. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Ahh, this makes sence. The CollisionCallback code I wrote just loops over the bullet collision manifolds. If more than one bullet simulation step occurred then it could be possible to miss collisions. I hope to have some time to look into this but am very busy at the moment unfortunately. One problem with your suggestion is that collision callback could be called multiple times per frame on the same object.
     
  43. Nosnew

    Nosnew

    Joined:
    Apr 18, 2014
    Posts:
    20
    You are totally right.It did be called multiple times per frame on the same object.So I just make bullet simulation step occurs once per physics frame.And it fixed my problem.
    Thanks, Phong.
     
  44. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    is there way to change values at runtime for soft body? i need to change stiffness, is it hard to implement?
     
  45. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    You can get the the BulletSharp softbody object with if (bsb is a BSoftBody):


    SoftBody sb = (SoftBody) bsb.GetCollisionObject();

    You can change whatever is changable through the sb object.
     
  46. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    i change stiffness and nothing happens, i do something wrong?

    Code (CSharp):
    1.  
    2. using BulletSharp.SoftBody;
    3. using BulletUnity;
    4. using UnityEngine;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour
    7. {
    8.  
    9.     void OnGUI()
    10.     {
    11.         var mesh = GetComponent<BSoftBodyWMesh>();
    12.         var softBody = (SoftBody)mesh.GetCollisionObject();
    13.         foreach(var pm in softBody.Materials)
    14.             pm.LinearStiffness = GUILayout.HorizontalSlider(pm.LinearStiffness, 0, 1, GUILayout.Width(300));
    15.     }
    16. }
    17.  
    18.  
     
  47. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    I suspect the stiffness is only being applied to the object when the object is created. Look at the class SBSettings. You probably need to call ConfigureSoftBody after adjusting the settings.
     
  48. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
  49. AKQJ10

    AKQJ10

    Joined:
    Feb 9, 2012
    Posts:
    33
    Hi, @Phong,
    thanks a lot for making this great tool. I have one quick question, is it possible to make elastic ropes? The rope has one end fixed on the headObject, and the other one end free. When the head moves, the rope will swing around, then the damping will slow it down, and finally the rope will rest in its original shape(our goal is to simulate a hair braid)? Right now, as in the demos the rope will end up hanging down as straight lines as the gravity is pointing down?
     
  50. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,083
    Yes, instead of using a soft body rope, you can use a soft body mesh. By adjusting the configuration you can make this as stiff or floppy as you like. Look in the example scenes. There is one for driving a high poly skinned mesh using a low poly simulated softbody. This does something similar to what you are after. I have a tutorial on the setup here:

    http://digitalopus.ca/site/bullet-physics-tutorial-attaching-softbodies-to-skinned-meshes/