void ProcessParentCollider(ArchetypeChunk chunk) { var count = chunk.Count; var parents = chunk.GetNativeArray(ParentType); var entities = chunk.GetNativeArray(EntityType); var bounds = chunk.GetNativeArray(BoundsType); var shapes = chunk.GetNativeArray(ShapeType); var tags = chunk.GetNativeArray(TagType); var cellSize = Constants.CellSize; for (int i = 0; i < count; i++) { var entity = entities[i]; var aabb = bounds[i]; var shape = shapes[i]; var tag = tags[i]; var parent = parents[i]; var sourceCollider = new ColliderData { Bounds = aabb, Entity = parent.Value, ColliderEntity = entity, Shape = shape.Value, Tags = tag.Target, }; var targetCollider = new ColliderData { Bounds = aabb, Entity = parent.Value, ColliderEntity = entity, Shape = shape.Value, Tags = tag.Self, }; // Add collider to where its center is inside collider map var hash = HashUtil.Hash(aabb.Center, cellSize); ColliderSourceMap.Add(hash, sourceCollider); // Add collider to every cell it stays in + neighbour cells inside target map var gridBounds = PhysicsUtil.GetGridBounds(aabb, cellSize); for (int x = gridBounds.x; x < gridBounds.z; x++) { for (int y = gridBounds.y; y < gridBounds.w; y++) { hash = HashUtil.Hash(new int2(x, y)); ColliderTargetMap.Add(hash, targetCollider); } } } }
public void ExecuteNext(int key, ColliderData source) { var hasValue = TargetMap.TryGetFirstValue(key, out var target, out var iterator); while (hasValue) { // TODO: We need to check collider heights aswell if (CanCollide(source.Tags, target.Tags) && source.ColliderEntity != target.ColliderEntity && Bounds2D.DoesCollide(source.Bounds, target.Bounds)) { PossibleCollisions.Add(key, new PossibleCollision { SourceEntity = source.Entity, SourceCollider = source.ColliderEntity, SourceType = source.Shape, TargetEntity = target.Entity, TargetCollider = target.ColliderEntity, TargetType = target.Shape, SourceCenter = source.Bounds.Center, TargetCenter = target.Bounds.Center, }); } hasValue = TargetMap.TryGetNextValue(out target, ref iterator); } }