Search Unity

Demo: Megacityish traffic system that is physics driven close to player

Discussion in 'Entity Component System' started by Birkeman, Dec 10, 2018.

  1. Birkeman

    Birkeman

    Joined:
    Apr 22, 2013
    Posts:
    21
    I'm working on a flying taxi game called Hover Cabby where you zoom around a big futuristic city doing various tasks as quickly as possible. I'm targeting 60 fps on consoles and 100+ fps on PC for the players that have high refresh rate displays so performance is a top priority.
    I was really happy when ECS was announced and even more so when the Megacity was revealed since using ECS will help me tremendously. When Unity said you were going to be my engine team I didn't expect this kind of specific support :p Eager to learn ECS I started porting over my traffic system. I had some goals for the new implementation:
    • Scale better than the old system that ran on rigidbodies
    • Switch to be driven by rigidbodies when close to the player to still allow for collisions
    • Support cars of different sizes, meshes, materials and collider shapes
    • Allow cars to be run as either pure ECS or game objects since some cars like the trucks carrying boost around affect gameplay and use particle systems while a lot of the simpler cars only has one mesh they are rendered with
    • Be easy to add new car types into the simulation
    Here's a video of where I am so far:

    The ease of using ECS has completely blown me away. I've done a lot of compute shaders recently so thinking in DOD was not new to me but I had not done a lot of CPU multithreaded code before so I was pleasantly surprised with how easy it was to get into ECS and Jobs. The structure it forces you to use makes it quick to figure out and the (mostly) clear error messages makes it quick to figure out if you've made a mistake.

    My collision system works by having a jobified system check if an agent is close enough to the player. If it isn't yet driven by physics it has a flag component added to it. Afterwards a non jobified system adds it to a list on a mono behaviour that in the next frame gets a corresponding game object with matching colliders and rigidbody and assigns the entity to be driven by the position of the rigidbody. The updating of positions is done by simply looping over all the rigidbodies and assign the entity's position component to be the same as the rigidbodies' transform component. The hardest part here was to get the physics simulated agents to move in a similar way to the ones purely driven by ECS. This still needs some more work. Here's an image that shows how the cars close to the player has collisions enabled on them.

    upload_2018-12-10_14-5-6.png

    With my old system I could only have 1.4k agents whereas with the new system I'm up to 6.3k. Now the rendering is the bottleneck so the plan is to look into LODing next and maybe occlusion culling later. I have a very long draw distance on my camera so I have already implemented a rough distance cull by exploiting the frustrum culling using WorldMeshRenderBounds. Since it contains the rough size of a car as the radius I change the position to be behind the camera if it's too far away based on the radius. It does mean I have to store a car's actual position in a separate component so chunk utilization goes down. The upside is that this culling automatically also works for cars that are rendered with a game object hierarchy so when I get the pure ECS cars to LOD I'll maybe keep it around for game object cars. Plan is to also have the fancy lane switching and merging from the Megacity demo. Streaming traffic lanes in will also be important since there's no reason to simulate and cull cars that are too far away anyways.

    In summary ECS is already at a level where I was able to use it for a game in development and mix it with Unity's existing systems. Really looking forward to see where it goes from here!
     
    Antypodish, elcionap, Flurgle and 7 others like this.
  2. Nice job, keep up the good work! :)
     
  3. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    looking fantastic, keep us updated!
     
  4. mikerz1985

    mikerz1985

    Joined:
    Oct 23, 2014
    Posts:
    79
    That looks great! Good job
     
  5. Deleted User

    Deleted User

    Guest

    Looks like it could be quite widespread, end up going to another country or continent...

    The big question though is...

    ... Will the radio constantly play The Offspring?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Indeed nicely done. Any progress since December. I wonder?