Search Unity

2D How to prevent bouncing ball from going through collider boxes?

Discussion in 'Physics' started by Flybye, Jul 23, 2015.

  1. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    Hi all,

    So I have a simple game that feels like my bouncing ball sometimes escapes past collider boxes. The game consists of the following:

    1. 4 rectangular colliders - One at the top, one on the left, one on the right, and one at the bottom that acts as a level restart

    2. Simple polygon collider that acts as a paddle and is movable on the x axis over the restart box

    3. Bouncing ball

    So ball hits paddle, goes bouncing up, hits a wall or ceiling, goes back bouncing down, hit paddle, goes back up then back down, and ball hits paddle level restarts. Simple. I have a limit on the x velocity to try to keep the ball moving in y position mostly this way if the ball hits the wall it simply shots back down as opposed to shooting across to the left or right. All objects have a bounce material on them.

    It could take a while of maybe playing the game for 10-15 minutes or more, but sometimes the ball just goes shooting straight out the collider boxes. Only thing left to do is shut down and restart the app. I currently have it running in iOS if that matters any.

    What could be done to prevent the ball from escaping? Of course I have not left any "holes" between the collider boxes to let the ball escape.

    Slow down time of game, maybe slow down y velocity, maybe not have every object with a bounce material are just some of the things Im thinking. But I am sure there might be some easy unknown method that I dont know about to also fix this.

    Thx!
     
  2. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    Try setting the Collision Detection on the ball's rigidbody to Continuous (if it isn't already). That way it will collide with the other colliders even if it doesn't touch one during a physics update.
     
  3. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    I have it on discrete. I'll change it and let you know how it goes. TY!
     
  4. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    Still didn't work. :(

    Ive tried so many things such as:
    -Also putting paddle on Continuous
    -My walls are only colliders so they dont have the option.
    -Changing min penetration for penalty and Bauggarte numbers around. Although Im starting to think 2d Physics settings are more like a witches brew. If I dont have the right mixture then poof.
    -Tried changing Fixed Timestamp to .001. Yeah its pretty, low but its actually not quite bad on an iPhone 6 only pulling around 30% cpu when all other figures are left stock. Ive had as much cpu pullng as 50-60% after playing around with the 2d physics settings.
    -Ive also changed my maximum Allowed Time between .5 and 1.5 and still no go.
    -I have the bounce material on my paddle and ball. I removed it from the paddle.


    I only have 4 collider boxes which are not in contact with each other. I checked this because some say its a taboo if they do touch.

    Here is whats interesting:
    I cant duplicate it at any one specific point! Ive played for a few minutes and caused it. Ive played for 15+ minutes then caused it. Running a script for my paddle to clamp itself with the ball to auto play didnt even do any good because it wasn't able to reproduce it.

    What I am starting to think is that Im going to have to slow down the ball even more. I have it slowed down now with the following with x at 10 and y at 50:
    Code (csharp):
    1.  
    2.   public float xMaxV = 10f;
    3.    public float yMaxV = 10f;
    4.  
    5.    void Update () {
    6.  
    7.      var rb = GetComponent<Rigidbody2D> ();
    8.      Vector3 vel = rb.velocity;
    9.      rb.velocity = new Vector3 (Mathf.Clamp(vel.x, -(xMaxV), xMaxV), Mathf.Clamp(vel.y, -(yMaxV), yMaxV), Mathf.Clamp(vel.z, 0f, 10f));
    10.      }
    11. }
    Before I had that script, the ball would bounce to some astronomical speed and break the game immediately. After this script the game worked great, but I never did extended testing on it till now that I have people beta testing it. I really dont see any unity games with extremely fast moving physics objects, and I think I'm just going to have to slow the ball down unless someone has any other ideas. :(

    Do you think making my walls rigid bodies make any difference?
     
    Last edited: Jul 26, 2015
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Make your walls wider, I think your problem is, that the ball is too fast and bug's trough the wall.
     
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Move your script from update to FixedUpdate
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Don't modify these unless you know what you're doing. The min-penetration should be at the default otherwise you don't provide enough buffer region for continuous collision detection to work.

    You've got the fixed-update interval set to update 1000/sec? It's unlikely it can meet that demand. If you're trying to increase accuracy then leave it at default and increase the velocity/position iterations.

    I would not recommend manipulating the velocity directly either. Use the Max-Translation-Speed. This is how much Box2D can move a Rigidbody2D per-iteration (not per second). The default of 100 is a huge amount so that Rigidbody2D.MovePosition can work. If you were running at the default 50Hz fixed-update time, 100 means it any Rigidbody2D can move 50*100m = 5000m. If you wanted the maximum anything can move to be 100m then you'd set it to 100m/50Hz = 2.

    I'm not sure what this means. Presumably these are your walls and they are static. There's no problems overlapping them, they'll never collide with each other in that case.

    If I were you I'd reset the physics settings back to default.
     
  8. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    I reset the physics back to default! Actually, I did it before even reading your reply, Melv. :) I wasn't sure what they did, and playing with them solved nothing so I switched them back to default.

    I have my fixed interval very high because I have 50 or so hinge joints connecting small textures essentially making me a string/rope type of effect. My skills are still very limited, and this was the only way I was able to make a string from scratch. My ball is connected to this string. I had noticed the textures were becoming loose from their hinges, and the only way I could think of them staying together was by setting the fixed timestep to 0.001. My ball moves very fast, and I only changed the timestep to allow the string to stay intact.

    I also came back to mention FixedUpdate to not solve my problem. Nor did making my box colliders larger. Perhaps I didnt make them large enough, but I ended up giving them all rigidbodies and making their collision detection continuous. I have played the game for about 30 minutes so far with great results, so it looks good!

    Thx all for all the suggestions. :) If anyone has a better way of keeping my texture in sync with my joints and not having a timestep of 0.001, then I am all ears. :)
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    It would be worth you having a quick read through the Box2D manual as it has some useful snippets in there. Increasing the frequency of updates isn't how you improve accuracy of joints, you use the iteration properties to do that.

    Also, Box2D joints are soft and can break their constraints because of the solver used so stringing together lots of mass using joints can cause them to stretch. One thing that can help when making ropes/chains is to additionally use the DistanceJoint2D set to "Max Distance Only" which provides a very stiff joint that stops the overall length from going beyond a certain distance. This is even used in the Box2D test-bed. When you select that option, we use Box2Ds 'rope' joint.
     
    Last edited: Jul 31, 2015
  10. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    TY, Melv. At first playing with iterations was not giving me the results I wanted, but I cranked it up a bit more this time (to 500), reduced updates back to .01, and I am now seeing the results I want with less CPU use.