Hi all, I am pretty new to ECS and have recently learnt about collisions and triggers.
Whilst having things react when they are different component data types has been obvious (eg a bullet colliding with an enemy and the enemy PhysicsVelocity goes flying), what I want to know is given two objects of the same type how do I use them?
In my game I have a tonne of objects just moving about. They have a ‘HumanInfectionData’ type on them. I want to start with one infected and as they ;touch’ each other, the infected infect the others…
This is what I have in the collision system, and the execute is just bad right now, but the main point is, how to check collisions between objects of the same type, and update those objects?
[GenerateAuthoringComponent]
public struct HumanInfectionData : IComponentData
{
public bool isInfected;
}
[UpdateAfter(typeof(EndFramePhysicsSystem))]
public class HumanInfectionTriggerEventSystem : JobComponentSystem
{
// we need a couple of extra worlds that have finished simulating
BuildPhysicsWorld physicsWorld;
StepPhysicsWorld stepWorld;
protected override void OnCreate()
{
physicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
stepWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
}
struct HumanInfectionTriggerEventJob : ITriggerEventsJob
{
[ReadOnly] public ComponentDataFromEntity<HumanInfectionData> HumansGroupA;
public ComponentDataFromEntity<HumanInfectionData> HumansGroupB;
// this wont work this way, I need to know how I can approach this.
public void Execute(TriggerEvent triggerEvent)
{
Entity entityA = triggerEvent.Entities.EntityA;
Entity entityB = triggerEvent.Entities.EntityB;
bool entityInGroupA = HumansGroupA.Exists(entityA);
bool entityInGroupB = HumansGroupB.Exists(entityA);
if (entityInGroupA)
{
var AData = HumansGroupA[entityA];
var BData = HumansGroupB[entityB];
if (AData.isInfected)
BData.isInfected = true;
HumansGroupB[entityB] = BData;
}
else if (entityInGroupB)
{
var AData = HumansGroupB[entityA];
var BData = HumansGroupA[entityB];
if (AData.isInfected)
BData.isInfected = true;
HumansGroupA[entityB] = BData;
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
JobHandle jobHandle = new HumanInfectionTriggerEventJob
{
HumansGroupA = GetComponentDataFromEntity<HumanInfectionData>(),
HumansGroupB = GetComponentDataFromEntity<HumanInfectionData>()
}.Schedule(stepWorld.Simulation, ref physicsWorld.PhysicsWorld, inputDeps);
jobHandle.Complete();
return jobHandle;
}
}
It simply looks at the 2 entities and if one is ‘infected’ it infects the other. Pretty basic, but I can’t compare 2 lists where the same entity may exist in both as I get a native array exception.
I have been searching but am struggling to figure out the best way forward. I cannot think of a way to separate out the same archetypes to be able to do this…
Does anyone have any suggestions?
Edit: I found this thread which could work, but I need to update my component data types as part of my solution, so I am still stuck. Cheers