Search Unity

  1. 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,082


    The high performance open source Bullet Physics library now available within Unity. Wanting more flexibility than I could get using PhysX, I started this Unity port of the open source BulletSharp project. Bullet Physics is compiled as a native plugin and accessed from unity scripts using C# PInvoke wrappers.

    This is a free Open Source project started by Unity author and physicist Ian Deane, creator of Mesh Baker and Fast Shadows. Much of the thanks for this project goes to Andres Tracks, the author of the BulletSharp wrappers that wrap the Bullet Physics library so that it can be accessed from C# code. Thanks also to the original author of Bullet Physics, Erwin Coumans.

    Features
    • Simulate multiple physics worlds simultaneously
    • Simulations independent of game loop
    • Access to every byte of source code
    • Softbodies, ropes, cables and cloth
    • Featherstone inverse dynamics
    • Two APIs:
      • Low level Bullet Physics API
      • Unity components similar to the PhysX components
    • ZLib license (very permissive)
    Asset Contents
    • native Bullet plugins for x86, x64, iOS, Android and OSX
    • C# wrappers for the native plugins
    • Demos
    • Unity Bullet Components
    Bullet Unity Contains Two APIs
    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 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.
    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. These components are synchronized with Unity’s FixedUpdate loop.
    Requirements
    Links






     
    Last edited: Jun 17, 2017
    AntonioModer, dyupa, Bamfax and 10 others like this.
  2. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    The Unity package should include an smcs.rsp file in the root of the Asset folder. This file should include the text:

    -unsafe

    It is needed to allow use of pointers needed to interact with the Bullet Physics library. If you don't import the smcs.rsp file or it is not in the package you will need to add this file or add this line to your existing smcs.rsp file.
     
  3. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    hello Phong. I tried to run the Bullet Physics, but I get this error
    Failed to load 'Assets/Plugins/BulletUnity/Native/x64/libbulletc.dll' with error 'The specified module could not be found.
    ', GetDllDirectory returned ''. If GetDllDirectory returned non empty path, check that you're using SetDirectoryDll correctly.
    etc.
    the dll is there, if I click on it, settings look like this (see picture)
    what should I do (the rsp file is ok)
     

    Attached Files:

  4. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    I just tried it in Unity 5.3.5. Clean project 64bit editor, fresh asset store download, build target standalone and it worked. Which version of Unity are you using? Also is it 32 or 64 bit editor and which platform and build target?
     
  5. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    This looks so awesome! Thanks for sharing! Cant wait to try it :)
    Can i use it wirh the standard unity physics stuff? Radolls for example
     
  6. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    The simulations are not aware of each other. If you have two rigid body components on the same object that are both non-kinematic then they will both try to control the object with bad results. However if one of the rigid bodies is kinematic then that works. I created a cool experiment where I simulated a building collapsing in slow motion using Bullet using dynamic rigid bodies and a very small timestep. Then I attached PhysX colliders and kinematic rigid bodies. My character (all PhysX) could run across the falling bodies like the Flash in X-Men. Very cool!
     
  7. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    Haha. That sounds cool. Would love to sea a video :D

    But they can effect each other, right? So for example a normal unity rigidbody cube and a bullet physics flubber ball? :D
     
  8. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    tried it in 5.31p4 (64bit) and 5.34f1 (64bit)

    now I installed 5.35p3, downloaded again, and I got this
     

    Attached Files:

  9. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    That error happens if there are no build platforms installed. For some reason Unity defaults to WebPlayer which is deprecated and doesn't allow code in the project capable of writing a file due to the webplayer sandbox. Switch the build platform to Standalone. To do this you probably need to download the Standalone platform using the Unity downloader.
     
  10. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    yes, I realized that I dont have windows module installed. sorry about that.

    but installed again and the bullet.dll problem is there again (checked the windows platform settings on, but didnt help)
     

    Attached Files:

  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Hmmm, I would love to know why this is happening. Are you using the 32 bit editor? If so, make sure that:
    • for x64 libbulletc (untick all platforms and untick editor and untick x86)
    • for x86 libbulletc (tick editor, tick x86)
    • restart Unity
    Also do you have Visual Studio installed or just Monodevelop. It may be that Visual Studio installs some redistributable libraries that are needed. If this is the problem I would like to see if I can recompile bullet natives so without that dependency.
     
  12. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    Another question: is it mac compatible?
     
  13. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    If the Unity rigidbody cube has only PhysX components and but bullet flubber ball has only bullet components then they won't see each other. Its like they are in different dimensions. However if you add a PhysX collider to the ball, then other PhysX components would see that collider, however it would be like a kinematic object. It wouldn't react to being hit unless you added a script that would detect the collision, grab the impact impulse and apply it to the Bullet rigid body. The result would be some sort of weird intradimensional interaction.
     
    Der_Kevin likes this.
  14. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Yes, currently it supports x86, x64, Android, OSx and iOS. Other platforms can be added it is just a matter of downloading the source from github and compiling a build for that platform. There will probably never be a build for web platforms because the sandbox won't allow native plugins.

    Note that when I tested the package from the asset store on my mac it lacked the smcs.rsp file. I had to create it by hand.
     
    Last edited: Jun 11, 2016
  15. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    that might be the problem. when installing I had some error about visual studio. (there is only monodevelop on my comp, as far as I know)
     
  16. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
  17. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    i finaly found some time to test it, but it doesent work on my machine ( Mac OSX El Capitan, Unity 5.3.4f1)
    actually all scenes are frozen and these are the errors:
    DllNotFoundException: libbulletc
    BulletSharp.AlignedManifoldArray..ctor () (at Assets/Plugins/BulletUnity/BulletSharp/LinearMath/AlignedManifoldArray.cs:84)


    DllNotFoundException: libbulletc
    BulletSharp.DefaultCollisionConfiguration..ctor ()
    BulletUnity.BPhysicsWorld._InitializePhysicsWorld () (at Assets/BulletUnity/Scripts/BPhysicsWorld.cs:488)
    BulletUnity.BPhysicsWorld.Get () (at Assets/BulletUnity/Scripts/BPhysicsWorld.cs:60)
    BulletUnity.BPhysicsWorld.Awake () (at Assets/BulletUnity/Scripts/BPhysicsWorld.cs:263)
     
  18. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Thanks for the report. I just had a look and the OSX plugin folder is empty. Not sure when that got deleted. I am pushing a new build up today so I will definitely fix that.

    [UPDATE]

    I submitted the new build. It takes a few days to go live. In the meantime you can download it from github if you want. https://github.com/Phong13/BulletSharpUnity3d
     
    Last edited: Jun 14, 2016
    Der_Kevin likes this.
  19. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    thanks! its working now.
    i got another noob question ;)
    is there any way to let bullet physics object collide with a unity collider?
    i just used the ragdoll sample for example and put a normal unitybox with kinematic rigid body in there but it slips threw the boy. is there a solution or does it just not work?

    what iam also try to understand:
    why does these two lovely object from the demo scene are colliding with the ground but not with each other?
    bulletphysics.gif
     
    Last edited: Jun 15, 2016
  20. gian-reto-alig

    gian-reto-alig

    Joined:
    Apr 30, 2013
    Posts:
    756
    Finally some movement on the physics engine front.

    Questions:
    - Are there Wheel Collider systems in Bullet, or do I have to code such a system from scratch? I guess there are tutorials on how to do that on the net?
    - Does Bullet support NON-CONVEX mesh colliders? Those were removed from PhysX when the version was upgraded with Unity 5, which broke my code at the time.
    - Is there some Raycasting system in Bullet?
    - What is the performance compared to PhysX 3.3 as currently implemented in Unity? Better - Worse - About the same?


    Thanks for porting and your work!
     
  21. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    It is like the Unity physics world and Bullet Physics world are in completely different dimensions. There are a few ways to get them to interact to a limited degree:

    1) You can make one of the Rigidbody components dynamic and one kinematic. One world is doing the simulation, the other is along for the ride.

    2) You can add a PhysX collider to a Bullet Rigidbody. This is a lot like option 1

    3) You can add a PhysX trigger to a Bullet Rigidbody.

    4) You can add a script which will detect a collision, grab the contact forces and apply them to the object in the other world

    There are a few things that can affect what collides with what.

    1) If there is a constraint between the objects you can check that you don't want collisions between those objects

    2) The first three fields in the Rigidbody inspector control the collision mask. These are similar to the collision layers matrix in the PhysX physics properties.
     
  22. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    I have not seen a special WheelCollider like PhysX has in the bullet code. There is a simple hover car demo in the examples.

    Bullet supports three different types of mesh colliders:
    • ConvexHull
    • TriangleMesh
    • BvhTree (supports non-convex but I think it is restricted to static geometry)
    There are some utilities for doing convex decomposition which I haven't explored.

    There is a raycasting system. Some of the demos use it.

    I confess that I have not done a head to head comparison with PhysX 3.3 yet. I think it would be favorable because bullet dispenses with a lot of things that make calculations more complex (scaling the transform. rigidbodies with pivot not at center of mass). These are things that are reasonably easy to work around and can speed up the physics a lot if the engine does not need to deal with them. On the down side, bullet needs to go through the scripting code which can slow things down.

    The main reason I did this was that I wanted to be able to do physics simulations that are not tied to the game loop. Path prediction lines, bullet time etc...
     
  23. Flurgle

    Flurgle

    Joined:
    May 16, 2016
    Posts:
    389
    This is amazing.
     
    KEMBL and Phong like this.
  24. str3lok

    str3lok

    Joined:
    May 27, 2012
    Posts:
    24
    Hi.
    We have a problem with the plugin for iOS (7.1). App crash in the method getOrientation (see screenshot). We use the latest version of the plugin from the Asset Store.
     

    Attached Files:

  25. Flurgle

    Flurgle

    Joined:
    May 16, 2016
    Posts:
    389
    Is there functionality to break apart meshes (like a bullet through a wall)?
     
  26. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    I will take a look but am away from my Mac for the weekend so I won't be able to look until Monday.
     
  27. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    There is a convex decomposition utility. I haven't looked at it myself, but the example scene ConvexDecompositionDemo (came with BulletSharp) uses it.
     
    Flurgle likes this.
  28. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    We came home early so I did a quick test but was not able to reproduce the problem. I think the problem is likely that I am using iOS 8.4 (my only iOS device). The plugin was compiled with XCode 6.4. Does it work for a time then crash or does it crash right away? Do the example scenes work?

    The test I used was a simple scene with only one script:

    BulletUnity\Examples\Scripts\BulletHelloWorld.cs

    This creates a world steps the simulation three hundred times and disposes of everything. I modified it to print the orientation every step:

    Code (CSharp):
    1.  
    2. BulletSharp.Quaternion q = fallRigidBody.Orientation;
    3. Debug.Log(q);
    4.  
     
  29. str3lok

    str3lok

    Joined:
    May 27, 2012
    Posts:
    24
    Crash consistently repeated, but inside the current project. If I try to move it to a separate test project, it is not repeated. Debugging, I realized that it is affected by other code, unrelated directly with physics.

    P.S.
    But maybe I'm wrong. I need more time to debug.
     
    Last edited: Jun 20, 2016
  30. gian-reto-alig

    gian-reto-alig

    Joined:
    Apr 30, 2013
    Posts:
    756
    Thanks for your reply...

    Hm, the triangleMesh Collider sounds mighty interesting. If it is the old PhysX Meshcollider, and you can just use any old mesh you created, then I would have the functionality back I am missing from PhysX 3.3!
    Used the meshcolliders back in PhysX 2.8 to create my own armour models, done with splitting up a simplified mesh of the visual model into armour groups and importing these armoru groups as their own meshes, create a meshCollider with it, and reassamble the original model from the individual mesh colliders. Was surprisingly easy to setup once the armourgroup meshes have been created, and worked like a charm (don't know about perfromance, given how I was also doing a lot of other physics and the game was running at a reasonable framerate with 4 vehicles, the impact was rather moderate).
    Of course, for such a system, a) cannot use the PhysX standart convex meshcolliders as they are inaccurate as hell, b) cannot use any CONVEX decomposition as the collider no longer forms a thin armour strip (unless I isolate every armour plate into its own mesh, which would increase the amount of colliders a lot).

    Do you know if there is a good workflow to create a TerrainCollider for your Bullet Physics port? Given I might want to write my own wheel colliders anyway (using a custom terrain shader that uses tesselation and smoothing, so visual terrain does not reflect the terrainCollider 100%, wheelColliders have to adjust for that somehow), if the terrainCollider can be somehow converted, I could get rid of PhysX altogether.
    Is PhysX running in the background for a Build if nothing uses PhysX, or is it culled from the Built game in this case? If not, can I somehow cause Unity manually to exclude PhysX?

    If you say "bullet needs to go through the scripting code which can slow things down"... do we have any indication / documentation on how big this impact is?
     
  31. str3lok

    str3lok

    Joined:
    May 27, 2012
    Posts:
    24
    Well I was able to put the code in a separate project. You can download it here: http://www.filehosting.org/file/details/578779/BulletCrashTest.zip
    The problem is most likely with the release/memory usage. Because if you just comment out the block of "if" before calling my method ProcessPhysics, the crash disappears.

    Code (CSharp):
    1.    
    2.     void Update()
    3.     {
    4.         if (Input.GetKey(KeyCode.Escape)) // if you completely comment out this block, the crash will disappear!
    5.         {
    6.             Application.Quit();
    7.         }
    8.  
    9.         player.ProcessPhysics();
    10.     }
    11.  
     
  32. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Hi Gian,

    There is good info on the colliders here:

    http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Collision_Shapes

    I am not sure but I think the TriangleMeshCollider may need to be convex. and the bvhTriangleMeshShape is supposed to be static.

    Bullet comes with a btHeightfieldTerrainShape. I have a component that can be added to a Terrain to replace the PhysX terrain collider.

    PhysX still runs, I don't believe there is any way to get rid of it. It is deeply integrated with Unity. However if there are no PhysX components attached to any game objects in the scene then the PhysX physics loop should be very, very light.

    Still need to do some benchmarking.
     
  33. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    I will try to take a look at this later today.

    ...
    Sorry, I didn't get to it. It will be a few days now before I can look at it. Sorry.
     
    Last edited: Jun 21, 2016
  34. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Hi str3lok,

    I had a look at the code on my Windows machine. Will try it later on my iPad. One thing I noticed is that both Player.cs and LevelController.cs do cleanup on the physics world in OnDestroy(); OnDestroy is not governed by script execution order so I think that one of these would fail or at least throw a null pointer exception. I don't think this relates to the crash you describe though.
     
  35. Leuthil

    Leuthil

    Joined:
    Jul 26, 2013
    Posts:
    97
    I'm very interested in this for multiplayer physics-based games. I'll try to do some testing to see how feasible it would be. The example scenes look very good but using the pre-made components were a bit confusing to me and didn't work as I expected, but I'm sure I'm just using them wrong.

    Edit: I figured out my problem. I couldn't figure out how to make static collision objects but now I see that I just have to set the mass to 0.
     
    Last edited: Jun 24, 2016
  36. str3lok

    str3lok

    Joined:
    May 27, 2012
    Posts:
    24
    Hi, Phong.

    The crash can be repeated only on the iOS device. No cleanup code, as it makes no sense for this test application.
     
  37. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Hi Leuthil,

    You should also set the "Collision Flags" to "Static Object". If you do this then the mass field will be set to zero when the rigid body is created.
     
  38. Leuthil

    Leuthil

    Joined:
    Jul 26, 2013
    Posts:
    97
    Great! If all goes well I am planning to create a test scene with client-side prediction working with UNet. Perhaps if it's successful you can add it as an example to the project. I think this will be a pretty common use-case for game devs to use Bullet Physics instead of the built-in PhysX.
     
  39. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    That sounds awsome! Let me know if you have any questions or need things added. The SimpleOfflineSimulation might help when creating the prediction simulation. I also came across this excellent looking article on Networking physics that I bookmarked a while back: http://gafferongames.com/game-physics/networked-physics/ When I had time I intended to try to it out.
     
  40. Leuthil

    Leuthil

    Joined:
    Jul 26, 2013
    Posts:
    97
    I will keep you posted. I have a few game ideas that I haven't been able to accomplish because of the lack of this feature so I am interested in getting it working with this. And yeah that's the article that I am using as a reference :).


    Edit: I seem to be having some glitch regarding sleeping rigidbodies. If I try to control a dynamic rigidbody with forces and I give it time to deactivate, my forces no longer do anything to it. Putting sleep thresholds to 0 fixed the problem. Note: I'm using BRigidBody.
     
    Last edited: Jun 25, 2016
  41. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    I thing there is a flag for never sleep that you can set.

    You can also check if the rigid body is sleeping an activate it before applying your forces.

    BTW make sure you are using ApplyImpulse, instead of ApplyForce. ApplyImpulse is for individual pulses. ApplyForce is for lingering forces that are applied over many timesteps. It is unreliable when applied at only one timestep.
     
  42. Leuthil

    Leuthil

    Joined:
    Jul 26, 2013
    Posts:
    97
    Hmm ok I'll see if I can figure out how to set that flag. And I believe I did try to Activate but still had the same problem. Perhaps it's not sleeping that is causing the issue but actually something else... not sure.

    And yes I am using Impulses! But thanks for the explanation because I didn't realize that, I was just using impulses because it felt better haha.

    Just a progress update:

    I have a sample scene working with Bullet Physics balls spawning that players can control. It is using UNet and a new ball for each player spawns as players connect. Next on my list is to attempt to get offline simulations working correctly so that I can use it for client side prediction... wish me luck ;).
     
    Last edited: Jun 28, 2016
  43. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
  44. juliocdep

    juliocdep

    Joined:
    Sep 30, 2010
    Posts:
    30
    Is it better use this engine instead of the PhysX for a game like Besiege?
     
  45. Leuthil

    Leuthil

    Joined:
    Jul 26, 2013
    Posts:
    97
    Awesome thanks :) I'll give that a try.

    Is there some other way I can contact you instead of spamming this forum thread? Unless you prefer it so others can see.

    Those methods would be very useful. I haven't had a chance yet to try to get the offline simulation working but if I'm lucky I can get some time this week :).
     
  46. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Beseige is very impressive. This is a guess on my part but they are probably not using any out of the box physics engine. No out of the box physics engine that I am aware of has the ability to attach huge numbers of constraints together like that and be stable. It might be a modified soft body simulation. They may have started with something like bullet or havock but probably modified it.

    IMO you couldn't build that with PhysX without having access to the source. I doubt you could do it with out of the box bullet either.
     
  47. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Sure, you can use contact.digitalopus@gmail.com. For my email. That is probably best. It it is a support question I people to post here so others can see the answers. I probably won't get my changes up this week. Kids are out of school, Canada day etc.. hard to get any work done.
     
  48. Zogrim

    Zogrim

    Joined:
    Jun 29, 2016
    Posts:
    1
    Just a remark - afaik Besiege is done with default PhysX engine.
    That's why they have announced hage performance improvements when switching from Unity 4 (PhysX 2.8) to Unity 5 (PhysX 3.3)
    "Unity 5 uses a much newer version of PhysX which has support for multithreading, which will allow Besiege to use your CPU far more effectively than in the past."
    http://steamcommunity.com/games/346010/announcements/detail/806498976982433279

    Also, for anyone looking for physics engines performance comparison, there is a very nice tool called PEEL (Physics Engine Evaluation Lab)
    https://github.com/Pierre-Terdiman/PEEL
    Includes Bullet, various PhysX versions and Newton
     
    StaffanEk likes this.
  49. Leuthil

    Leuthil

    Joined:
    Jul 26, 2013
    Posts:
    97
    Ok sounds great. I'll email you probably next week. I'm from Canada as well so this weekend is a write off :).
     
  50. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,082
    Wow! I am impressed. Thanks for the info.