Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Total Pool 2

Discussion in 'Made With Unity' started by Giles-Coope, Apr 14, 2022.

  1. Giles-Coope

    Giles-Coope

    Joined:
    Jan 5, 2015
    Posts:
    50
    Hey guys!

    I've been working on my pool and snooker games a bit recently, this one is made with Unity with my own custom C# physics engine. I use the shader graph for some cool rendering tricks, Unity Ads Mediation for advertising, and am just starting with the Localization package.

    https://apps.apple.com/bw/app/total-pool-2/id1392507030
    https://play.google.com/store/apps/details?id=eu.friendlymonster.totalpool2

    Screenshot_14-04-2022-20-50-48.png

    I'm not getting much traction at the moment so would love a review and some advice and tips. I don't know a huge amount about pool culture in the US for example and would love to know if I'm missing something.

    Also happy to answer any questions about how it was made.
     
  2. impheris

    impheris

    Joined:
    Dec 30, 2009
    Posts:
    1,511
    O. O your own physics engine?? you definitely have to talk about that...
     
  3. Giles-Coope

    Giles-Coope

    Joined:
    Jan 5, 2015
    Posts:
    50
    Yes of course, there's a lot to say here. I'll try and write in a FAQ style to anticipate some of the questions you might have, but please feel free to ask more.

    Why not use an off-the shelf physics engine?
    - although generic physics engines are amazing at creating plausible and performant physics simulations, they don't capture the phenomena of pool and billiards very well
    - sliding and rolling friction are the main forces outside of collisions and these aren't well modeled usually, would have to hack this in myself
    - collisions of balls against cushions are complex (two simultaneous contacts, one of which deforms) and would also have to be handled as a special case
    - balls move fast relative to their size, simple intersection checks could lead to balls going through other balls or cushions unless timestep is very slow = very expensive
    - they are too expensive to run on low power mobile hardware
    - collisions of balls packed together, such as on the break are usually solved with simple heuristics (solving jacobian of contacts) which aren't correct and experienced pool players will notice the discrepancy
    - due to all of the above, the amount of custom work you would have to make to the physics engine means you aren't left with much of the engine left and you might as well start from scratch with your own

    Isn't it really difficult to write a physics engine?
    - yes and no, since my only kinetic objects are balls, which are spherical, there are a lot of shortcuts and speed ups I can take advantage of. For example, imagine you are trying to test if two arbitrary meshes are colliding given positions and orientations, that is a non-trivial test that will involve many steps of checking triangles and points against one another. Now imagine you are trying to test if two spheres are colliding, you simply take the distance between the centres and compare it against the sum of the radii. My physics engine is a specialist one, I can't do any physics that doesn't involve spheres on a pool table.

    What are the things that make your engine special?
    - I don't use a fixed timestep, updating the positions and then seeing what is colliding and resolving, instead I use closed-form equations of the ball trajectories given their initial position, speed and spin and solve these mathematically for intersections with cushions and each other, when I have found the first possible collision time, I step the simulation forward to this time, then I resolve the collision, then I recalculate the ball trajectories for the balls which were involved and then look for the next collision. No ball is ever intersecting another ball or can possibly go out of bounds, and complex conditions such as the break (balls packed together being hit fast) are fully simulated and not just approximated
    - it's really fast, as soon as you hit the cue ball the entire shot is simulated in the first frame, and what you are watching after that is just a keyframe playback of the already simulated shot
    - it's really, really fast, when the ai is deciding what shots to play, it has access to the physics engine and can simulate 2000 different shots (within a second on a mobile phone) before it chooses which to play, of course it's important to add some fuzziness to it's logic so it isn't too omniscient and potting balls off three cushions all the time

    Is it 2d?
    - no, the balls need to rotate in all three dimensions to simulate the different spins on the table, also balls falling into the pockets can be handled well
    - I usually keep the balls close to the surface of the table and not jump, firstly it's difficult to see a jump from the perspective I use, and secondly it's usually either undesirable or against the rules to jump the balls to gain an advantage

    How do you do the collisions with the cushions?
    - I treat the cushions as a rigid body, the physical deformation is usally so small that you can't see it with the naked eye
    - I found a research paper which had a complex algorithm for resolving the collision, given some paramters (cushion height, dynamic and static friction for ball-cushion and ball-table, coefficient of restitution), it was far too expensive to simulate on the device but I was able to precalculate a large 5d look up table given the input parameters (speed, angle, 3 dimensions of spin), this can then be accessed and interpolated on the device in real time
    - I verified the coefficients of friction by analysing real life camera footage of pool and snooker games and using computer vision to track the balls, fit the parameters to the observed results
     
  4. impheris

    impheris

    Joined:
    Dec 30, 2009
    Posts:
    1,511
    that is very interesting, i like to play those pool games in my phone but i never thought it was so complicated to make the physics for those games and i'm surprised that is very fast to calculate all that in a phone jeje, very cool