Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[RELEASED] Bullet Physics For Unity

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

  1. gonzalokriger0

    gonzalokriger0

    Joined:
    Feb 2, 2019
    Posts:
    2
    The problem seems to be just the collision margin. I also tried it with unity 2018 and I'm using the unity store version.
     

    Attached Files:

  2. Adrien1Button

    Adrien1Button

    Joined:
    Oct 4, 2021
    Posts:
    2
    Hello Phong,
    I'm currently looking into Ragdolls, and I'd like to apply it to a Rigged character model, for this purpose I'd like to have CollisionShapes and Rigidbodies on all the relevant bones. This however would break the "no-rigidbody nesting" rule, I've looked at the Ragdoll example, but it doesn't really fit my situation as I'm having to handle a Single Skinned mesh with it's hierarchy of bones and not a flat collection of constrained meshes.

    Is there any built in solution or do I need to generate a hierarchically flat Ragdoll at runtime and sync my bones to the Ragdoll each frame ? If you have any advice or insights I'm happy to hear them and thanks for this free plugin.
     
  3. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,065
    Hi Adrien1Button,

    Sorry to say that there is no built-in solution for that. You will need to build a flat hierarchy with constraints and sync your bones to the Ragdoll each frame.
     
  4. Adrien1Button

    Adrien1Button

    Joined:
    Oct 4, 2021
    Posts:
    2
    I expected as much and am already working on it, it shouldn't be too difficult with our fairly simple models, it's unfortunate though.

    And thanks for you quick answer.
     
  5. qdeanc

    qdeanc

    Joined:
    Nov 15, 2017
    Posts:
    15
    Hey Phong,

    Bullet
    gives way more contact point data than Unity's PhysX implementation, so I'm switching to Bullet so I can play impact sounds at the contact points of collisions in my game :)

    However, when hundreds of rigidbodies are colliding, iterating through each contact point uses a lot of processing. Unity's "Collision" class has the "impulse" property, described as:
    "The total impulse applied to this contact pair to resolve the collision."

    I initially used that "impulse" sum to determine the 32 most impactful collisions to play sounds for. This helped me ignore the hundreds of weaker collisions (and their contact point impulses) which I don't have enough speakers for.

    I figure Bullet has to combine all contact point impulses at some point when performing physics calculations. Is there some way I can get that data in Unity?
     
    Last edited: Dec 16, 2021
  6. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,065
    Hi,

    In bullet I have implemented some functions that act like Unity's OnCollisionEnter, OnCollisionStay, OnCollisionExit. However these are a lot of overhead if there are a lot of bodies.

    The most efficient thing to do would be to iterate through the collision manifolds. Look at method BPhysicsWorld.OnPhysicsStep. This is where we iterate through the collision manifolds and fire the event handlers. You could instead iterate through the manifolds and collect the 32 most impactful collisions.
     
  7. qdeanc

    qdeanc

    Joined:
    Nov 15, 2017
    Posts:
    15
    Thanks for the reply!
    I ended up getting the Dispatcher from BPhysicsWorld's CollisionWorld. The Dispatcher let me efficiently iterate through each PersistentManifold like you said.

    Here's a physics test I made using green debug rays to visualized the location and impulse magnitude of each contact point:



    It runs much faster, but I found a new issue when it comes to playing audio:

    heavy_mass_contact_point_impulse.png

    When a rigidbody with a large mass rests on another rigidbody, there is a large continuous impulse applied at the contact points of the collision pair. If I play impact sounds purely based on the impulse strength, this will result in a loud sound playing every update (until the rigidbodies fall asleep, of course).

    Having data that tells me the total linear/angular impulse on each rigidbody would help me determine that these rigidbodies are more or less in equilibrium, and should not play sound despite the large impulses at their contact points. So I think I still need that "Total Impulse" data.
     
    Last edited: Dec 21, 2021
  8. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,065
    Can you subtract a bodies (mass * gravity * dt) from each bodies impulse? Would that give yout impulses that are not simple body-at-rest impulses?
     
  9. qdeanc

    qdeanc

    Joined:
    Nov 15, 2017
    Posts:
    15
    Oh, that's actually why I used the term "equilibrium" instead of "at rest".
    Here's a physics scenario where removing gravitational impulses would still leave some large impulses behind:


    Looking into this more, I think what I'm looking for is the delta impulse of each collision. It makes sense to me that sudden increase in force is what causes shockwaves/vibrations/sound.

    If two objects suddenly collide, the delta impulse will be significant (impulse jumping from 0 to some larger number).
    So sound is produced.

    If an object is pushing against another with constant force, even though the objects might be experiencing large impulses, the delta impulse will be insignificant.
    So no sound is produced.
     
    Last edited: Dec 23, 2021
  10. wechat_os_Qy07am7Wy1_qI2hEgygU9lgVI

    wechat_os_Qy07am7Wy1_qI2hEgygU9lgVI

    Joined:
    Aug 10, 2019
    Posts:
    48
    Hey Phong, Can this bullet physics use for Lockstep game?
     
    DJ_Design likes this.
  11. wechat_os_Qy07am7Wy1_qI2hEgygU9lgVI

    wechat_os_Qy07am7Wy1_qI2hEgygU9lgVI

    Joined:
    Aug 10, 2019
    Posts:
    48
    maybe someplan to use fixed points?
     
  12. qdeanc

    qdeanc

    Joined:
    Nov 15, 2017
    Posts:
    15
    Hey Phong, I've been spending a lot of time trying to answer my own question, and I've realized it has very little to do with BulletSharp or BulletUnity. It's more of a general engine question, so I should have asked on the pybullet.org forums! Sorry!

    That being said, I'll summarize what I've found so far:

    For each collision island, Bullet takes all individual contact points, and applies their impulses by changing each rigidbody's linear/angular velocity within the island. It does this on a per-island basis; not a per-manifold basis. This is why manifolds do not contain any kind of "total impulse" or "delta velocity" data for their rigidbodies.

    The application of contact point impulses can be found in the btSequentialImpulseConstraintSolver.cpp class, specifically in the gResolveSingleConstraintRowGeneric methods.

    When
    btDiscreteDynamicsWorld.cpp calls its processIsland method, that method calls btSequentialImpulseConstraintSolver.cpp's solveGroup method. In summary, this turns each contact point within the island into a constraint (using things like friction and restitution to create the constraint properties). Those constraints are then passed into the gResolveSingleConstraintRowGeneric methods (and some other methods within the class), which set the m_deltaLinearVelocity, m_deltaAngularVelocity, m_pushVelocity, and m_turnVelocity variables of the solverBody.h associated with each rigidbody. Then it calls solverBody.h's writebackVelocityAndTransform method, which uses m_deltaLinearVelocity and m_deltaAngularVelocity to set the rigidbody's velocities and m_pushVelocity and m_turnVelocity to set the rigidbody's transform; solving the rigidbody's collision within the island.

    At some point, I'll be looking into how I can pass a manifold ID through this process so I can store the rigidbody delta velocities per-manifold with minimal extra processing. I think this is extremely important for physics games that want accurate sound. They need to know the delta impulse of a collision! But for right now I'm going to work on a game that doesn't require so much work (ᅲ﹏ᅲ )
     
    Last edited: Jan 8, 2022
  13. pshtif

    pshtif

    Joined:
    Mar 6, 2017
    Posts:
    12
    Guys is there a way to get positions of all rigid bodies in a row in memory? Does Bullet store them in such a way or is there some option to get them like that. Something like copying all positions of rigidbodies into a NativeArray in Unity so I can that use Jobs to modify transforms in Unity.
     
  14. DJ_Design

    DJ_Design

    Joined:
    Mar 14, 2013
    Posts:
    122
    Also very curious about this. a multiplayer rts for example is a very lacking concept in unity due to the nature of physx.
     
  15. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,065
    Bullet is approximately similar to PhysX in this regard. I believe that the new UnityPhysics is designed to be fully deterministic across different hardware architecture. However it requires a project to use DOTS / ECS with is a big commitment.
     
  16. YiboInsane

    YiboInsane

    Joined:
    Jul 22, 2019
    Posts:
    16
    Just saw this post. I wonder how the performance is compared to Unity's physics and Unity's ECS physics .
     
  17. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    Last time I have tested:

    Bullet physics is single threaded with UNITY. Wasn't suitable for anything sensible in terms of game implementation for me at that time and performance wise.

    Unity DOTS physics, there are two, native and havok. Both are multithreaded and support burst. Native solution is mostly deterministic. Cross platforms determinism is not guaranteed however. Havok focuses on stacking and performance, but doesn't support determinism.
    Specialy suitable, when doing anything with DOTS.

    Unity PhysX is supported by NVidia. So you have GPU support for physics. Generally focused on game objects imtegration. And having many out of box supporting solutions from Unity.

    I don't know about bullet physics, but physx and DOTS solutions support cross-platforms.
     
  18. Swiftle

    Swiftle

    Joined:
    Feb 11, 2014
    Posts:
    37
    Hi there, I'm using bullet only for collision detection between boxes and I've ran into an issue which I'm not sure how to solve. I have a very simple scene where I spawn cubes in a line with a small gap between them. There isn't a BPhysicsWorlds in the scene. Instead, in my script I create a CollisionWorld and CollisionObjects (with a BoxShape) for each cube (I also used the debug draw utility to make sure that those collision objects and their shapes exactly match the cubes). I check for collisions by calling PerformDiscreteCollisionDetection when and then I'm using CollisionWorld.Dispatcher.NumManifolds and GetManifoldByIndex.
    My problem is the following. When the gap between the cubes is very small ( < 0.3), Bullet tells me that all the objects are colliding with eachother, even though they aren't (NumManifolds > 0). If the distance is > 0.3 then Bullet correctly tells me that there are no collisions (NumManifolds = 0). Even so, when going through each manifold, the NumContacts is 0 so the objects aren't really coliding.
    It seems to me that the narrow phase isn't executing or it's not updating the manifold correctly and I'm kinda lost as to where the issue might be coming from. The dispatcher is using the defaultNearCallback.
    Is there a step that I'm missing?
     
    Last edited: Apr 20, 2023
  19. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,065
    I think this is the expected behavior.

    NumManifolds is not an accurate measure. Bullet adds a small margin to each collider. If the colliderWithMarginBoundingBoxes of any pair of colliders intersects then a manifold gets created and persists for as many time steps as these bounding boxes intersect. This happens in the broadphase.

    The broadphase is a quick-and-dirty approximate test. It only checks the bounding boxes of the colliders for intersection (adding a small margin to each). The broadphase is a quick, cheap test designed to quickly eliminate most of the possible collisions between all pairs of colliders. It generates a list of collision manifolds that may persist across several timesteps. Each manifold is a potential collision. The narrowphase (expensive) then runs on each potential collision pair.

    To get a shortlist of true collisions, you need to traverse the list of manifolds and collect the manifolds that have non-zero NumContacts.

    https://andysomogyi.github.io/mechanica/bullet.html
     
  20. manurocker95

    manurocker95

    Joined:
    Jun 14, 2016
    Posts:
    193
    Does anyone know if it's still possible to cut softbody volumes with bullet?
     
  21. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,065
    TBH I am not sure. I would ask on the puBullet forums: https://pybullet.org/Bullet/phpBB3/
     
  22. manurocker95

    manurocker95

    Joined:
    Jun 14, 2016
    Posts:
    193
    I had already looked there. They removed the feature like 10 years ago for no reason. But I was asking in case someone tried to achieve that with Bullet in Unity.