Skip to main content

Merge Velocities

The library contains one implementation of a merger - CombineVelocitiesJobWrapper. It turns all the VelocityResult from behaviors into one DesiredVelocityComponent which the movement systems use to update the entities' VelocityComponent. The merger requires that a DesiredVelocityComponent is present on the entity. Behaviors which can be used with it must return VelocityResult (all behaviors in the defaults library do).

CombineVelocitiesJob

For each entity, the merger sorts it's VelocityResults by Priority first. Starting with the highest priority, it starts accumulating a weighted sum of VelocityResult.Direction * VelocityResult.DirectionDesire, until there are results or sum of VelocityResult.DirectionDesire is at least one. This way results from low priority behaviors can get completely filtered out. The direction of the weighted sum is then the desired direction in DesiredVelocityComponent. In the same way, a weighted sum of all VelocityResult.Speed * VelocityResult.SpeedDesire is calculated. It determines the desired speed in DesiredVelocityComponent.

Description of the image

Video and an image showing CombineVelocitiesJobWrapper combining influences of three behaviors, green - CohesionJobWrapper, red - SeparationJobWrapper and yellow WanderingJobWrapper. the final desired direction is shown in white.

DesiredVelocityComponent

Holds the desired velocity of the entity. Movement systems then look at this as the direction and speed which the entity wants to have, and then modify its VelocityComponent accordingly.

DesiredVelocityAuthoring.cs
public struct DesiredVelocityComponent : IComponentData
{
public float3 Value; // Desired velocity - direction and speed
public bool Debug; // Toggle to debug
public float DebugScale; // Scale for debugging
public Color Color; // Color to draw Value with
}