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

Object bounces even before colliding with a boundary collider

Discussion in 'Physics' started by sudarshans, May 1, 2022.

  1. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    Hello,
    Im making a carrom kind of game where Im using physics for striker movement, the problem is my striker object bounces from the edge of the board even before there was a real collision between my striker object collider and the board edge colliders.
    Please check this video where I have highlighted my project setup, is I'm doing anything wrong here?
    https://drive.google.com/file/d/1xifD--sXHvQWjLbud-Lpt-qG3GAXu-hS/view?usp=sharing

    I tried to fix it in many ways but wasn't able to reduce this offset and without resolving this issue I wasn't able to proceed further, because if there are 2 objects very near to each other and if the striker hits only one of those objects accurately still the nearest another object is also considering it as a collision even though there wasn't a real collision!

    Is this a known bug? if not how can I remove this offset and can have accurate collisions that are required for carrom kind of games (where multiple objects are near to other)?
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    Just for narrowing down the issue, could you lower the physics timestep to see if it’s closer? I suspect that the distance travelled between the steps before and during the collision are actually pretty much the same, although on an angled path during the frame where the collision happens, which is the correct behavior in my opinion.

    You should only see the object really touching the wall if the fixed timestep exactly matches the point in time where they collide.
     
  3. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    Checked by changing the fixed timestamp to 0.0167ms (60 frames per second) but the same issue is happening and didn't see any improvement.
    if this was related to a fixed timestamp then at least 1 or 2 times it should have hit properly but in my case, this is happening with all the collisions so it is very unlikely that not even once the fixed timestep exactly matches the point in time where they collide.
    upload_2022-5-2_10-48-2.png
     
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    You probably already tried lowering the "Default Contact Offset" (https://docs.unity3d.com/Manual/class-PhysicsManager.html), but it might be worth mentioning anyways.

    I'm not sure if the Step button advances the time by a visual frame or a physics frame, but if it's actually steps through a fixed physics timestep, then lower the fixed timestep to maybe 0.002 (ten-fold change). The 20% increase in physics frames per second might not be easy to spot (0.02 to 0.0167). That's really just to see if that's the issue. It might not make sense to really use such a small fixed timestep.

    Another thing you can look at is the general scale of your game. If the objects are really tiny, say sub 10cm range, things tend to behave weirdly. You can try to increase the physical size of the whole game so that the striker is roughly 1-game-unit in size.
     
  5. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    Yeah, the Default contact offset I have already set to 0.0001 but that didn't help, and except for my striker object all of the other game object's scale is in 1 unit itself (x,y,z), striker scale was 0.75 on all axis but now I changed it to 1(x,y,z) but still the above gap is not reduced.

    Im unable to wrap my head around this issue!
     
  6. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    It looks like the issue is related to bounce property, just for testing I have removed bounce physics materials from the striker as well as from the board edge colliders so post that it looks like the gap has been reduced a lot.
    upload_2022-5-2_13-5-13.png
     
  7. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    Expecting some help here!
     
  8. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    Since you're expecting some help there, I took 45 minutes to create a project, setup a scene, use blender to make a puck, created a script that adds some impulse, created 4 pucks and a wall, set the pucks to the collision detection modes "Discrete", "Continuous", "Continuous Dynamic" and "Continuous Speculative", respectively (in that order from top to bottom) and stepped through the scene a couple of times. I made a screen recording (also a couple of times) uploaded it to youtube and wrote all this text.

    All this to ask you to stay a bit more humble, maybe even say thanks and not demand help like in your previous post.

    I hope this helps

     
    sudarshans likes this.
  9. DSivtsov

    DSivtsov

    Joined:
    Feb 20, 2019
    Posts:
    150
    Exist many articles describing your case "the different a bouncing problem at collisions" (i self was meeting with this). The size of problem will depend from your certain circumstance (speed, mass, angle and so on). You can partially decided the problem by give to the "Physic engine" more time to calculated physic tasks (by tune different project parameters), but you must know that the main idea of the "Physic engine" is made some fast estimation of the real physic processes with little consumption of CPU resources.
    ... !!! And it always will some estimation, if you not satisfied by this, you can give more resource to the "Physic engine" ... OR self calculate the physic process with demanded for you accuracy. Exist the many articles which describe how it can do in Unity (by example the bouncing from wall and etc.)
    P.S> "the problem is my striker object bounces from the edge of the board even before there was a real collision between my striker object collider and the board edge colliders." It's not a problem - the engine when you set not the "discreet collision detection mode" (all other types) will always do some estimation before real the collision, you can simple detect it by check the current object position in all FixedUpdate() calls.
     
    Last edited: May 2, 2022
    sudarshans likes this.
  10. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    Thank you so much man for your help but before creating this thread I have tried everything and run out of all the solutions, I even tried "Discrete", "Continuous", "Continuous Dynamic" but they were also giving the same result and as a last attempt yesterday night I installed latest unity version and did the similar setup to check physics activity over there but after setting up the environment Unity editor got crashed and scene setup was lost so that's why before going to bed I just posted this message expecting some hint from someone who might have already faced a similar issue.

    Anyway thanks for your time, I will check that again.
     
  11. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    I got the mistake that I have done!
    In the editor, every time while going to play mode striker object collision detection mode was set to "Continuous Speculative" and at run time I used to change it to "Discrete" or "Continuous" or "Continuous Dynamic" assuming I will be able to change it at run time but that's not the case, even though editor allows us to change collision detection mode at run time, technically it will only consider the mode which was set initially(i.e while starting the play mode), so If I have to see the behavior of "Continuous Dynamic" then I have to stop the play mode and set collision detection mode to "Continuous Dynamic" and run it again!
     
    tjmaul likes this.