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

2D bullet physics?

Discussion in '2D' started by sebastiansgames, Oct 19, 2014.

  1. sebastiansgames

    sebastiansgames

    Joined:
    Mar 25, 2014
    Posts:
    114
    I'm wondering if there are suggestions for best practices when it comes to detecting 2D collisions between dynamic bodies?

    Like a moving ball hitting a moving racket. Or in my case, a fast moving pinball hitting a flipper.

    The Box2D manual refers to a "bullet setting" for these cases (http://www.box2d.org/manual.html search "bullet"). However I'm not seeing that implemented in Unity?

    Could I please make a feature request to have the bullet setting added to Unity? Or are there perhaps Unity3d 2D physics settings that might help in my case? I've tried tweaking a bunch of stuff in the 2d physics settings but am still getting tunneling. I have also tried all collision detection setting combinations (continuous, extrapolate, interpolate) on both paddle and ball with no impact on the problem.

    I suppose I might need to try rolling my own solution like this somehow? http://gamedev.stackexchange.com/qu...gh-speed-object-collisions-avoiding-tunneling

    I've also made a 2D version of the popular don't go through things script (http://wiki.unity3d.com/index.php?title=DontGoThroughThings). That seems to help in about half the cases.

    But the ball is still going right through the flipper in my tests too often for a workable game.

    Please help!

    Thank you!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,326
    Set the Rigidbody2D to continuous (CCD) mode if things are moving fast. If you're getting tunneling then I can only think that you're setting the angle of the flipper yourself i.e. you 'teleport' the collider from one orientation to another in which case it won't matter what mode you use. Box2Ds continuous collision detection is pretty solid.

    Perhaps you could use a HingeJoint2D (Box2Ds b2RevoluteJoint) on each flipper and use the motor to drive it.

    BTW: Interpolation mode just affect the rendering side of things i.e. the transform is positioned each render-frame between the last rigid-body position and the current rigid-body position whereas extrapolation works each render-frame but moves from the current rigid-body position to a position extrapolated based upon the current velocity and neither affect the rigid-body simulation.
     
  3. sebastiansgames

    sebastiansgames

    Joined:
    Mar 25, 2014
    Posts:
    114
    Hi Melv-- Thank you very much for your reply. Hope it's cool if I ask some follow up questions!

    1. I definitely have everything set to CCD (ball and flipper). In my case it's less of a flipper and more of a rotating circle with spokes that hit the ball (edited polygon collider2D). I did read your reply in a previous thread that cautioned against setting rotation manually. So I'm using rigidbody2D.MoveRotation per your recommendation in that thread. Unfortunately, when the spokes rotate quick enough (not ridiculously fast-- but still still in the range of fun gameplay speed) I still get tunneling where the spoke passes right through the ball.

    2. That's so great to know about interpolation! Thank you! I've read the documents over and over and never realized this. Document states: "Movement is smoothed based on the object’s positions in previous frames." Perhaps this is something that could be clarified in the docs so others don't have this confusion too.

    3. There are many settings in the Physics2D settings that are a mystery to me. Perhaps a tutorial on these settings could be created? Would increasing the velocity iterations and/or position iterations help with my tunneling? I've toyed with these numbers but I feel like I'm guessing blindly.

    4. Lastly-- I also have a handful of rigidbody2D's that I need to move together as a group. What is your advice for moving parented rigidbodies? Should I add a rigidbody to the parent empty game object and use rigidbody2D.MovePosition on that parent to keep the physics intact?

    Thanks very much for your help!!
     
    Last edited: Oct 20, 2014