Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Learning about Unity physics, strange things happening with ball and blocks

Discussion in 'Physics' started by Reloque, Dec 19, 2018.

  1. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    After watching the tutorial on the breakout game, I started to make one of my own as practice. And make it slightly more complex. The games uses 3d physics and colliders. The ball is a sphere collider, the walls are cubes with box colliders, the blocks are using meshcolliders. All items are 'bouncing ball' materials, no friction, bounciness of 1 and combines of average. So i'd expect never to lose velocity. If i understood correctly that is.

    The paddle is a kinematic rigidbody. So are the blocks. All seems to work fairly well, until the ball hits at a angle. Then this happens.



    The bounce angle suddenly is straight. The ball seems to lose some velocity, then in the corner loses all speed. Also it seems to wiggle through the side to hit multiple blocks at once.

    Is this some limitation of the physics I ran into? Is my configuration or interpretation wrong? What kind of problem am I looking at here. Please bear in mind, this is a learning exercise for me, so forgive me if my questions are strange or basic.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    "This video is not avialable"

    Why mesh colliders on the blocks and not box colliders? They are a lot more efficient for the physics system.

    Show us your relevent code
     
  3. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    Not available? That's weird. I'll check youtube, should work at;


    I use mesh colliders to find out how they work. It's a learning project. As for code, there is very little to no code to this game yet. Just an OnCollision bit;


       // Update is called once per frame
    void Update()
    {
    if (destroyBlock)
    {
    destroyBlock = false;
    AudioSource audio;
    audio = GetComponent<AudioSource>();
    audio.Play();
    GetComponent<MeshRenderer>().enabled = false;
    GetComponent<Collider>().enabled = false;
    Destroy(gameObject, 2);
    }
    }
    void OnCollisionEnter(Collision col)
    {
    destroyBlock = true;
    }
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Im not sure why its loseing velocity, but you could just keep track of the direction and force its velocity to your wanted speed
     
  5. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    I could do that. But I'd like to do it using the physics engine in Unity. I'm trying to learn how to use it, and working around the engine and doing it manually doesn't seem to right route for that.
     
  6. If you play it in YouTube, and you slow down the video when the slow down happens (hehe), then you can see that the ball is forcing through the boxcollider corners. Which means vertical bouncing happening between the corners of the collider and the ceiling. When it finally bounces back it loses all its energy. I would advise against using mesh colliders or box colliders to do these kind of things, physics currently is not deterministic, which means you're relying on luck somewhat. Like these cases.
    The best solution for these type of games is if you have triggers only and handle the bounce back yourself (just send back the ball where it came from with the same velocity to not to loose energy).
    Blocks games and non-deterministic physics engines aren't really friends.
     
  7. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    And here I was thinkging that building a block game would be a good way to learn about the Unity physics engine. At least I understand it a bit now.

    So, trigger and perhaps a debounce of sorts. Like in electronics. Thanks.
     
  8. Well, technically you have learned about it. :D

    BTW, if you want to intentionally fall on your sword, you can try to build a golf game purely on physics. ;) Or mini-bowling with the string and the pins.
     
    SparrowGS likes this.
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Learning you cant do something(either personal or technical limitations) is still a good thing to learn.

    It lets you know if something is gonna work, and it makes you more intuative about (in this case) Unity and PhysX.
     
    Reloque likes this.
  10. Oh BTW, also can fix this very issue if you remove the top and bottom rows from the blocks, so the ball has enough space to bounce off the colliders (blocks and the ceiling), but, you also will have problems with the blocks' corners. It always will put off the ball somewhere. And if you have two corners close to each other like this, you can have this very same problem but between two blocks.
    You also can play with the bounce threshold and with the solver iterations, but again, this physics engine is not deterministic, so you can't predict that you will have the good results every time.
     
  11. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    You are technically correct. The best kind of correct. I did learn and I did solve it by doing it 'by hand' as suggested.
     
    Lurking-Ninja likes this.