Search Unity

Bug Interpolation some objects causes visual errors

Discussion in 'DOTS Physics' started by argibaltzi, Mar 2, 2021.

  1. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    Hi
    I have a problem with some objects since i started using smoothing INTERPOLATION on dynamic bodies.

    You can see this video for more details
    https://imgur.com/alh8AIF

    The red ones are meshes with dynamic bodies(made in editor) but the brown ones are dynamic bodies with meshes as children(the dynamic body has no meshes on itself, and it can have 1-4 meshes under it)(all made in code)

    Some of them will go crazy and have this ghost effect. It seems to have something to do with the transform matrices somewhere but i can't find a link or a pattern.

    All the debug data seems fine and i can't understand why some of the brown objects work and some don't.... they all made in same place in the same way

    Does ayone know what might be wrong?
    Before i enabled interpolation everything worked fine
    If i apply another explosion force they will behave the same way again

    Thanks
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @argibaltzi , could you please clarify that ghost effect a bit more? Do you mean those objects spinning fast in the background?
    It might be easier to put fewer objects on the scene so it's more noticeable. It also depends a lot on video quality.

    Did you check your monitor refresh rate and the project vsync setting (under proj settings -> quality)?
    You could also try drawing colliders in Physics Debug Settings or even printing NormalizedTimeAhead and smoothedTransform in Unity.Physics.GraphicsIntegration.SmoothRigidBodiesGraphicalMotion.SmoothMotionJob (requires making UP package local), to check if the problem lies in physics or rendering .
     
  3. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    yes those objects in the back, they seem to appear in 2 or 3 places at the same time in very fast movement.

    if you observe the red ones behave normal but on top left part in the back you can see some brown debris going nuts. However once they collide with the ground they snap back to normal behaviour

    this happens after i apply the ExplosionForce function

    IT ONLY happens when interpolation is activated, no interpolation and its smooth... :D

    @milos85miki i uploaded a higher res on dropbox (maybe quality is better if u download)
    https://www.dropbox.com/s/x7tgd70q3eq4l5y/bugTest.mp4?dl=0

    its a bit more clear in the back what happens
     
    Last edited: Mar 3, 2021
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    I suspect something's wrong with rotational movement interpolation. Debugging SmoothRigidBodiesGraphicalMotion.SmoothMotionJob should reveal that, I'd say printing out smoothedTransform would be a good start. Also, adding EntityTypeHandle to that job would help identify indexes of problematic entities.
    I'd suggest reducing the demo as much as possible, to get something that's easier to investigate.
     
  5. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    i will and see what debug i can achieve, are you saying that there might be a bug in there something or am i doing something wrong?
     
  6. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    If you repro the problem when interpolation is ON and there's no problem when it's OFF (without touching anything else), I'd say the problem is in interpolation code. Note that it might also be caused by big diffs between rendering and physics FPS, so it's not necessarily a bug. Imagine you need to interpolate between rotations that differ by 180 degrees on 1 axis, which side would you go (clockwise or counter clockwise)?
     
  7. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    I have noticed that high angular explosion velocity usually causes this error, when they dont have much rotation they seem ok (the other flying debris look fine)

    the entities that seem to suffer from this are built in code like that

    1) find render meshes that need to explode
    2) cluster them together and build an approximate box collider
    3) create a dynamic body with a box collider and add the render meshes as children

    I can't really see much wrong in my code to be honest, i have compared lots of things in the entity debugger and they all seem to be ok ( i will keep looking )

    I will try that debugging you suggested when i find some time and maybe some other tests with no angular velocity
     
    Last edited: Mar 5, 2021
  8. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    i did some small tests
    I set the Angular velocity to zero after i apply an explosion force and the bug is gone...
    It seems there is something going wrong with interpolation and high angular velocity

    can someone confirm this is indeed a bug in unity physics
     
  9. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    It would be great if you could provide exact rotation values that are causing the unexpected behavior. I don't see any obvious bugs, we're basically just calling math.nlerp for rotation.

    As I already mentioned, if diff between rotations is 180 degrees, it's quite hard to figure out which way to interpolate. This is just an example, to illustrate that there are hard edge cases for interpolation.
     
  10. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    i have seen angular velocities in the range of 300-500, should i apply some caps?

    edit: i did some debug logs and i saw some 800 values for angular velocity
    is that too high?

    its calculated from the ApplyExplosionForce

    or is the problem only for previous angular - angular = 180?
     
    Last edited: Mar 8, 2021
  11. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Yes, those values sound very high. The example I mentioned is when currentTransform.rotation - prevTransform.rotation = 180 degrees. Of course, those transforms are the results of angular velocity integration for the frame (diff = angVel * dt).
     
  12. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    Hi
    I was finally able to reproduce this
    You need 2 objects

    Root
    [PhysicsBody with Interpolation on, Mass:1 ]
    [PhysicsShape, Size: 0.5, 0.3, 0.2, Center: 3.95, 1.95, 5.5]

    Child Mesh (Unity Cube)
    LocalScale: 0.5, 0.3, 0.2
    LocalPosition: 3.95, 1.95, 5.5

    Note: this is not a compound body... its a simple dynamic box collider with a mesh under it
    From what i noticed, the smaller the sizes the more strong the ghosting effect it is

    This simple explosion will trigger the ghosting if you hit it in the "right place"
    Code (CSharp):
    1.  if (Input.GetMouseButtonDown(0))
    2.         {
    3.             Unity.Physics.RaycastHit raycast;
    4.             bool hasIntersection = InputSystem.RaycastWorld(out raycast, CollisionFilter.Default);
    5.  
    6.             if (hasIntersection)
    7.             {
    8.                 float delta = Time.DeltaTime;
    9.                 Entities.WithBurst().ForEach((ref PhysicsVelocity velocity, in PhysicsCollider collider, in PhysicsMass mass, in Translation position, in Rotation rotation, in Entity entity) =>
    10.                 {
    11.                     velocity.ApplyExplosionForce(
    12.                         mass, collider, position, rotation,
    13.                         500, raycast.Position, 10,
    14.                         delta, new float3(0, 1, 0), CollisionFilter.Default, 0.8f);
    15.  
    16.                 }).Run();
    17.             }
    18.         }
    If you disable interpolation then it works fine
     
    Last edited: Apr 5, 2021
  13. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Have you tried printing smoothedTransform values in SmoothRigidBodiesGraphicalMotion.SmoothMotionJob?
    This really sounds like a problem in interpolation with big angular velocities, I'll try to repro and take a look...
     
  14. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    this only happens when there is an offset on the child
    i tried with a 0,0,0 offset and it worked as expected
    It seems some combination of child offset and rotation interpolation is making it look like that?

    I haven't tried printing the smoothmotionjobs yet
     
    Last edited: Apr 5, 2021
  15. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    Would also like to point out that even with my Physics is 60 FPS and Rendering at 60 FPS, i still get this bug when interpolation is on
     
  16. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Thanks for the info, I'll try to repro locally and investigate.
     
  17. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @argibaltzi , it should be fine if you set the desired scale (0.5, 0.3, 0.2) on Root object and leave PhysicsShape size and child scale at (1,1,1). Is that ok for your use-case?
     
  18. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    Regarding my use-case that is not possible

    The root physics object has a box collider that is an approximation of all mesh boxes under it. There is usually 1-6 meshes under it and they are sometimes close to 0.0.0 and sometimes far from it

    Were you able to find out what is wrong? Is it a limitation or a bug? Its strange it only happens when interpolation is on
     
  19. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    SmoothRigidBodiesGraphicalMotion does a regular (and easy) job there, since the parent has no scale. However, seems like something doesn't fit together well later when child's LTW is calculated (in EndFrameTRSToLocalToWorldSystem, EndFrameTRSToLocalToParentSystem and/or EndFrameLocalToParentSystem), because child has scale and parent was interpolated.

    Will investigate further and let you know when I have some news.
     
  20. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    ok i see, so there is something wrong in the system it seems
     
  21. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Yes, the problem is definitely not in physics code, which you can confirm by enabling "Draw Collider Edges" in Physics Debug Display. You'll see that physics collider is bouncing as expected, but something is not calculating the rendering transform properly (most probably one of 3 systems I mentioned above). I'd recommend opening a bug for ECS Entities or rendering.
     
  22. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    164
    so i guess i won't see a fix anytime soon :D
     
  23. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    I really can't make any promises about timeline for fixes (especially outside of physics), but please don't assume it'll take too long. :) There are people monitoring incoming bugs, triaging and fixing them.
     
    argibaltzi likes this.
unityunity