Game.Simulation.ObjectCollisionSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
ObjectCollisionSystem detects and resolves physical collisions between in-game objects (including vehicles and lane objects) using Unity DOTS/ECS jobs. It performs spatial queries against the game's net search tree, samples object transforms across frames to find intersections, enqueues detected collisions, and then resolves them by applying velocity/ angular changes and generating Impact / Damage / Destroy events. The system uses an EndFrameBarrier to emit event entities and synchronize command buffer execution.
Fields
-
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used for frame indexing and simulation timing. -
private Game.Net.SearchSystem m_NetSearchSystem
Reference to the net search system used to perform spatial queries against network-related objects and lane objects. -
private EndFrameBarrier m_EndFrameBarrier
Barrier system used to create a command buffer for producing event entities at the end of the frame. -
private EntityQuery m_ObjectQuery
EntityQuery selecting objects considered for collision detection (requires UpdateFrame, Transform, Moving, TransformFrame and excludes deleted/temp/unspawned). -
private EntityQuery m_ConfigQuery
EntityQuery used to fetch FireConfigurationData (used to update structural integrity data). -
private EntityArchetype m_EventImpactArchetype
Archetype used to create Impact event entities. -
private EntityArchetype m_DamageEventArchetype
Archetype used to create Damage event entities. -
private EntityArchetype m_DestroyEventArchetype
Archetype used to create Destroy event entities. -
private EventHelpers.StructuralIntegrityData m_StructuralIntegrityData
Helper storing structural integrity values (updated from configuration and used to compute damage thresholds). -
private TypeHandle __TypeHandle
Generated container of type handles and component lookups used by the scheduled jobs (assigned at creation).
Properties
- None (this system exposes no public properties).
Constructors
public ObjectCollisionSystem()
Default preserved constructor. Initialization of fields is performed in OnCreate rather than the constructor.
Methods
-
protected override void OnCreate()
Initializes system references (SimulationSystem, NetSearchSystem, EndFrameBarrier), builds queries and archetypes, and prepares structural integrity helper. Registers the object query as required for update. -
protected override void OnUpdate()
Main system update: - Computes current transform frame indices based on SimulationSystem.frameIndex.
- Creates a NativeQueue to collect collisions detected by a parallel job (FindCollisionsJob).
- Schedules FindCollisionsJob (IJobChunk) that queries nearby objects via the net search tree and tests per-substep box/box intersections along object motion to find collision times and impact locations.
- Schedules ResolveCollisionsJob (IJob) that sorts collisions, filters duplicates, applies velocity/angular changes, creates Impact/Damage/Destroy events and updates components (Transform, Moving, TransformFrame) for involved entities.
-
Ensures proper dependencies: passes net search tree reader handle, disposes the collision queue after the resolve job completes, adds net search tree reader and EndFrameBarrier producer handles, and sets base.Dependency.
-
protected override void OnCreateForCompiler()
Internal setup used by the generated code path / compiler support: assigns queries and type handles for the system. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Internal helper used by the generated code to initialize any queries used by the system (kept empty aside from a temporary builder in this implementation). -
Nested types (summaries):
-
private struct Collision
Small POD struct describing a detected collision: positions, rotations, impact location, involved entities, collision time and transform frame indices. Implements IComparableto sort by time. -
[BurstCompile] private struct FindCollisionsJob : IJobChunk
Chunk job that:- Iterates entities matching m_ObjectQuery.
- For each entity, constructs swept boxes between previous and current transform frames.
- Uses a NetIterator (INativeQuadTreeIterator) to query nearby objects/lane objects from the net search tree.
- Samples up to 16 substeps (interpolation fractions) and conducts box-box intersection tests to determine collision time and impact location.
- Enqueues found Collision entries into a parallel NativeQueue.
-
[BurstCompile] private struct ResolveCollisionsJob : IJob
Single-threaded job that:- Converts the collision queue to an array and sorts collisions by time.
- Skips collisions involving entities already processed in the same pass.
- Computes point velocities, moments of inertia and angular velocity deltas.
- Applies velocity/ angular changes to Moving components (or packages them into Impact events when the object isn't currently simulated as moving).
- Creates Impact, Damage or Destroy event entities through an EntityCommandBuffer provided by the EndFrameBarrier.
- Uses structural integrity values to determine damage amounts and whether an entity should be destroyed.
-
private struct TypeHandle
Generated structure holding all the Unity Entities type handles, ComponentLookup and BufferLookup objects used by the jobs. Provides __AssignHandles(ref SystemState) to initialize lookups from the current SystemState.
Usage Example
// From another system or mod initialization: get the system and let it run as part of the simulation.
var collisionSystem = World.GetOrCreateSystemManaged<Game.Simulation.ObjectCollisionSystem>();
// If you need to read collision-driven events, listen for Impact / Damage / Destroy events
// by querying entities with the corresponding components in a system that runs after
// EndFrameBarrier has produced them.
Notes: - The system relies heavily on Unity's DOTS (Entities, Jobs, Burst) and the game's net search tree to limit collision checks. - Collision detection uses conservative swept-box checks and substep interpolation (16 samples) to find approximate impact times and locations. - Events are emitted via an EndFrameBarrier command buffer to ensure safe structural changes at the proper point in the frame.