• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ben-rasooli · Sep 12, 2021 at 10:05 AM · physicsdots

PhysicsCollider not moving when setting Translation component [DOTS Physics]

I have a list of points and I want to Collider.CalculateDistance(PointDistanceInput) on them. However, before that, I want to move the collider to a new position. Everything works fine but the problem is that the collider stays at the origin and not moving. Even the entity itself moves and its RenderMesh follows it to the new position. Any help is really appreciated. I've been trying to solve this issue for two days, but no luck!

 protected override void OnUpdate()
 {
   Entity _meshColliderEntity = GetSingletonEntity<MeshColliderEntity_tag>();
 
   if (_someCondition)
   {
     foreach (float3 position in _positions)
     {
       EntityManager.SetComponentData(_meshColliderEntity, new Translation { Value = position });
       PhysicsCollider meshCollider = GetComponent<PhysicsCollider>(_meshColliderEntity);
 
       foreach (float3 point in _points)
       {
         var pointDistanceInput = new PointDistanceInput
         {
           Position = point,
           MaxDistance = 0.001f,
           Filter = new CollisionFilter()
           {
             BelongsTo = 1u << 3,
             CollidesWith = 1u << 2,
             GroupIndex = 0
           }
         };
 
         if (meshCollider.Value.Value.CalculateDistance(pointDistanceInput, out DistanceHit hit))
           Debug.Log(hit.Position);
       }
     }
   }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by andrew-lukasik · Sep 12, 2021 at 04:09 PM


PhysicsCollider will move once BuildPhysicsWorld runs again. Your code doesn't work because PhysicsCollider component has no access to this Translation value you're changing here.


Also, you don't need to change positions to test that.

 using Unity.Entities;
 using Unity.Mathematics;
 using Unity.Collections;
 using Unity.Transforms;
 using Unity.Physics;
 
 [UpdateInGroup( typeof(FixedStepSimulationSystemGroup) )]
 public class TestSystem : SystemBase
 {
     NativeArray<float3> _positions, _points;
     protected override void OnCreate ()
     {
         _positions = new NativeArray<float3>( new float3[]{ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } , Allocator.Persistent );
         _points = new NativeArray<float3>( new float3[]{ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 } , Allocator.Persistent );
     }
     protected override void OnDestroy ()
     {
         if( _positions.IsCreated ) _positions.Dispose();
         if( _points.IsCreated ) _points.Dispose();
     }
     protected override unsafe void OnUpdate ()
     {
         var positions = _positions;
         var points = _points;
 
         Entities
             .WithReadOnly( positions ).WithNativeDisableParallelForRestriction( positions )
             .WithReadOnly( points ).WithNativeDisableParallelForRestriction( points )
             .WithAll<MyTag>()
             .ForEach( ( in PhysicsCollider collider , in LocalToWorld transform ) =>
             {
                 float3 transformPosition = transform.Position;
                 var colliderInfo = collider.Value.Value;
                 foreach( float3 p0 in positions )
                 foreach( float3 p1 in points )
                 {
                     float3 testPoint = transformPosition + ( p1 - p0 );
                     var pointDistanceInput = new PointDistanceInput{
                         Position    = testPoint ,
                         MaxDistance    = 0 ,
                         Filter        = CollisionFilter.Default
                     };
                     if( colliderInfo.CalculateDistance(pointDistanceInput,out var hit) )
                         UnityEngine.Debug.Log("HIT");
                     else
                         UnityEngine.Debug.Log("MISS");
                 }
             } )
             .WithBurst()
             .ScheduleParallel();
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ben-rasooli · Sep 12, 2021 at 10:47 PM 0
Share

Thanks for your reply. Where in your code, you're moving the collider to a new position?

avatar image andrew-lukasik ben-rasooli · Sep 12, 2021 at 11:17 PM 0
Share

I barely know dots physics but from I can figure out you don't need to move a collider to do these tests at all. Collider position can be a temporary origin point too.

 foreach( float3 p0 in positions )
 foreach( float3 p1 in points )
     float3 testPoint = transformPosition + ( p1 - p0 );
avatar image ben-rasooli andrew-lukasik · Sep 13, 2021 at 12:38 AM 1
Share

Oh, wait! I think I know what you mean. You're actually changing the point's position relative to the collider's position. I'll give it a try. Thanks.

Show more comments
avatar image andrew-lukasik ben-rasooli · Sep 12, 2021 at 11:27 PM 0
Share

Also, if you can convert those points into an AABB then it will be much faster to calculate a distance between 2 bounds (backup link). As an early eli$$anonymous$$ation mechanism if nothing else.

avatar image ben-rasooli andrew-lukasik · Sep 13, 2021 at 12:28 AM 0
Share

Those points can't be AABB. They are individual points.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

206 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity (ECS & DOTS) Entity Sinking 0 Answers

Entity System Collider And Destroy 1 Answer

how to capsule cast from within Entities.ForEach job? ECS 1 Answer

2D 360 degress platformer example needed 0 Answers

Can not add PhysicsCollider at runtime Unity in ECS 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges