Hi this is my first project with using ECS, i am currently creating a collision detection system for circle colliders, currently my code look like this:
public class CollisionSystem : JobComponentSystem
{
[DeallocateOnJobCompletion]
public NativeArray<CircleCollider> Colliders;
[BurstCompile]
struct CollisionJob : IJobParallelFor
{
[ReadOnly]
[DeallocateOnJobCompletion]
public NativeArray<CircleCollider> colliders;
public void Execute(int index)
{
int size = colliders.Length;
for(int j = 0; j < size; j++)
{
float3 pos = colliders[j].position;
//collision detection
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
Colliders = GetEntityQuery(typeof(CircleCollider)).ToComponentDataArray<CircleCollider>(Allocator.TempJob);
var job = new CollisionJob()
{
colliders = Colliders
};
var jobHandle = job.Schedule(Colliders.Length, 64);
return jobHandle;
}
}
And i got some questions, why i got such low performance on that ? If i want to take a collider from the array i got low performance, but when the loop got no operations inside, everything is ok. How can i create such comparison mechanic by using jobs ?