Search Unity

Unreliable Physics2D in Unity 5

Discussion in 'Physics' started by helios, Mar 13, 2015.

  1. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    So I've upgraded to 5.0 and everything seems to be working fine in my project except for one major thing - my edge colliders (or box colliders) aren't accurate anymore. My player regularly falls right through the ground or gets stuck on slopes. I've changed absolutely nothing relating to the physics, and it was rock solid in 4.x.

    My character is a BoxCollider2D, moved by forces and majority of my grounds are EdgeCollider2Ds. Is anyone else experience behavior like this?
     
  2. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    I've been having to make a lot of workaround since upgrading from 4.6.2 to 4.6.3p2 as well... A lot of interpenetration errors as well. I'm having to add invisible walls and setting some RigidBody2Ds to continuous because it's causing me a lot of issues as well. I'm assuming your and my issues are coming from the same source, since they are the same symptoms.

    Another thing that is really bothering me ( Though this was the same case for me since 2D Physics were introduced in Unity ) is that they seem to be non-deterministic. I can run the same inputs towards a sequence and I get slightly different results each time... why? This is terribly unreliable. I would love to know if there's any configuration parameter I'm missing that might at least give me deterministic results.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    It has been reported that stuff is falling through EdgeCollider2Ds even when using continuous collision mode (you are using this right?). The source of this is currently unknown; nothing major has changed from 4.6 to 5.0. Box2D itself is exactly the same. It is being looked at for sure.

    As to determinism; Unity has no effect on this, it's all Box2D. Box2D is deterministic on the same machine with the same inputs. The Box2D manual written by Erin Catto states the following:

    ---

    Is Box2D deterministic?
    For the same input, and same binary, Box2D will reproduce any simulation. Box2D does not use any random numbers nor base any computation on random events (such as timers, etc).

    However, people often want more stringent determinism. People often want to know if Box2D can produce identical results on different binaries and on different platforms. The answer is no. The reason for this answer has to do with how floating point math is implemented in many compilers and processors. I recommend reading this article if you are curious: http://www.yosefk.com/blog/consistency-how-to-defeat-the-purpose-of-ieee-floating-point.html

    But I really want determinism
    This naturally leads to the question of fixed-point math. Box2D does not support fixed-point math. In the past Box2D was ported to the NDS in fixed-point and apparently it worked okay. Fixed-point math is slower and more tedious to develop, so I have chosen not to use fixed-point for the development of Box2D.
     
  4. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Hi, thanks for the awesome reply! :)

    I had thought the Box2D implementation in Unity was altered since I moved actors and levels from my own engine with the same settings and got different results form the beginning. When I reload levels in Unity, that is, Destroy ( I even wait some frames before spawning the new one ) a root game object with all the actors, then instantiate it the prefab, I get different responses even without doing anything. For example, a stack my fall in one reload of a level, in another time I reload it, it stands upright. What could possibly be causing this break in determinism? I'm not looking for deterministic results across processors, but rather the issues I explained. This happens right within the editor even. When I had these levels in my engine Box2D would always without a hitch cause the same simulation given the same input. Which is why I am very confused as to what might be causing the issue in Unity... Any thoughts?
     
  5. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    PS : In my case ( as opposed to helios') the penetration issues began when I upgraded from 4.6.1f to 4.6.3p2. Not from 4.6.2 as I previously thought ( I just checked the older version I was working form ). So far they've happened with all kinds of colliders : circle, box and polygon. I use discrete collision detection normally, have worked around the issues by setting the culprits to use continuous. It happened in shapes which were working fine in 4.6.1f, went unchanged into 4.6.3p2 and started occurring.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    So the difference is probably that in 4.6.3p1, you'll see a fix of 'Physics: Fix behaviour issue in Box2D itself where non-dynamic/dynamic contacts always use continuous collision detection.'.

    Essentially, Box2D permanently used continuous collision detection for such cases i.e. collision against static geometry i.e. you paid the price whether you wanted it or not.
     
  7. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Ah got it, I searched through the changes and I had read that, but I guess I read it incorrectly.

    Any idea about the different output when instantiating the same prefab? Are the children of the prefab spawned in a non-deterministic order potentially causing the initial collisions to be "settled" differently? Would it be wise for me to take another approach at level loading?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    That should not be the case. Have you tried something simpler than this without dynamically instantiating a prefab i.e. a bunch of stacked objects?
     
  9. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Do you mean dynamically instantiating and stacking the objects individually as opposed as part of a prefab?

    I remember doing different simple cases months ago, but all from prefabs ( since I save my levels as prefabs ), but promptly moved on from it after I assumed something had been changed from box2d that was causing it. Otherwise, I have not tested with individual instancing if that's what you mean.

    But I'll give it a try today and report back.
     
  10. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Regards to the breaking through EdgeColliders2D - I am indeed using those, but I am not using continuous collision. Never had a problem before with discrete.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Box2D always uses continuous col-det if the collision involves a non-dynamic collider irrelevant of whether you ask for it or not. This was reported as a bug (in the sense that you get the performance hit whether you want it or not) and was fixed in 4.6.3p1 (also went into 5.0.0p1) so you may find that you start penetrating static colliders so just select continuous.

    To let you know though; I have a bug report for 5.0 that clearly shows stuff falling through edge colliders however the issue isn't edge-colliders themselves but a minor modification to Box2D for the effector system. I am now in the process of fixing that issue which stems from the fact that effectors can disable contacts. This will cause you problems even if you're not using effectors.

    I'll get the fix done and pushed into the next available 5.0 patch release at that time. I'll also post back here.
     
  12. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Hmm, that is interesting. I am also having the opposite issue where hitting the sides of an edge collider (that is, the actual end points of a straight horizontal line) sometimes registers a collision and prevents the other object from passing through. I would say it does this 30% of the time.

    This happens if I am just using a simple platform effector, or even if I manually do a Physics2D.IgnoreLayerCollision() ..the object will sometimes be stopped by the corners of the EdgeCollider when it clearly shouldn't.

    Could this possibly be related to the same thing?
     
  13. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Regarding the continuous collision detection fix that went in after Bennett's bug report: Shouldn't Unity just change the 2D physics defaults to collision detection in the UI, and then let people disable if they want? So update the visible defaults to match the actual behavior instead of the other way around...
     
  14. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    I did a few tests today, to no avail. I still get random behaviors happening after each reload.

    Here's an example of it :

    Every time it flashes I'm reloading it the level on the fly. All it's doing is deleting the previous objects, waiting a ( FixedUpdate ) frame, then re-instancing the prefab.

    I also tested instancing each object inside the level prefab individually but got the same results. I noticed that some of the scale vectors are being slightly altered in some of the objects for some reason when they are placed under the root level object, ( some objects randomly had 0.999 scale instead of 1 ), so I thought it had something to do with that and I instanced each object individually again and they remained at root level with no parent, I got the same non-deterministic behavior. This was done with a much simpler set-up, just 1 stack of the same object, all sharing the same physics properties. In each reload something different happened, but it was usually one of 2 exact outputs. Sometimes they either fell, sometimes they remained.

    Don't know what else it could be. I'm not altering them in any way, just instancing them the same way each time.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Unfortunately it's not that simple as Box2D uses time-of-impact CCD only when it thinks that a dynamic body will potentially touch a static body. For dynamic/dynamic it uses CCD if you select it. This means there's no 'backwards compatibility' that can be applied to keep the previous behaviour, short of turning CCD on for all dynamic bodies, which would be prohibitively expensive. This was one of those 'fixes' where we almost didn't do it but because it wasn't a data change and technically applied the correct and expected behaviour and was done in a patch, it was worth applying. If there was an overwhelming push to reverting it then that wouldn't be an issue so we went for it.
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    What I was trying to get you to test was to *not* instantiate stuff using fixed-update but rather just have this stuff already in a scene and press play to isolate the problem from the instantiation in fixed-update. Also, then you could try not using prefabs to see if it's related to that. Certainly a slightly different scale would affect stacking. As an aside, you can make a more stable simulation by increasing the position/velocity iterations. Also, you might want to consider starting those objects being stacked as asleep.
     
  17. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    I already tried not loading from the prefab and instead instantiating them by itself, like I mentioned in my previous post. I also tried not having them parented to avoid that random scaling issue I mentioned, same thing happened in every case.

    I just tried setting up a stack manually, and simply going into play mode and not, several times, as you mentioned, I am getting non-deterministic behavior as well. Every time something different happens. The don't fall, or fall, and each time they fall, they fall differently as well. There are no scripts attached or running what so ever. I use 20 for both position and velocity iterations, so it should be pretty stable, though the main issue is the lack of determinism.

    So in all cases getting the same problems, be it dynamically instanced or not.
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Would you mind creating a simple repro in a bug case and pass me the case number?
     
  19. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Sure thing I have an isolated project already prepared.
     
  20. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Here's the case number : Case 682428

    Thanks very much for your attention :)
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Not a problem, it's what I'm here for.

    I'll try to look at this later today but it's more likely I'll not get a chance until the weekend.
     
  22. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Just a heads-up on the original topic here: The fix for the issue where colliders would interpenetrate the EdgeCollider2D has been merged into 5.0.0 patch 3 so all should be well in the world when you get it.
     
  23. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Great, thanks! Do you think this will also resolve the issue of sometimes colliding with the edges of an edgeCollider with a one-way PlatformEffector? To clarify, say there is a single edgeCollider with 2 points that is perfectly horizontal with a PlatformEffector set to one-way. An object sometimes, but not always, collides and gets stuck against either corner of the platform. Many thanks.
     
  24. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Maybe but I cannot say for definite. If you can create a simple reproduction case and create a bug report and pass me the case number I can certainly take a look.
     
  25. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    I've made a small repro project and filed a bug report earlier today, but have yet to receive a confirmation email (not in spam folder). Is a delay common? Not sure if I should resend.
     
  26. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    So I took a look at this and sure enough I could reproduce the problem. I tracked this down to the fact that when you press play, the Box2D world itself is not destroyed then recreated (it is completely empty prior to playing though). This means that when you first load-up the project and run, you'll get one result but when you run again you'll get another. If you come out of Unity and do the same, you'll get the same results as last time i.e. the first plays will be the same as last time, so will the second, third etc.

    I verified this by taking screenshots and sure enough they are exactly the same.

    To solve this, I'll look at changing it so that the Box2D world is recreated when you press play thus resetting any state inside Box2D that is causing this.
     
  27. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Fixed. I'll be pushing this to the next available 4.6.x and 5.0.0 patch releases.
     
  28. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Hi MelvMay, I've submitted a bug report two times and have yet to receive a confirmation email/case number - Is there a bug with the Unity 5 bug reporter? I don't recall it ever taking this long in the past. Can I send you the package directly?
     
  29. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Thanks again for your attention!

    This is certainly strange... I've been using Box2D for a few years and didn't come across any issues with determinism before without recreating the world ( I would destroy actors and recreate them as I am doing in Unity now ). Does this mean that this fix will still result in un-deterministic behavior when reloading actors in the same manner dynamically.
     
  30. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    The fix, as it stands, will resolve the issue when initially playing a scene as I described however if you have any objects marked as 'DontDestroyOnLoad' then it cannot recreate the scene as it'll never be empty so it won't solve that situation when switching scenes but obviously still works when you initially play the scene but yes, it won't affect things created dynamically but I'll check that later.

    I've used Box2D for many years and I've never verified that dynamically creating a stack of boxes, letting them fall then destroying them and doing the same again results in identical results. I've only ever seen this from the world being creating. I'm not saying it won't, just that I've never verified it.

    It's certainly very strange but to exactly why this occurs, I'm not sure as I didn't dig that deep into Box2D. 3D physics recreates the scene in the same way, possibly for the same reason.

    I would certainly like to get away from recreating the scene so this isn't the end of it for me; I fully intend to find out why this is. I'll start by duplicating this in the Box2D test-bed and see if the same happens there. I just won't be able to do this work right now; probably over next weekend or early next week.
     
  31. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Hi MelvMay - Is there any way around the bug reporter? I've submitted it three times since Friday and the Bug Reporter tells me everything was sent successfully, but I get no confirmation email (nothing in spam folder) with any verification that it went through properly.

    Is there another way to check if Unity received it? The bug is very easy to reproduce and I can send the package directly to you if possible.
     
  32. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    There's no problem that I know of; receiving other bug reports fine. Can you provide a link for me to download.
     
  33. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Thanks again for your response and time! It's certainly appreciated. Please know that I'm not in a rush as I've done some workarounds around these issues so there's no sense of urgency on my part. But big thanks for your attention and help regarding the issue. :)
     
  34. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    You're most welcome. It all helps improving 2D physics for all. :)
     
  35. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    I've sent you a private message with a link to the test project. It appears to happen when the Y velocity of the colliding body is less than 0. There's also a quick movie file in the root Asset folder that demonstrates the issue.

    Many thanks in advance for checking it out!
     
  36. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Also, I've managed to successfully submit an official bug report with Unity 5, case # 683964

    Thanks again.
     
    Last edited: Mar 26, 2015
  37. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Hi Melv, unfortunately bad news. There seems to be a regression in patch 3. Bodies now snap/get pushed out again when going through one-way platforms (PlatformEffector2D). This was fixed in p1 (and I presume p2, though I never got that one), but it is happening again in p3.
     
  38. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Noooooo! It'll have to wait until Sunday or possibly Monday before I can check this out. This problem is not under any unit-tests as it's not easy to set-up as a test hence it's easy to regress. Might have to spend more time setting up a test for it if it has regressed. Sorry if that's so; I'll get a fix out ASAP.
     
  39. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Any update on this? Were you able to verify the bug returned? Thanks.
     
  40. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Yes, I have confirmed this and the specific change that caused the regression. I am dealing with this as a matter of urgency.

    Sorry for the regression, I'll have it fixed and in your hands ASAP in a patch release. I'll post back here as soon as it's in.
     
    Last edited: Apr 2, 2015
  41. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Any update on this? I didn't see anything in the change log for the latest patch release.
     
  42. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    It's not in the patch yet. It's part of a bunch of changes that are in the patch release pipeline. As I say, I'll post back here when it's in. Won't be too long.
     
  43. dustinandrew

    dustinandrew

    Joined:
    Apr 30, 2010
    Posts:
    102
    I'm also seeing the problem with 2D Colliders breaking through EdgeColliders. Unity 5.0.1.f1

    My project was simple enough that I changed out the EdgeColliders for simple PolygonCollider2Ds and it seems to work. Also for any SpringJoint2Ds make sure that the EnableCollision is on if needed for colliding with connected Rigidbodies.
     
  44. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Try setting your Rigidbody2D to use continuous collision detection.
     
  45. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    I can verify that the snapping/colliding midway through One Way platforms is fixed in the latest patch. Thanks!

    Now the only major PlatformEffector bug left is case 683964
    Do you know if this is actively being worked on, or put on the back burner? It's a pretty bad bug that really makes the OneWay option of PlatformEffectors pretty useless.
     
  46. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    I might not be working on it this second but it's not on any back-burner either. It'll get actioned ASAP.
     
  47. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Yea, I didn't mean this very second :)

    Thanks for the updates and prior fixes.
     
  48. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Sorry to be a bother, but any news on the PlatformEffector bug?

    Thanks.
     
  49. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Hate to always be the bearer of bad news, but I noticed yet another terrible regression in the latest patch release (5.0.2p4). Confirming this also happens in 5.0.2f1. This only happens with Rigidbody2D, not Rigidbody.

    Very bad and strange bug where the Rigidbody2D MovePosition function NEVER ceases to execute, even when disabling the script. This is only apparent with MovePosition. MoveRotation behaves normally. This is also a regression, my last known version where it was working correctly is 5.0.1p2.

    I've filed a bug report, case #: 702390

    You can easily test this though in a new project by creating any gameObject with a kinematic Rigidbody2D and attaching the following script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestBug : MonoBehaviour {
    5.  
    6.     public bool allowMovement = true;
    7.     public Vector2 speed = new Vector2( 0.5f, 0 );
    8.     public float rotationSpeed = 20.0f;
    9.  
    10.     private Rigidbody2D _rigidbody;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         _rigidbody = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     void FixedUpdate ()
    18.     {
    19.         if( allowMovement )
    20.         {
    21.             _rigidbody.MovePosition( _rigidbody.position + speed * Time.deltaTime );
    22.             _rigidbody.MoveRotation( _rigidbody.rotation + rotationSpeed * Time.deltaTime );
    23.         }
    24.     }
    25. }
    If you run the scene, you'll see the object moving and rotating as expected. If you uncheck the "allowMovement" flag, then you'd expect the movement and rotation to stop, but it doesn't - only the rotation does. The .MovePosition function somehow never stops being called. This is even apparent if you disable the script entirely. What gives?

    Thanks.
     
    Last edited: Jun 7, 2015
  50. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    This was a regression introduced in 5.0 patch. Essentially, the regression is that after a move-position, the linear velocity of the Rigidbody2D should be restored to what it was prior to the move-position being called. The bug is that now the velocity isn't being restored and whatever linear velocity being used to move the Rigidbody2D stays the same this it can look like the MovePosition never ceases. It does, just that the linear velocity needs resetting.

    A potential workaround in the meantime would be to reset the velocity yourself which may be easy if that's always zero.

    Anyway, itt has been fixed and is being back-ported to all patch releases.

    Sorry for the problem.