Search Unity

Unity Bullet Physics Plugin Demo

Discussion in 'Assets and Asset Store' started by chbfiv, May 9, 2012.

  1. Jiraiyah

    Jiraiyah

    Joined:
    Mar 4, 2013
    Posts:
    175
    Guys can i suggest something, how about a little community of coders gather together and port bullet to unity ourselves? a team of 4-5 coders i mean, if anyone interested contact me on skype : alireza.rpg or by email : jiraiyahsama@yahoo.com
     
    Nition likes this.
  2. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I had a similar idea. This would make a great Open Source project. I have had some success with Andres Traks BulletSharp project. I was able to get the PInvoke branch https://github.com/AndresTraks/BulletSharpPInvoke partially working with Unity. Dynamics seems to work but SoftBody is generating errors. Would love to share what I have so far.

    There is also another project in Github https://github.com/sanglin307/BulletUnity3D. Looks like it has not been contributed to in three years but someone put some work into this.

    Here is what I accomplished with BulletSharp
     
    Last edited: Nov 23, 2015
  3. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi Everyone,

    I have done some more work on integrating Bullet Physics with Unity using the Native Plugin with BulletSharpPInvoke wrappers it is working very well. I don't think it will take very long to get a high quality set of wrappers for the bullet library. All the examples I have tested so far are working. I have checked my code into Github, if anyone wants to help please PM me.
     
  4. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    AdamGoodrich, RatherGood and moure like this.
  5. RatherGood

    RatherGood

    Joined:
    Oct 12, 2014
    Posts:
    22
    This is awesome! Nice work.

    Give me some time to wrap my head around this. I might be able to help out.
     
  6. RatherGood

    RatherGood

    Joined:
    Oct 12, 2014
    Posts:
    22
    The Bullet softbody is awesome. This could be a great tool with a little love.

    For some reason I thought I was smart enough to write my own softbody simulation engine and have been playing with it for some time. Great learning experience but its never going to happen in this lifetime. Not if I ever want to finish an actual game I want to make and I have exceeded the limits of what Unity physics can handle unfortunately.

    I have a few suggestions:
    • Put the BulletSharp project scripts in the "Plugins" folder along with the DLL. This way if you eventually create a BulletSharpU(with unity mods) DLL, you can remove the Bullet sharp scripts and replace with the DLL. I tried this but some of the BulletSharp utilities/extensions were interdependent.
    • "BulletForUnity" or whatever, namespace to all your new stuff.
    Some ways I can help:
    • I have some experience in creating editor extension and can finish setting the bullet basic primitives with menu shortcuts. Looks like you started that work also.
    • We should create a structure that mimics (looks like) Unity types. Similar UI and parameters that closely match what Unity users expect. That will make this more palatable to non programmers.
    • A GameObject/scene converter that automatically replaces Unity types with bullet types. And vice versa.
    Let me know what works for you and what would be the most beneficial.
    Again, very nice work!
     
  7. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks RatherGood,

    I too struggle with facing my own limitations. I am giddy about being able to find and leverage a high quality product like Bullet in Unity.

    Your suggestions are good. I think I have managed to fix the first one already. I am hoping to enter my game EVA Infinity in the Windows Store competition. To do this I needed to build the wrappers into a DLL because unity didn't like "unsafe". For some reason the smcp.rsp flag didn't work.

    Thanks for the offer of help. All of these things would be great!. I actually don't have much spare time to work on this (1 hour per day) so progress is slow. Some of them may be premature since the Unity Components aren't very complete yet. I would consider them early Alpha. Here is my current todo list:

    1) More CollisionShapes. Currently only have SphereShape and BoxShape. At the very least we need Capsule and Bullet MeshCollider equivalent. MultiSphere would be great as well.
    2) Integrate the Bullet DebugDraw with the OnGizmos for the implemented collision shapes when they are live in the Bullet world.
    2.b) Finish implementing the DebugDraw callbacks.
    3) Some of the CollisionShapes support local non-uniform scaling (Box, MultiSphere and TriangleMesh I think, check manual). Would be nice to support this.
    4) Something to replicate OnCollisionEnter, OnCollisionStay, OnCollisionExit if practical/possible. Could be a "register a delegate" system of some sort.
    5) Some sort of Trigger component.
    6) Constraints, I haven't even started looking at this yet.
    7) Get more demos working.

    There are also some significant bugs (probably lots more because I haven't explored much of the API):

    1) SoftBody.GetLinkVertexData crashes Unity on some platforms. A workaround is to loop over the nodes and get the positions but it would be nice to know why this crashes Unity.
    2) The MotionState callbacks don't seem to work with IL2CPP. Appear to need to add a decorator to the callback. Need to investigate more.
     
  8. RatherGood

    RatherGood

    Joined:
    Oct 12, 2014
    Posts:
    22
    50 objects with about 15 softbodies. A little flat. ;)




    Phong, I'll PM you the code. You can include what works for you. I'm still figuring this out but the results are very good so far!

    Edit: Changed scene to UnityPLayground. Dropping 468 poly Wolves like rain. Includes, rope, other softbodies and rigidbodies all interacting![/MEDIA]
     
    Last edited: Jan 3, 2016
  9. RatherGood

    RatherGood

    Joined:
    Oct 12, 2014
    Posts:
    22
    Huge amount of SoftBody objects and decent frame rate! Coming along nicely.

     
    Last edited: Jan 3, 2016
  10. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
    Will this technique of using Bullet SDK within Unity also work on iOS / Android devices?

    Keep up the great work!
     
  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    We haven't got that far but it should work. It all hinges on being able to build Bullet as a native plugin for those platforms. Unity has examples showing how to build Native plugins in C and C++ for those platforms and bullet is a well written .cpp library designed to be portable so I don't anticipate it will be hard to compile it for those platforms.
     
  12. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
    As this is something that I'd like to test out on mobile, and sometimes money for beer can be a great motivator, I'm happy to put $250 towards getting a native Unity plugin for Bullet that will work on mobile.

    Soft body isn't required, if the plugin had the following, I'd be happy to PayPal the money over...
    - rigid body ( basic primitives as well as mesh support, even just convex )
    - static support for concave meshes
    - support for stepping the physics ahead, with determinism.
    - control over the step size etc
     
  13. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi crazyFolker,

    I have a build for Windows 10 Phone, (for the Unity competition), but I see your thinking iOS and Android. I will try to get something going on iOS it might be a week or so though since its crunch time for the Windows Store competition. I don't own an Android device so it might be a bit for that one.
     
  14. mew_fi

    mew_fi

    Joined:
    Oct 1, 2014
    Posts:
    17
    Hey, Phong.

    I have a specific question in regards to the soft body physics movement. Is it possible to apply torque forces to soft bodies?

    For example if you had a soft body sphere, could you give torque forces to it to roll it around, or is it like the Unity 4 interactive cloth object which can only be moved with "External acceleration", aka wind?
     
  15. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I have not tried that specifically, but the API appears to allow it. SoftBody inherits from RigidBody so all the AddForce, AddTorque functions are available. It would be easy to test. In the SoftBody demos
    there is a BunnyCar (3:15). You could add some torques to the wheels to see what happens. After watching this I think someone could make a very goofy fun game with soft body jelly like vehicles.

    UPDATE:

    I was wrong. There are no "AddTorque" functions in the SoftBody API. However there is "AddForce( force, node)" Where node is a node in the soft body. By applying forces to nodes on the surface of the soft body you could create rotations.
     
    Last edited: Jan 18, 2016
  16. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
    Hi Phong,

    That sounds good. I meant to add that the plugin should be available for everyone ( hopefully it was implied! ) so that more people can easily try it out and make it more robust etc.

    If you get it working a bit, I'm more than happy to PayPal over some of the money, just so you know that you'll get paid etc.

    And good luck with the Windows Mobile project!
     
  17. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi CrazyFolker,

    I put some work in over the past week on an iOS build and spent a good part of today on it. It is going to take longer than I originally anticipated. There are about 100 linker errors. These appear to be caused by mismatches between the BulletSharpPInvoke C# wrappers and the corresponding native methods they are trying to call. These don't show up with the plugin because the native methods are dynamically loaded so they can't be checked until used. I need to fix these, but I would rather do it on the latest BulletSharpPInvoke code rather than the fork I am working with which is about two months out of date. Realistically it will probably be a couple of weeks before I have something.
     
  18. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
    Sounds great, I look forward to getting to try it out here!
     
  19. KinuxUnity

    KinuxUnity

    Joined:
    Dec 4, 2015
    Posts:
    8
    Hello ! Any news about an android or an IOS port ?
     
  20. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I have been working on an iOS port. It feels like I am close. I can get XCode to include the bullet physics source. All linker errors have been resolved. I can definitely interact with the bullet physics library. The scene loads and runs on the device but the objects arn't moving. I think the problem is due to the MotionState callbacks. Bullet likes to move the RigidBodies by calling methods on a MotionState object. I think I need to add [MonoPInvokeCallback] attributes to these callbacks. That's as far as I got last night.

    Unfortunately things are moving slowly as I only have 1-2 hours a day to work on this as I am full time trying to get EVA Infinity released. Will report back as soon as I have something running.
     
  21. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Good news! Things are moving now!

    Got it working well on OSX and rigid bodies are working on iOS. Still need to do some testing of the MotionState callbacks and it will take a some time to organize the different builds into a coherent source tree.
     
  22. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I checked the code into github. This checking includes a native plugin for iOS and OSX in addition to the Windows plugin that is already there.

    Disclaimer: This is really in an alpha state right now. O.K. to play with but don't bet your game on it without stress testing it first. The unity components are a start but definitely not complete yet.

    The github projects are;

    A Unity project containing bullet physics native plugins, wrappers and a mess of half finished components and scenes.
    Fork of the BulletSharpPInvoke project for building the native plugins for various platforms and C# wrappers.

     
  23. KinuxUnity

    KinuxUnity

    Joined:
    Dec 4, 2015
    Posts:
    8
    Thank you Phong.
    Do you plan to add an Android version ?

    Best regards
     
  24. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, not sure when. Hopefully it will be easier than the iOS build. It should be since I now have a codebase that builds for five different platforms using two different build tools.
     
  25. KinuxUnity

    KinuxUnity

    Joined:
    Dec 4, 2015
    Posts:
    8
    It seems the Unity PC editor plugin is broken in your last BulletSharpUnity3DProject
    I managed to build it for Android the hard way with ndk-build and jni directory. Nothing more than include all your files you included in the IOS build in the Android.mk
     
  26. KinuxUnity

    KinuxUnity

    Joined:
    Dec 4, 2015
    Posts:
    8
    I tested the mac and ios version, it is working fine ( I only launched the Test3 scene tought)
     
  27. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for the heads up. Glad to hear it works for someone else on iOS. It makes sense that I broke the windows build, I made some changes to the libbulletc library to fix problems on iOS with the MotionState callbacks. Forgot that I would need to rebuild the Windows plugin. Thats what I get for checking in fresh code at 11 PM. I will build the windows plugin later today and check in the new build.

    If the android plugin works could you send me the NDK project and a build? Or submit a git pull request? I don't actually own an android device so it is a pain for me to create and test a build. My friends get nervous when I ask to borrow their phones so I can try out my latest untested experimental code on their devices.
     
    RatherGood likes this.
  28. KinuxUnity

    KinuxUnity

    Joined:
    Dec 4, 2015
    Posts:
    8
    ok, I'll send it to you in an hour or two via unity forums messaging
     
  29. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for sending the Android build. I should be able to get my hands on an Android device later this week and test it.

    I updated the github Unity3d project with new windows native plugins so it should work in the editor and for standalone now.
     
  30. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Tested and added the android plugins. So far we have:

    Windows, OSX, iOS, Android
     
  31. RatherGood

    RatherGood

    Joined:
    Oct 12, 2014
    Posts:
    22
    Hi Phong, Downloaded your latest from GitHub. Getting DLL not found exceptions. Win 64 bit in editor. Do I need to do anything special to set this up now?
     
  32. Lee7

    Lee7

    Joined:
    Feb 11, 2014
    Posts:
    137
    Is Bullet deterministic? Can it do physics rollback? (for clientside predition in multiplayer games)

    If so, I would use it.
     
  33. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    This is a bug I introduced over the weekend. Will check in fix.

    You need to find "native.cs" and change "libbulletc.dll" to "libbulletc".

    [UPDATE] checked the fix into github
     
    Last edited: Feb 22, 2016
    RatherGood likes this.
  34. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I haven't tested this myself. You can look on the bullet forums.

    The wiki says it is http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Determinism.
    Erwin Coumans says only if running on the same processors and compiled using the same settings: http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=5989

    I do not believe that bullet physics has built in support for rollback. I think you would need to track the position, and velocities of your objects and do the rollback yourself.
     
  35. Kirbyrawr

    Kirbyrawr

    Joined:
    Jul 23, 2012
    Posts:
    945
    Hi there, i'm actually contributing to do the components more user friendly, if anyone have any feedback i would be happy to implement it : )

    Cheers!
     
  36. RatherGood

    RatherGood

    Joined:
    Oct 12, 2014
    Posts:
    22
    Hi Kirby, Yes! I left quite a mess in the soft body settings and procedural mesh generation. Got it working in the RGPlayground test scene but definitely isn't user friendly.

    My "help" probably set Phong back a month LOL. Glad to see more people join in the fun here.
     
    Kirbyrawr likes this.
  37. Kirbyrawr

    Kirbyrawr

    Joined:
    Jul 23, 2012
    Posts:
    945
    No haha, i'm not blaming your work in any way, the actual editors are nice but for example colliders shapes don't have editors so i said why not improve all of them, it's not like they are bad!! : D!

    Cheers!
     
    RatherGood likes this.
  38. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I appreciate your contributions RatherGood. I use the BulletUnity menu all the time to create test objects.

    If you are looking for suggestions, one would be a GUI for the BulletSharpExample scene. Bullet physics has an ExampleRunner application that presents a menu with all the examples that lets the user browse them. Currently the only way to switch the running demo is to edit the ExampleRunner.cs file and restart the scene. I suspect it takes a long time for new people to figure out how to switch the demos.
     
    Kirbyrawr likes this.
  39. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    RatherGood and elias_t like this.
  40. Kobix

    Kobix

    Joined:
    Jan 23, 2014
    Posts:
    146
    Very cool work Phong!