1
\$\begingroup\$

I have properly defined the CollisionEventJob, scheduled it, and attached necessary components to my entities, but for the love of God, ECS is not detecting collisions between those entities. Here is the setup. The entities involved in the collision are initially child game objects and are converted to entities during runtime using my authoring script. They have physics shape and physics body attached and are set to collide raise trigger events. I tried following Unity's documentation, but they have a terrible documentation on the actual implementation of collision detection for SystemBase. How can I make the CollisionEventJob properly detect collisions between entities?

Setup image: enter image description here

[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(PhysicsSystemGroup))]
public partial class VertexUpdater : SystemBase
{
    public GameObject ObjectManager;
    public ObjectManagerController objectManagerController;
    [HideInInspector] public List<SpriteMeshModifier> spriteMeshModifiers = new List<SpriteMeshModifier>();
    // [HideInInspector] public List<Vertex> vertices = new List<Vertex>();  // List of vertices; each element of this variable is what I must make magnetic
    bool merged = false;

    [BurstCompile]
    public struct CollisionEventJob : ICollisionEventsJob
    {
        public ComponentLookup<IsStaticComponent> isStatic;
        public ComponentLookup<SizeComponent> size;
        public ComponentLookup<MarkForDelete> markForDelete;
 
        public void Execute(CollisionEvent evt)
        {
            Debug.Log("Fuck");
            // Ensure both entities have the IsStaticComponent.
            if (!isStatic.HasComponent(evt.EntityA) || !isStatic.HasComponent(evt.EntityB))
            {
                return; // At least one of the entities doesn't have the required component, so we skip.
            }
            Entity staticEntity;
            Entity dynamicEntity;

            IsStaticComponent entityAStatic = isStatic[evt.EntityA];
            IsStaticComponent entityBStatic = isStatic[evt.EntityB];

            // Determine which entity is static and which is dynamic.
            if(entityAStatic.isStatic && !entityBStatic.isStatic)
            {
                staticEntity = evt.EntityA;
                dynamicEntity = evt.EntityB;
            }
            else if(!entityAStatic.isStatic && entityBStatic.isStatic)
            {
                staticEntity = evt.EntityB;
                dynamicEntity = evt.EntityA;
            }
            else
            {
                // Either both are static or both are dynamic.
                // Randomly select one to be the static entity and the other to be the dynamic entity.
                bool randomDecision = UnityEngine.Random.value > 0.5f;
                if (randomDecision)
                {
                    staticEntity = evt.EntityA;
                    dynamicEntity = evt.EntityB;
                }
                else
                {
                    staticEntity = evt.EntityB;
                    dynamicEntity = evt.EntityA;
                }
            }

            // Modify the size of the static entity by adding the size of the dynamic entity.
            SizeComponent staticSize = size[staticEntity];
            SizeComponent dynamicSize = size[dynamicEntity];

            float currentArea = staticSize.width * staticSize.height;
            float updatedArea = currentArea + dynamicSize.width * dynamicSize.height;
            staticSize.width = (updatedArea/currentArea)*staticSize.width;
            staticSize.height = (updatedArea/currentArea)*staticSize.height;

            size[staticEntity] = staticSize;

            // Mark the dynamic entity for deletion.
            markForDelete[dynamicEntity] = new MarkForDelete { markForDelete = true };
        }
    }

    protected override void OnUpdate()
    {
        spriteMeshModifiers = new List<SpriteMeshModifier>();
        List<GameObject> waterslimesDisplay = objectManagerController.colorSlimesDisplayGlobal;
        foreach (GameObject waterslimeDisplay in waterslimesDisplay) {
            spriteMeshModifiers.Add(waterslimeDisplay.GetComponent<SpriteMeshModifier>());
        }
        consolidateGameObjectToPos2D(Vector3.zero, 0.6f);
        //revertGameObjectToInitialpos2D(0.6f);
        CallJobs();
        QueryVelocitiesAndUpdateStaticComponents();
        var collisionJob = new CollisionEventJob
        {
            isStatic = GetComponentLookup<IsStaticComponent>(),
            size = GetComponentLookup<SizeComponent>(),
            markForDelete = GetComponentLookup<MarkForDelete>()
        };
        collisionJob.Schedule(
            SystemAPI.GetSingleton<SimulationSingleton>(),
            this.Dependency
        ).Complete();
        //Update the actual MonoBehavior game objects based on conditions of the entities
        UpdateMonoBehaviorGameObjectsAfterCollision();
        //throw new System.NotImplementedException();
    }

}

authoring script

public class SlimeAuthoring : MonoBehaviour
{
    public float velocity;
    public Entity thisEntity;
    public GameObject targetSizeObject;

    public void SetEntity(Entity entity)
    {
        thisEntity = entity;
    }
}

public struct IsStaticComponent : IComponentData
{
    public bool isStatic;
}

public struct SizeComponent : IComponentData
{
    public float width;
    public float height;
}

public struct MarkForDelete : IComponentData
{
    public bool markForDelete;
}

public class SimpleBaker : Baker<SlimeAuthoring>
{
    public override void Bake(SlimeAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.Dynamic);
        authoring.SetEntity(entity);
        AddComponent(entity, new IsStaticComponent
        {
            isStatic = true
        });
        // Extract size
        if(authoring.targetSizeObject != null)
        {
            MeshRenderer renderer = authoring.targetSizeObject.GetComponent<MeshRenderer>();
            if(renderer != null)
            {
                Vector3 size = renderer.bounds.size; // this gets the size of the mesh's bounding box
                AddComponent(entity, new SizeComponent{
                    width = size.x,
                    height = size.y
                }); // assuming SizeComponent takes a float for magnitude
            }
        }
    }
}
\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .