Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help Wanted Rotating a Non-Uniform Scaled Collider

Discussion in 'DOTS Physics' started by doompr, Jul 26, 2021.

  1. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    Hey everyone,

    So I stumbled upon the same issue as explained here, however I don't have a LocalToWorld to use: https://forum.unity.com/threads/localtoworld-rotation-affected-by-entity-scale.882133/

    I start off with a rotated non-uniformly scaled GameObject prefab, which only consists of a collider. I convert it to an Entity and then when trying to apply rotations to it, it doesn't rotate correctly with the GameObject. In this specific case I simply copy + paste the rotation from the visual (as seen in the picture) to the entity (which is the collider).

    This works fine if the prefab itself has all rotational values to 0.

    Some fixes given in the thread above are to use:
    - quaternion.LookRotationSafe(fwd, up)
    or
    - Unity.Physics.Math.DecomposeRigidBodyOrientation(localToWorld.Value)

    However the rotation I'm copying is not from an Entity, so I cant use LTW from the visual object, I've tried creating a new LTW component on the go with the visuals' position / rotation / scale but it doesn't seem to work. Altering the LTW component itself didn't give me the results I am looking for either.

    Now I could fix it by rotating all objects used in the prefab to a zero rotation in blender, but I'm sure there's a better solution for this where I can freely use rotations / scaling in prefabs before converting & applying rotations on it later.

    For info: Im using the latest entity/havok packages

    Thanks in advance!
     

    Attached Files:

  2. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34
    Unless I'm understanding your problem incorrectly, I changed the default sync system to solve this issue in this thread https://forum.unity.com/threads/syn...-without-weird-rotation.1135444/#post-7311553 Simply create that script, and then add a new Authoring Component script onto the object you want rotated so the system knows to sync your object (in my script I call it "MyFixedSync") like so...

    Code (CSharp):
    1. using Unity.Transforms;
    2. using Unity.Mathematics;
    3. using Unity.Physics;
    4. using Unity.Entities;
    5.  
    6. [GenerateAuthoringComponent]
    7. public struct MyFixedSync : IComponentData
    8. {
    9. }
    10.  
    Also fair warning this script doesn't seem to be burst compatible unless you disable safety checks "Jobs > Burst > Safety Checks" or just delete the line [BurstCompile]
     
  3. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    I do run my own sync system as I don't need to set the position/rotation/scale every frame, and coincidentally I found your thread before the one I posted. However I'm trying to copy the rotational values from a GameObject and not another Entity, which makes the difference :(

    Thanks anyway for the input!
     
  4. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    Just to clarify: turns out that moving / rotating after prefab conversion is working as expected regardless of rotation / scale in the prefab.

    However after using a custom scale tool the GO which is used to generate the collider is scaled accordingly and then when I regenerate the collider things go wrong when setting the rotation after that. At uniform size the rotations are fine but any non-uniform scale is wrong. This seems to only apply if the original GO had some rotational non-zero value.

    The rotations of the collider are being set correctly, so it seems regenerating a non-uniform collider is part of the cause?
     

    Attached Files:

    Last edited: Jul 27, 2021
    LandonF likes this.
  5. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34
    I think you're running into the problem again that, after conversion, scale is baked into the physics shape. This is an old thread but maybe something like the CompositeScale component may help? https://forum.unity.com/threads/transformmatrixcomponent-and-scaling.524054/#post-5390964. I would also take a look at the CopyTransformFromGameObjectSystem.cs to see how Unity applies transformation to an entity from the GO data.

    Have you tried doing what that system does and setting a new localToWorld for your entity using the GO's properties?
    Code (CSharp):
    1.                 localToWorld = new LocalToWorld
    2.                 {
    3.                     Value = float4x4.TRS(
    4.                         myGameObject.position,
    5.                         myGameObject.rotation,
    6.                         new float3(1.0f, 1.0f, 1.0f))
    7.                 };
     
    doompr likes this.
  6. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    I've tried CompositeScale & setting the LTW matrix before, but no result with either of those unfortunately. It seems that I have to regenerate the colliders after scaling regardless as scalable objects have colliders varying from box to mesh. (I could technically make a scaling option for every single physicsshape as mentioned in the examples, but for meshes it would still require a regeneration)

    It feels like if the converted entity is a child of an object which has rotational values those values are somehow embedded into the collider as well. I've included an example gif (with blue background) of this where the left boxcollider entity is a child of an object which is rotated 5, -45, 0. And the right one is just 0,0,0. As for the top parent this is also 0,0,0.

    When scaling in this gif, the colliders are regenerated constantly, as soon as its not uniform the left collider just rotates wrongly.

    I've included another gif where I rotate the collider in the prefab itself, this is with the root parent scaling set to 1.1, 0, 0. (Other values are the same as described above)
     

    Attached Files:

    LandonF likes this.
  7. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34
  8. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    In the end I just adjusted all models which were used in prefabs so that their prefab orientation was 0,0,0.

    I'm sure there's a way to figure out the reverse calculation to not do this, but I don't have all the time to figure it out, I did manage to see that the children who where rotated by 90 degree steps would rotate fine with the parent for whatever reason :)
     
unityunity