Search Unity

[Solved] Correctly scale BoxCollider

Discussion in 'Entity Component System' started by Antypodish, Sep 9, 2019.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,775
    Edit:
    It actually works. I did simple error along the line, during creation of BoxColliders.

    Original Post (Ignore)
    So I have tried multiple times, and seams I am failing to scale BoxCollider.
    I can not see it working, or system trying to resize, causes instant crashes.

    As starting point I use samples
    5. Modify / 5b. Change Collider Size
    https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/UnityPhysicsSamples/Assets/Demos/5. Modify
    Scripts / ChangeSphereColliderRadiusBehaviour.cs

    There is that little Job, I based on
    Code (CSharp):
    1. private struct ChangeSphereColliderRadiusJob : IJobForEach<PhysicsCollider, ChangeSphereColliderRadius, Scale>
    2.     {
    3.         public unsafe void Execute(ref PhysicsCollider collider, ref ChangeSphereColliderRadius radius, ref Scale scale)
    4.         {
    5.             // make sure we are dealing with spheres
    6.             if (collider.ColliderPtr->Type != ColliderType.Sphere) return;
    7.  
    8.             //
    9.             // tweak the physical representation of the sphere
    10.             // grab the sphere pointer
    11.             SphereCollider* scPtr = (SphereCollider*)collider.ColliderPtr;
    12.             float oldRadius = scPtr->Radius;
    13.  
    14.             float newRadius = math.lerp(oldRadius, radius.Target, 0.05f);
    15.             // if we have reached the target radius get a new target
    16.             if (math.abs(newRadius - radius.Target) < 0.01f)
    17.             {
    18.                 radius.Target = radius.Target == radius.Min ? radius.Max : radius.Min;
    19.             }
    20.  
    21.             // newRadius = 1.5f ; // Test override
    22.             scPtr->Radius = newRadius;
    23.  
    24.             // now tweak the graphical representation of the sphere
    25.             float oldScale = scale.Value;
    26.             float newScale = oldScale;
    27.             if (oldRadius == 0.0f)
    28.             {
    29.                 // avoid the divide by zero errors.
    30.                 newScale = newRadius;
    31.             }
    32.             else
    33.             {
    34.                 newScale *= newRadius / oldRadius;
    35.             }
    36.             scale = new Scale() { Value = newScale };
    37.         }
    38.     }

    and line with pointer, which sets the radius.
    scPtr->Radius = newRadius;

    So that works

    Then I had similar system, but instead
    Code (CSharp):
    1. SphereCollider* scPtr = ( SphereCollider* ) collider.ColliderPtr ;
    I changed to
    Code (CSharp):
    1. Unity.Physics.BoxCollider* scPtr = ( Unity.Physics.BoxCollider* ) collider.ColliderPtr ;
    Then I tried set new size
    Code (CSharp):
    1. scPtr->Size = new float3 (1) ;
    Or 0.1f, or 2f etc. But now that Unity crashes.

    I removed scaling mesh part, past line 24 for simplicity.
    But no avail.

    How I suppose to do it correctly with these pointers?
     
    Last edited: Sep 9, 2019
  2. Timboc

    Timboc

    Joined:
    Jun 22, 2015
    Posts:
    238
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,775
    Thx for a replay.

    Uh. I feel a bit daft now however, as I just solved it.
    I was creating Compound instead BoxCollider, which triggered few issues along the line.
    I was disabling of course
    Code (CSharp):
    1. if (collider.ColliderPtr->Type != ColliderType.Box) return;
    But nvm. It actually works :)
     
    Timboc likes this.