Search Unity

Question Problems in limiting Physics to a 2D Plane

Discussion in 'Physics for ECS' started by Naotagrey, Mar 29, 2022.

  1. Naotagrey

    Naotagrey

    Joined:
    Apr 17, 2016
    Posts:
    37
    Hi!
    I'm trying to create a 2D Game sample using DOTS and Physics.
    I'm trying to limit the movement to a single plane.

    First I thought that on my Prefab to be converted, ticking the constraint boxes ( Freeze position Z, and Freeze Rotation X and Y) would suffice, but that doesn't seem to work anymore after being converted to entities.

    The second thing I tried is having a system that would reset any Z position to zero after physics computation, but I can't figure on how to reset non-Z rotations to zero, all the Mathf quaternion functions like Euler() are not in Unity.Mathematics for Entities

    The next thing I tried was to have a static reference entity and create an additional joint entity for every physics entity I instantiate to create a joint between the shape and the reference entity, but I might just be having trouble getting the right constraints set up to produce the behavior I want...

    For reference, for now I'm trying a simple restriction of all rotation aside from Z, and result in the following joint entity with components:

    Code (CSharp):
    1. var jointEntity = em.CreateEntity();
    2. var joint = new PhysicsJoint();
    3. var constraints = joint.GetConstraints();
    4. constraints.Add(new Constraint()
    5. {
    6.      Type = ConstraintType.Angular,
    7.      Min = 0f,
    8.      Max = 0f,
    9.      ConstrainedAxes = new bool3(true, true, false)
    10. });
    11. joint.SetConstraints(constraints);
    12. em.AddComponentData(jointEntity, joint);
    13. em.AddSharedComponentData(jointEntity, new PhysicsWorldIndex(0));
    14. em.AddComponentData(jointEntity, new PhysicsConstrainedBodyPair( physicsAffectedEntity, EntityCreator.physicsReferenceEntity, false));
    Anyone can point me in the right direction?
    Would be appreciated to know what I've been doing wrong in either or all of my approaches
     
  2. gato0429

    gato0429

    Joined:
    Mar 5, 2015
    Posts:
    22
    Hello, Maybe this can help you, I am making a system that freezes the movement in some ordinate, for this case I freeze the z ordinate and also the rotation. I'll pass you the code that I'm still polishing.

    pdta: remove the component ComponenteUnidad, is component that i use in other systems.


    Code (CSharp):
    1.  public partial class Fisica2DBloqueoZ : SystemBase
    2.     {
    3.         private EntityQuery m_Query;
    4.         BuildPhysicsWorld m_BuildPhysicsWorldSystem;
    5.         StepPhysicsWorld m_StepPhysicsWorldSystem;
    6.         protected override void OnCreate()
    7.         {
    8.             m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
    9.             m_StepPhysicsWorldSystem = World.GetOrCreateSystem<StepPhysicsWorld>();
    10.             m_Query =GetEntityQuery(ComponentType.ReadWrite<PhysicsMass>(),
    11.                 ComponentType.ReadWrite<Translation>(),
    12.                 ComponentType.ReadWrite<Rotation>(),
    13.                 ComponentType.ReadWrite<PhysicsVelocity>(),
    14.                 ComponentType.ReadOnly<ComponenteUnidad>());
    15.         }
    16.         [BurstCompile]
    17.         private struct NiveladorJob :IJobChunk
    18.         {
    19.             public ComponentTypeHandle<PhysicsMass> PhysicsMassType;
    20.             public ComponentTypeHandle<Translation> TranslationType;
    21.             public ComponentTypeHandle<Rotation> RotationType;
    22.             public ComponentTypeHandle<PhysicsVelocity> PhysicsVelocityType;
    23.             public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    24.             {
    25.                 NativeArray<PhysicsMass> physicsMassArray =  chunk.GetNativeArray(PhysicsMassType);
    26.                 NativeArray<Translation> translationsArray =  chunk.GetNativeArray(TranslationType);
    27.                 NativeArray<Rotation> RotationsArray =  chunk.GetNativeArray(RotationType);
    28.                 NativeArray<PhysicsVelocity> physicsVelocitiesArray =  chunk.GetNativeArray(PhysicsVelocityType);
    29.  
    30.                 for (int i = 0; i < chunk.Count; i++)
    31.                 {
    32.                    PhysicsMass masa =  physicsMassArray[i];
    33.                    Translation translation = translationsArray[i];
    34.                    PhysicsVelocity velocity = physicsVelocitiesArray[i];
    35.                    Rotation rotation = RotationsArray[i];
    36.                    rotation.Value = Quaternion.Euler(0,0,0);
    37.                  
    38.                    masa.InverseInertia = new float3(0,0,0);
    39.                    translation.Value.z = 0;
    40.                    masa.Transform.pos = translation.Value;
    41.                    masa.Transform.rot = rotation.Value;
    42.                    velocity.Angular = .00f; // .05
    43.                    velocity.Linear = .00f; //.01
    44.                    physicsMassArray[i] = masa;
    45.                    translationsArray[i] = translation;
    46.                    physicsVelocitiesArray[i] = velocity;
    47.  
    48.                    RotationsArray[i] = rotation;
    49.  
    50.                 }
    51.             }
    52.         }
    53.  
    54.  
    55.         protected override void OnUpdate()
    56.         {
    57.             NiveladorJob jobDetectorCollision = new NiveladorJob
    58.             {
    59.  
    60.                 PhysicsMassType = GetComponentTypeHandle<PhysicsMass>(false),
    61.                 TranslationType = GetComponentTypeHandle<Translation>(false),
    62.                 PhysicsVelocityType = GetComponentTypeHandle<PhysicsVelocity>(false),
    63.                 RotationType = GetComponentTypeHandle<Rotation>(false)
    64.             };
    65.             JobHandle jobHandle = jobDetectorCollision.ScheduleParallel(m_Query, Dependency);
    66.             jobHandle.Complete();
    67.             //m_BuildPhysicsWorldSystem.AddInputDependencyToComplete(jobHandle);
    68.             // m_StepPhysicsWorldSystem.Simulation,
    69.             //ref m_BuildPhysicsWorldSystem.PhysicsWorld,
    70.             //   throw new System.NotImplementedException();
    71.         }
    72.     }
     
  3. JMPM-UNITY

    JMPM-UNITY

    Unity Technologies

    Joined:
    Jun 16, 2021
    Posts:
    94
    Discipol likes this.
  4. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    196
    I'm wondering, why are all these joints not part the Unity.Physics package (or a separate package).
    Seems odd that all these things that are in normal Unity are living in a sample project for DOTS.
     
  5. JMPM-UNITY

    JMPM-UNITY

    Unity Technologies

    Joined:
    Jun 16, 2021
    Posts:
    94
    Because the joint authoring components present in the PhysicsSamples are provided as-is and are subject to change, while we bring the authoring of unity physics to where one can use the normal Joint components from classic unity to author such joints in unity physics.
     
    Occuros and Krooq like this.
  6. Discipol

    Discipol

    Joined:
    May 6, 2015
    Posts:
    83
    We would prefer out-of-the-box settings like in vanilla Rigidbody to lock axis and rotations individually + have the GameObject -> Entity converter of a Rigidbody to take those old locks, naturally.
     
    layker90524 and Tudor like this.