Search Unity

I need help about optimizing physics in mobile devices

Discussion in 'Physics' started by e01100111e, Apr 15, 2020.

  1. e01100111e

    e01100111e

    Joined:
    Aug 5, 2017
    Posts:
    20
    Hello!

    I have made a game which uses hundreds of objects with sphere colliders and rigidbodies. For the game design, I hold those objects in a jar like candies, and I need them to fall down like when the jar breaks as in the real world. I have tested with an iPhone 8, 10, 11 and Samsung Galaxy S8 which I have no significant lags but in others, especially in Android devices, it's like hell. I checked on profiler and it seems like the problem is mostly physics. I guess because those candies are constantly colliding with each other, physics engine go nuts and in most of the devices it's not handled easily.

    If I make them not collide with each other by layer settings, they don't look like candies on top of each other like in a jar, instead they get inside of each other.
    If I remove the rigidbodies or make them kinematic, the candies don't fall when the jar is broken. The jar is not supposed to be always completely broken, sometimes only some part of it gets broken and the objects inside spill down from the crack. So I couldn't figure out what to do.


    I don't know much about the optimization, I'm still learning with Unity lessons, but I need urgent help in this case :(

    You can see the attached image of profiler.

    Thanks in advance!
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Your main problem is just the sheer number of objects in your scene. It may simply not be possible on some slower phones to do the simulation in real time.

    May I ask if this simulation needs to be dynamic each time? Will there be different candies and will they have to fall in a different way each time? If you can get away with it looking the same every time you do it, you could attach a https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Animations.GameObjectRecorder.html to each object in the scene and create a preset animation for each object. Then you could play the animations back with Animators. This will be much more performant than doing real time physics.
     
    e01100111e likes this.
  3. e01100111e

    e01100111e

    Joined:
    Aug 5, 2017
    Posts:
    20

    Thank you so much! I really wanted it to be physics related game, so the candies fall depending on the broken jar parts and fall naturally. However, I didn't know about GameObjectRecorder at all! I will try to adapt this to the game in the worst case scenario.
     
  4. Grimps

    Grimps

    Joined:
    Nov 4, 2016
    Posts:
    15
    Since it's a bit out of scope to explain exactly what your needs are, I can't be sure this is a wise suggestion, but you could make the candies particles that collide with the jar (but not each other). And then you can get callbacks when those collisions hit and visualy 'fill' the jar up as more and more candies hit your trigger. This requires a good deal of understanding of the particle system.
     
    e01100111e likes this.
  5. e01100111e

    e01100111e

    Joined:
    Aug 5, 2017
    Posts:
    20
    Thank you! I didn't know particles consume less than standard rigidbodies when it's come to physics, so I didn't even consider using particles in this option
     
  6. vecima

    vecima

    Joined:
    Jun 6, 2017
    Posts:
    16
    Another possible option would be to utilize each candies "Awake" and "Asleep" physics state. I'm not actually sure how feasible this is, but the idea that came to mind is to have each candy cast a ray in a few directions (or maybe even just toward jar openings) and if they find that they are blocked by other candies, put the rigid body to sleep (or possibly even change it to kinematic). if they are able to ray cast to the opening without hitting other candies, then it may be their turn to fall, in which case the rigid body should be awoken, or turned back to dynamic if you switched it to kinematic.

    The overall idea here is that it should be fairly cheap for each candy to cast just one, or maybe a few rays and if they can figure out that they won't need to move soon remove themselves from the simulation. You could possibly also optimize the performance of the ray casting by making them wait some amount of time between casts instead of just casting every Update or FixedUpdate.
     
    MuffinMyst and e01100111e like this.
  7. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    250
    Not sure what the game is, but perhaps it’s possible to do 2d physics?
    Eg for a lot of my 3D games, I’ve used 2d physics, eg using box2d which is far computational less expensive than 3d

    Also use the simplest collision shape possible, eg circle for 2d , sphere for 3d
     
    e01100111e likes this.
  8. e01100111e

    e01100111e

    Joined:
    Aug 5, 2017
    Posts:
    20
    Damn this is so practical! I heard of sleeping rigidbodies but this usage is something that I thought impossible! Thank you! <3
     
  9. e01100111e

    e01100111e

    Joined:
    Aug 5, 2017
    Posts:
    20
    Yep, I already use 3d for sphere :) Thanks anyways!
     
  10. MuffinMyst

    MuffinMyst

    Joined:
    Nov 17, 2014
    Posts:
    13
    Did you figure out a good way to do this? Did it work? Just asking because this might be the solution I'm looking for as well...
     
  11. e01100111e

    e01100111e

    Joined:
    Aug 5, 2017
    Posts:
    20
    Hi! Actually I haven't, I delayed this project for another, but definitely would let this post know, and really appreciate if you would do so :)
     
  12. X1one

    X1one

    Joined:
    Aug 13, 2020
    Posts:
    1
    I think you guys need to adjust physics time to reduce physics calculations that can help you alot
     
    e01100111e likes this.
  13. inSight01

    inSight01

    Joined:
    Apr 18, 2017
    Posts:
    90
    I know this is an old thread. But it's an issue I'm currently investigating myself. I used to play a game called Auralux (and the improved version Auralux: Constellations). Neither work on modern devices as the game is quite old. But it was able to have many thousands of physics objects with collisions even on very old mobile devices. I assume it was using some very naïve custom physics engine which focused more on performance than accuracy. But I'm struggling hard to implement something similar. Even Google created LiquidFun which is a custom Box2D engine that could render thousands of physics bodies and work on very old devices. And that was from 9 years ago.

    Surely this can be done without using ECS?
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    No, that's a particle simulation inside Box2D that made massive assumptions because it was taylored for a particle simulation; not a rigidbody simulation where each body has colliders/joints attached and each is in a broadphase for fast querying. Extrapolating that to infer each is a Rigidbody2D and all its features isn't correct.

    A "physics" object could be a point in space (particle). It's the extra features that consume performance.

    The Unity particle system can do similar in that its a particle simulation that can also use the 2D/3D physics to interact with colliders/bodies. That doesn't make it a generic physics simulation.