Search Unity

Pinball Physics

Discussion in 'Physics for ECS' started by phreezie, Sep 24, 2021.

  1. phreezie

    phreezie

    Joined:
    Oct 3, 2019
    Posts:
    119
    Hi!

    I'm looking into porting a pinball physics engine to DOTS Physics. The original code of this engine (written in C++) has been used for decades now by a rather large community of virtual pinball enthusiasts and is full of heuristics that have been added throughout the years. My goal is to port the most important mechanics like the flippers and rubbers to a modern engine to get a more flexible, performant and dynamic simulation.

    I already have all the geometry and most of the logic needed to run a game, and I have also ported the original physics code to DOTS (the "pure DOTS", no DOTS Physics involved). However this doesn't provide the benefits mentioned above, so having a proper physics engine doing the heavy work would be awesome.

    Here is a rough plan on how I'd like to proceed:

    1. Convert the existing colliders to the new physics shapes.
    2. Move velocity and displacement systems into the new physics loop
    3. Override collision handling for certain objects (e.g. ball <-> flipper)
    4. Add and implement "restitution fall-off" (the restitution of a rubber between a metal wall and a ball decreases when the ball collides with high velocity).
    5. Add and implement "scatter", which adds configurable randomness to collision reflection angles

    I've done 1, which was pretty easy:



    Now, the current engine's main loop looks like that:

    Outer loop
    ---Update velocities
    ---Inner loop
    ------Broadphase/Narrowphase
    ------Update displacements
    ------Solve collisions
    ------Handle contacts

    As far as I've understood from porting the code, Update displacements does the actual movement simulation, while Update velocities applies the movement for proper rendering. The outer loop runs at 1000Hz, so my first question would be:

    How can I run the fixed step at 1kHz with Unity Physics?

    And my second question:

    How can I override collision solving between certain objects?

    For example, what I have as the solver code is a function that takes in the flipper data, ball data and collision time and computes new velocities for both. I guess the first step is to identify that those particular types of objects are colliding. Then, how is possible to run this within Unity Physics?

    The whole project is open source, by the way.
     
    bb8_1 and mgear like this.
  2. DevViktoria

    DevViktoria

    Joined:
    Apr 6, 2021
    Posts:
    94
    What I recommend is to take a look at the Unity DOTS Physics sample, there you can find a lots of system examples for collision handling, especially here:
    https://github.com/Unity-Technologi...ysicsSamples/Assets/Demos/2. Setup/2d. Events
    The way I see it: basically the physics systems take care all of the calculations. You just have to set up everything correctly in order to make things work with the physics engine.

    Actually I started to work on a pinball game. And this is how I made the flipper work:

    Please note the title says: Correct flipper behaviour, but unfortunately this is not a correct behaviour. this is quite buggy in fact, because occasionally the flippers can get stuck in wrong positions. So I have to work on this a bit more :(

    I hope this can help a bit.
     
  3. phreezie

    phreezie

    Joined:
    Oct 3, 2019
    Posts:
    119
    Thanks, it's the "just setting it up correctly" part that I'm struggling with. ;)

    CollisionEventConversionSystem looks interesting though, gonna need to chew through that. Remember I don't want the physics system to take care of all the calculations, but use my own for some type of collisions.

    But cool that you're working on a pinball sim as well! As you might have seen we're pretty far already, and it's open source, so if you want to contribute we're always welcoming new members! :)