Search Unity

To change Scale and Collider radius in ECS Physic

Discussion in 'Physics for ECS' started by AlexandrBuryakov, Aug 5, 2019.

  1. AlexandrBuryakov

    AlexandrBuryakov

    Joined:
    May 30, 2018
    Posts:
    31
    Hello.
    1) How to change the object size?
    Found only such option:
    Code (CSharp):
    1.  
    2.     var entityManager = World.Active.EntityManager;
    3.     Entity sourceEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy ( go, World.Active );
    4.     BlobAssetReference<Unity.Physics.Collider> sourceCollider = entityManager.GetComponentData<PhysicsCollider> ( sourceEntity ).Value;
    5.     //Some code...
    6.     var instance = entityManager.Instantiate ( sourceEntity );
    7.     var cs = entityManager.GetComponentData<CompositeScale> ( instance);
    8.     cs.Value.c0.x = radius;
    9.     cs.Value.c1.y = radius;
    10.     cs.Value.c2.z = radius;
    11.     cs.Value.c3.w = 1;
    12.     entityManager.SetComponentData ( instance, cs );
    13.  
    14.     entityManager.SetComponentData ( instance, new PhysicsCollider { Value = sourceCollider } );
    15.  
    2) The size of a collider does not change. How to change the collider size?
    I correctly do?
     
    Tudor likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    From memory, collider scale is baked into the physics shape during conversion and can't easily be changed.

    You'd need to regenerate the collider.
     
  3. AlexandrBuryakov

    AlexandrBuryakov

    Joined:
    May 30, 2018
    Posts:
    31
    1) How it can be done?
    2) There is still a thought to have empty GameObject with a collider. Without restoration of a collider. To adjust a collider before converting.
    But I do not know how to calculate correctly the collider size. It is necessary to establish localScale as in CompositeScale?
    3) The second way will be more productive? (But it is necessary to store some settings separately.)
     
  4. mpforce1

    mpforce1

    Joined:
    Apr 4, 2014
    Posts:
    34
    Hi, just to add to this, in my current project we're using ECS and have found that the colliders generated using the conversion system don't change scale along with the Scale or NonUniformScale. However, they do move along with the Translation and Rotation. I would also like to know if:
    1) There is an existing API available that will allow us to create/modify the collider with expanded vertices?
    2) It is planned to make colliders scale along with the scale component in the future?
     
  5. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    Scale is not yet represented in the PhysicsWorld, largely because it adds significant complexity and because non-uniform scale in particular is not applicable to certain use cases (spheres and capsules).

    The current situation is:
    * Any scale present in a game object is baked into the collider during the conversion system
    * The physics world only uses entity position and rotation after that point
    * Primitive colliders (sphere, capsule, box, cylinder) have properties allowing you to scale them inplace if desired

    What to expect in future:
    * Convex, Mesh and Terrain collider will likely offer a "scale" property or method to modify them inplace
    * We want to experiment with applying at least the _uniform_ scale from entities into the physics world each frame (but no concrete plan)

    Hope that helps
     
    Occuros likes this.
  6. Vicotin

    Vicotin

    Joined:
    Mar 18, 2014
    Posts:
    7
    lclemens and SteffenEichler like this.
  7. edalbeci

    edalbeci

    Joined:
    Jan 21, 2018
    Posts:
    36
  8. mpforce1

    mpforce1

    Joined:
    Apr 4, 2014
    Posts:
    34
    Hello again, has there been any progress in providing a scale method for mesh colliders? If not could you provide some guidance on how scaling can be done? I'm having trouble figuring out how to extract the vertex and triangle arrays from an existing mesh collider. Additionally, the MeshCollider.Create method wants a filter and material but mesh collider only seems to expose a filter. Thanks.
     
  9. KoomZog

    KoomZog

    Joined:
    Apr 7, 2020
    Posts:
    4
    I also ran into this issue. Having changes to mesh scale also affect the collider scale would be useful to me.

    I circumvented the problem by deleting the old entity and spawning a new one from a modified prefab with the scale I wanted. It's not pretty, but it works.
     
  10. meanfox

    meanfox

    Joined:
    Apr 9, 2020
    Posts:
    25
    Another vote for scale to work with physics please.
     
  11. Koamr

    Koamr

    Joined:
    Jul 19, 2013
    Posts:
    8
    Hi, any update on scaling the collider?
     
  12. DomingoGD

    DomingoGD

    Joined:
    Jan 28, 2018
    Posts:
    16
    Up.

    Also Looking for a way to scale the collider.

    Hi Rory, any hints how to modify the primitives scale property ? (after entities are instantiate via SetComponentdata()).
     
  13. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    You typically GetGeometry() (say SphereGeometry or BoxGeometry), change what you want (scale the radius or size) and then SetGeometry().
     
  14. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    VolatileCoder likes this.
  15. VolatileCoder

    VolatileCoder

    Joined:
    Sep 21, 2014
    Posts:
    18
    In the demo, it's updating the radius property of a sphere collider. This works fine for simple colliders (spheres, cubes, etc), but would this work when scaling a mesh or convex hull collider?

    We really need a utility function for collider transformations/deformations at runtime.
     
    TorchFireGames likes this.
  16. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Uniform runtime scale is something we are planning on investigating, however at the minute for convex hulls or meshes the easiest thing is to modify the input geometry and recreate.
    Note that non-uniform runtime scale is not being considered.
     
  17. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Current status?
     
    hippocoder and OnkelAffe like this.
  18. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Just dropping by: I only need to change primitive collider dimensions - is this supported, and if so, how?
     
  19. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
  20. Lukas_Kastern

    Lukas_Kastern

    Joined:
    Aug 31, 2018
    Posts:
    97
    You should be able to do something like this:
    Code (CSharp):
    1.     Entity colliderEntity = default;
    2.     var collider = PhysicsColliderFromEntity[colliderEntity].Value;
    3.  
    4.     if (!collider.IsCreated)
    5.         continue;
    6.  
    7.     if (collider.Value.Type == ColliderType.Box)
    8.     {
    9.         var boxCollider = ( BoxCollider* )collider.GetUnsafePtr( );
    10.  
    11.         var geometry = boxCollider->Geometry;
    12.  
    13.         // Change the dimensions to whatever you like.
    14.         geometry.Size = new float3( 2.5f, 2, 2.5f );
    15.  
    16.         var bevelRadius = 0.05f;
    17.  
    18.         // Clamp the bevel radius to less than half the size of the smallest dimension.
    19.         // Otherwise Unity will yell at us.
    20.         geometry.BevelRadius = math.min( math.cmin( geometry.Size ) * 0.499f, bevelRadius );
    21.  
    22.         boxCollider->Geometry = geometry;
    23.     }
    24.     else if (collider.Value.Type == ColliderType.Capsule)
    25.     {
    26.         var capsuleCollider = ( CapsuleCollider* )collider.GetUnsafePtr( );
    27.  
    28.         var geometry = capsuleCollider->Geometry;
    29.    
    30.         // Modify Geometry
    31.  
    32.         capsuleCollider->Geometry = geometry;
    33.     }
    34.     else if (collider.Value.Type == ColliderType.Sphere)
    35.     {
    36.         var sphereCollider = ( SphereCollider* )collider.GetUnsafePtr( );
    37.  
    38.         var geometry = sphereCollider->Geometry;
    39.         // Modify Geometry
    40.  
    41.         sphereCollider->Geometry = geometry;
    42.     }
    43.  
    44.     PhysicsColliderFromEntity[colliderEntity] = new PhysicsCollider( )
    45.     {
    46.         Value = collider
    47.     };
    Keep in mind that PhysicsColliders are usually shared between entities.
    You can toggle ForceUnique on the PhysicsShapeAuthoring to create a collider per converted entity.
    But that won't work when you instantiate your entities from a prefab. Then the instantiated entities will all reference the collider of the prefab...

    You can create colliders that you can modify on a per entity basis by cloning the original collider and cleaning it up once the entity despawns.

    Here is an example out of my code-base where I do something like this.
     
    Tudor, MaskedMouse and hippocoder like this.
  21. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thanks! This will be very useful. I didn't expect it would require a new collider, but I guess it's because of optimisations.
     
  22. DizzyTornado

    DizzyTornado

    Joined:
    Nov 17, 2013
    Posts:
    134
    Hi everyone,
    I have the code below to change the scale of a sphere collider, it does not give any errors but it is not changing the size of the collider.
    Code (csharp):
    1. unsafe
    2. {
    3.      // grab the sphere pointer
    4.      SphereCollider* scPtr = (SphereCollider*)entityManager.GetComponentData<PhysicsCollider>(entity).ColliderPtr;            
    5.      // update the collider geometry
    6.      var sphereGeometry = scPtr->Geometry;
    7.      sphereGeometry.Radius = nodeScale;
    8.      scPtr->Geometry = sphereGeometry;
    9. }
    Anyone know how to fix this? (for context, nodeScale is the radius I want assigned)
     
  23. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    AFAIK you are not allowed to change the collider like this.
    You need to do it in a job that passes PhysicsCollider by ref.
    Here's the relevant snippet from the docs.
    upload_2022-7-27_8-6-28.png
     
    Opeth001 likes this.