Search Unity

DOTS and Unity Physics - instantiate Entities and select entities with raycast

Discussion in 'DOTS Physics' started by w4der, Aug 26, 2019.

  1. w4der

    w4der

    Joined:
    Sep 17, 2011
    Posts:
    17
    Hi,
    As I understood there was no physics engine for ECS before. Now it is this:
    https://docs.unity3d.com/Packages/com.unity.physics@0.0/manual/index.html

    I need to instantiate enities with the PhysicsShape component. To use it in InputSystem, where I can do raycast and hover, select entities.
    Unfortunately I'm not able to do this from script. How to setup component called PhysicsShape in script below?

    Could you please help me?
    What I'm doing wrong?
    Is there some more documentation or sample projects?

    Code (CSharp):
    1.   GemArchetype = entityManager.CreateArchetype(
    2.             typeof(Field),
    3.             typeof(Translation),
    4.             typeof(RenderMesh),
    5.             typeof(LocalToWorld),
    6.             typeof(PhysicsShape)
    7.         );
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Mathematics;
    6. using Unity.Physics.Authoring;
    7. using Unity.Rendering;
    8. using Unity.Transforms;
    9. using UnityEngine;
    10.  
    11. public class SpawnBoardSystem : ComponentSystem {
    12.  
    13.     protected override void OnUpdate() {
    14.         EntityManager entityManager = World.Active.EntityManager;
    15.  
    16.         Entities.ForEach((Entity e, ref Board boardData) => {
    17.             NativeArray<Entity> gemArray = new NativeArray<Entity>(boardData.SizeX * boardData.SizeY, Allocator.Temp);
    18.             entityManager.CreateEntity(Main.GemArchetype, gemArray);
    19.  
    20.             var id = 0;
    21.             for (int i = 0; i < boardData.SizeX; i++) {
    22.                 for (int j = 0; j < boardData.SizeY; j++) {
    23.                     var gem = gemArray[id];
    24.  
    25.                     entityManager.SetSharedComponentData(gem, new RenderMesh {
    26.                         material = Main.FlatShadedMaterial,
    27.                         mesh = Main.BlueGemMesh
    28.                     });
    29.                     entityManager.SetComponentData(gem, new Translation {
    30.                         Value = new float3(i * boardData.Spacing, j*boardData.Spacing, 0)
    31.                     });
    32.  
    33.                     // THIS LINE BELOW. HOW should I implement this?
    34.                    // entityManager.SetComponentData(gem, new PhysicsShape{});
    35.                    
    36.                     id++;
    37.                 }
    38.             }
    39.  
    40.             EntityManager.DestroyEntity(e);
    41.         });
    42.     }
    43.  
    44. }
     
  2. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    807
    Hi @w4der,
    1) you need to see the new documentation 0.2 not 0.0, link
    2) a physics entity needs other components.
    More details : link
    Look at Creating bodies from scratch.
     
  3. w4der

    w4der

    Joined:
    Sep 17, 2011
    Posts:
    17
    @Opeth001
    Thank you for your reply. I will dig into this topic.
    It is harder than I expected to start using DOTS in "real case" projects. ;(
     
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    807
    It was hard for me at the beginning ( still sometimes xD ) also ECS still in preview so API changes constantly.
    But when you see the benefits it's worth.
    Performance
    No more super dependencies between systems, each logic is independent from others so can be customized at any time.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    9,038
    w4der and Opeth001 like this.
  6. w4der

    w4der

    Joined:
    Sep 17, 2011
    Posts:
    17
    @Antypodish Thank you very much for this information!!!!!
     
unityunity