Search Unity

2D physics bounciness issue

Discussion in '2D' started by underoathgames, Nov 18, 2013.

  1. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    I'm setting up a breakout style game in 2D using Unity 4.3 and I've set the ball and the vertical walls to have a material (physicsMaterial2D) with zero friction and full (1) bounciness. But when the ball hits the vertical wall with a relatively shallow angle the ball just sticks to the wall and not bounce off of it. At larger angles the ball will bounce off the wall but not for shallow angles. Also if I speed up the ball velocity it seems to work ok too but not when it's slower. Any ideas why this is happening?
     
  2. Sinron

    Sinron

    Joined:
    Oct 12, 2012
    Posts:
    16
    Something tells me it may have to do with the mass, drag and angular drag of the ball.
    Check those values and modify them and see what results you get.
     
  3. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    +1 --- tweak those values until it stops doing it. If you can't figure it out then upload some screenshots of your object properties in the inspector and I will try to recreate the issue on my end.
     
  4. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    Thanks for the input. Here's the deal. The Rigidbody 2D component has zero's for both the linear drag and angular drag. The mass is set to 1 but experimentation with this number does not seem to have a big impact negatively or positively. I set the velocity of the Rigidbody 2D object in script with values of 2f and 2f for the x and y axis respectively. When I change the x value to 1f and keep the y value to 2f the issue is observed.

    I've created screen shots of the inspector for my ball object and the material. Also some quick videos showing the issue.
    $Inspector.png $Inspector_matrl.png

    http://www.underoathgames.com/issues

    If there is a problem with the links I created you should be able to watch the videos at http://www.underoathgames.com/issues

    I use the google chrome browser and was able to watch the vids. (They are HTML5 objects)
     
  5. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    I have a similar problem with my game (not a Breakout style game, but a physics-playground sorta game, where you knock a ball around). I'm adding forces to the ball, rather than setting the velocity directly, and it still happens.

    I suspect there's a stickiness threshold on the walls that isn't exposed in Unity.

    EDIT: Hah, searching for Box2D sticking to walls turned up pretty much exactly that, right away. http://stackoverflow.com/questions/5381399/how-can-i-prevent-a-ball-from-sticking-to-walls-in-box2d
     
    zezba9000 likes this.
  6. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    Interesting read @Togglesworlh. One thing I forgot to mention is that I'm using an Edge Collider 2D component for the walls. Could this be the problem? I'll try using a Box Collider 2D object instead. This is getting very frustrating :(

    Update: I tried using a Box Collider 2D component and got the same results. Also I switched the components to use the 3D versions for the colliders and material, and also got the same issue, no bounce. So if the 3D is behaving just like the 2D am I doing something horribly wrong or does this latest release of Unity 4.3 have some major glitches.
     
    Last edited: Nov 18, 2013
  7. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    The 3D PhysicsManager variables allow modification of the Bounce Threshold. I bet if you change that to 0 (Edit->Project Settings->Physics), it'll always bounce with 3D physics. It's just that they forgot to expose the equivalent variable for the 2D physics, I guess.
     
  8. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    Well I changed the Bounce Threshold to zero and used 3D physics but I still got the "sticky" wall syndrome. Not sure what's going on here.
     
  9. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Huh. Well, I'm out of ideas!
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Box2D has a bunch of compile-time constants and therefore are immutable at runtime. We have considered exposing some of these constants at runtime but it obviously involves modifying Box2D itself and therefore is prone to creating issues with dynamically allowing these to change at runtime.

    We believe it's important to investigate which of these we can safely expose however and so you can expect some of them to be exposed in the future.
     
  11. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    MelvMay, I did use the 3D physics colliders, materials and rigidbodys in Unity 4.3 and still got the sticky wall issue. I have been able to achieve what I'm attempting in previous versions of Unity using the 3D physics engine. So I'm a little confused as to why now I'm getting this problem.
     
  12. Mogitu

    Mogitu

    Joined:
    Nov 13, 2013
    Posts:
    40
    funny, i am working on a breakout style game aswell and ran earlier into this issue today. fiddling with the friction fixed it for me.
     
  13. Berenger

    Berenger

    Joined:
    Jul 11, 2012
    Posts:
    78
    I had a similar issue in a 3D game, but found a workaround. I didn't know about the bounceThreshold parameter, and it would have fixed it. But if the problems remains for now in 2D, my workaround might still be usefull.

    So, underoathgames, you can add a script on your wall that store into a list each object that enter in collision with it. When the object leaves, remove them from the list. If the list not empty for X seconds, add a for to each of the rigidbodies in that list, orthogonal to the wall. Just kick'em right in the face, you might say.

    I'd like to have a better solution though.
     
  14. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    @Mogitu, I'm glad fiddling with friction worked for you. I might keep messing with that parameter but I have a felling it won't work.

    @Berenger, I thought about doing something like that which I might have to implement if I can't get it working any other way. For my style of game it is critical for the ball to bounce off the wall :)

    Thanks for the suggestions.
     
  15. Berenger

    Berenger

    Joined:
    Jul 11, 2012
    Posts:
    78
    I handled the bouncing myself, using physics only for collision detection, and the result seems to be quite satisfactory. There might be some glitches with several balls though.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Ball : MonoBehaviour
    4. {
    5.     // Constant speed of the ball
    6.     private float speed = 5f;
    7.  
    8.     // Keep track of the direction in which the ball is moving
    9.     private Vector2 velocity;
    10.  
    11.     // used for velocity calculation
    12.     private Vector2 lastPos;
    13.  
    14.     void Start ()
    15.     {
    16.         // Random direction
    17.         rigidbody2D.velocity = Random.insideUnitCircle.normalized * speed;
    18.     }
    19.    
    20.     void FixedUpdate ()
    21.     {
    22.         // Get pos 2d of the ball.
    23.         Vector3 pos3D = transform.position;
    24.         Vector2 pos2D = new Vector2(pos3D.x, pos3D.y);
    25.  
    26.         // Velocity calculation. Will be used for the bounce
    27.         velocity = pos2D - lastPos;
    28.         lastPos = pos2D;
    29.     }
    30.  
    31.     private void OnCollisionEnter2D(Collision2D col)
    32.     {
    33.         // Normal
    34.         Vector3 N = col.contacts[0].normal;
    35.  
    36.         //Direction
    37.         Vector3 V = velocity.normalized;
    38.  
    39.         // Reflection
    40.         Vector3 R = Vector3.Reflect(V, N).normalized;
    41.  
    42.         // Assign normalized reflection with the constant speed
    43.         rigidbody2D.velocity = new Vector2(R.x, R.y) * speed;
    44.     }
    45. }
     
  16. underoathgames

    underoathgames

    Joined:
    Sep 17, 2013
    Posts:
    15
    Thanks so much Berenger, the code looks great. I didn't realize there was a Reflect function as part of the Vector3 class. I need to do a little reading up on that class. This just might work for me, I'll play around with it, thanks again.
     
  17. arnoscene

    arnoscene

    Joined:
    Jan 21, 2013
    Posts:
    1
    @Berenger

    Amazing this works perfectly I was having the same issues with the bounce material, thanks for your help
     
  18. K1kk0z90_Unity

    K1kk0z90_Unity

    Joined:
    Jun 2, 2013
    Posts:
    90
    Hi all,
    I have the very same problem. I'm happy I found this thread!
    Could I ask to Unity development team if the next release of Unity will allow us to set the Box2D minimum velocity threshold for elastic collisions property?
    Thank you in advance for your answer, and happy new year! :)
     
    Last edited: Jan 1, 2014
  19. Caliber-Mengsk

    Caliber-Mengsk

    Joined:
    Mar 24, 2010
    Posts:
    689
    Don't know if this will help, but under the physics, are two values called sleep velocity and angular sleep velocity. Since you only really have the ball to worry about, try setting these to 0 and see if it works. I'm guessing they are simply hitting the wall, getting a split second of the velocity being to low making it enter into sleep.

    If you have a lot of physics objects and only want the one object to keep moving no matter what, use this code
    Code (csharp):
    1.  
    2. if (rigidbody.IsSleeping() ) {
    3.     rigidbody.WakeUp();
    4. }
    5.  
    in the update loop. This will force it to ALWAYS check physics and apply forces and such. Putting this on specific items is probably the best way to do it, as the first mentioned way will make all physics objects always apply forces and such. Doesn't sound like a big thing, but if you have 1000 physics objects in a scene and only 3 are actually moving, thats 997 extra calculations for no reason, so the coded method is best. :D

    EDIT:
    I should also mention, this is what I had to do for some things in 3d. I don't know for certain it will work in 2d objects.
     
  20. K1kk0z90_Unity

    K1kk0z90_Unity

    Joined:
    Jun 2, 2013
    Posts:
    90
    @Caliber Mengsk: Thank you for your answer, but unluckly it seems that the cause of the problem is actually the minimum velocity threshold for elastic collision property of Box2D. Under that speed threshold, the bounce does not occurs, so we need that Unity exposes that value so that we can change it to zero, instead of the default value.

    @Unity dev team: Will the next release of Unity allow us to change that value? Thank you very much in advance for your answer!
     
  21. K1kk0z90_Unity

    K1kk0z90_Unity

    Joined:
    Jun 2, 2013
    Posts:
    90
    I've just submitted a suggestion to allow to change the value of the b2_velocityThreshold constant of Box2D. Please, vote for it if you have the problem described in the first post of this thread. :)
     
  22. mheinsohn

    mheinsohn

    Joined:
    Feb 5, 2014
    Posts:
    2
    Thanks for posting the script Berenger, this is the exact behavior I was looking for. You should get a medal.
     
  23. Riskki

    Riskki

    Joined:
    Aug 9, 2014
    Posts:
    1
    For me Berengers script didn't seem to work.. i may have got something wrong! however i found a solution which made it way easier to do this and it uses the physics system built into unity so makes it way easier with less scripting!

    what you have to do is lower the bounce threshold to 0.1 (apparently it will be all jittery if you change it to 0) it has a default of 2 and this is basically the minimum velocity threshold that something is travelling at and can still bounce!

    to change it go to Edit >> Project Settings >> Physics and you'll see 'Bounce Threshold'

    hope this helps :)
     
    Birocchi likes this.
  24. BadW0lf

    BadW0lf

    Joined:
    Aug 19, 2014
    Posts:
    1
    Berenger, post: with bounce code...

    Thank you for the code! This worked great and easily inserted into my existing script!
     
  25. pranav_cherayi

    pranav_cherayi

    Joined:
    Mar 28, 2014
    Posts:
    1
  26. rajat_neo

    rajat_neo

    Joined:
    Nov 30, 2017
    Posts:
    2
    Hi all,
    I have the very same problem, velocity Threshold (0.01) worked for me
    Edit-->ProjectSetting-->physics 2D
     

    Attached Files:

    path14 likes this.
  27. Pedrogial

    Pedrogial

    Joined:
    Dec 4, 2020
    Posts:
    1
    You are amazing