Search Unity

2D physics and balls within rectangular borders

Discussion in 'Physics' started by jtiai, Feb 10, 2015.

  1. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    I'm trying to create game where player pushes with vessel (ball) another balls within area that is limited by rectangular borders.

    I've encountered two problems:

    First problem is that when player pushes ball in shallow angle against border wall, ball doesn't bounce but glides along the edge.

    Second problem is that when ever ball ends up in the corner it's impossible to get it out of there.
     
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    What kind of physics material are you using?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Make sure that the 2D physics objects that are colliding are actually moving with their own velocity. If all you're doing is repositioning them via the Transform component then you're simply forcing overlaps and the physics system will do its best to resolve the overlaps. In other words, it's just causing problems that the physics system is trying to solve but nothing is actually 'moving' thus there's no bouncing etc.
     
  4. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    I'm not touching location (or forces) of the balls that player is supposed to push after initial layout. Only object that gets force applied is player. Balls do bounce properly if angle is big enough but smaller angles between dirction of movement and wall balls do tend to slide along the edge for some reason.

    I've put up prototype here: pusher prototype

    Player ship is controlled with: A = counterclockwise, D = clockwise, W = forward. Only blue ball currently does have a proper physics material and rigidbody.

    If that blue ball is pushed in small angle against wall it will glide. Also it's very easy to make that ball stuck in a corner without way to get out it there.

    Originally my idea was to have real clip + mirror on each edges but found out that's relatively hard to achieve, specially with similar physics requirements. I mean that if ship is half way over left hand edge, half of the ship is already visible on right hand side.
     
  5. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    Wall, Friction 0, Bounciness 0.8
    Player: Friction 0, Bounciness 0.8
    Blue ball: Firction 0, Bounciness 0.2 (tried with 0.4 and 0.6, same effect)
     
  6. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Hm I have absoluteley no experience with that kind of thing but what I would try next ist setting the friction of the wall a bit higher. My guess would be, that if the angle is too small, the physics calculates that the ball has to slide the wall, that means, all of the force is directed along the wall and nothing is there to bounce. If you think in real world terms, if you have a wall out of perfect ice then it would be the same behaviour.
     
  7. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    I've tried pretty much everything. I updated my prototype to have different bounciness and frictions to ball and walls. Apparently small angle will eventually end up with gliding along the edge - I really want to avoid that, after all this is supposed to be more arcade game than physics simulation.

    So I'm open for all suggestions - if everything else fails, I can always manually do the physics math.
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,443
    have you tried using Edge colliders for the borders?

    in one snooker2D test I used these modified physics2D settings:
    Velocity iterations:12
    Position iterations: 5
    Velocity threshold: 1

    But cant remember was it the edge colliders that helped the most..(for avoiding wall sliding)
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    If you're producing large tangental forces (along the wall) as oppose to small forces 'into' the wall then it's bound to 'slide' as the energy has to go somewhere. I'm not sure if it'll help but you can set friction to values >1 to produce non-realistic results.

    I'm still not clear on exactly what you want but have you considered adding a force to repel the ball from the walls when in contact? This'll force them out of corners or against edges.
     
    Last edited: Feb 13, 2015
  10. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    Large tangential forces sounds something that I'm most probably experiencing. Have to play around with frictions a bit more, and if that fails I probably have to add manually force on contact, which isn't bad option either.
     
  11. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    Tried to do pretty much everything proposed but I can't physics work as I would like them to.

    To help understand my problem I created image that should explain what happens
    Maybe I'm still missing something obvious here.
     
  12. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    After I converted my scene to 3D I got exactly what I wanted. I needed to change "Bounce threshold" in Editor -> Physic Settings.

    There are few other threads that experience same thing since (I guess it's called Box2D physics) doesn't expose that value for modification.
     
    JaisyKo likes this.
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Box2D has a velocity threshold (in 2D physics settings) which I converted from a compile-time constant to something you can change at runtime. Any collisions with a relative linear velocity below this threshold will be treated as inelastic.
     
  14. jtiai

    jtiai

    Joined:
    Aug 25, 2014
    Posts:
    20
    Thank you very much! That setting did the trick. I think I tried to play with that but maybe I didn't set it low enough.
     
    TimCurtis likes this.
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    You're welcome. Sorry I didn't connect you with that property earlier. :)
     
    FuBaa and Olipool like this.
  16. tobloef

    tobloef

    Joined:
    Apr 8, 2015
    Posts:
    13
    Thank you for this, this helped me a bunch.
     
  17. TimCurtis

    TimCurtis

    Joined:
    Apr 15, 2021
    Posts:
    2
    Spent hours looking for the solution for this and finally found it! Thank you!
     
    MelvMay likes this.