Search Unity

Unity Physics Discussion

Discussion in 'Data Oriented Technology Stack' started by smcclelland, Mar 18, 2019.

  1. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,357
    In 0.2.2 MeshCollider.Create never returns locking up Unity and just spinning the cpu on our terrain mesh.. It's a procedural mesh with duplicate vertices, that seems like a likely suspect I would think.
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,357
    So it's hanging in MeshConnectivityBuilder.WeldVertices, although it's not obvious to me at a glance why it would.
     
  3. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    10
    Hi @snacktime, you are right, there is a mistake in the deprecated MeshCollider.Create() - it allocates NativeArrays but doesn't copy the vertices and indices into them (doh!). Will file a bug for that.
    If its hanging in the new (burstable) Create() please send us a dump of the input vertices and indices if posssible so that we can reproduce. Thanks!
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,357

    Ya so it's not really hanging per say it's just the approach doesn't scale well. 190k verts that's 18 billion iterations in that inner loop, minimum 54 billion array indexes there.
     
    Rory_Havok likes this.
  5. Azzara777

    Azzara777

    Joined:
    Mar 10, 2013
    Posts:
    4
    How do you disable gravity? I set it to 0 in the project settings but it's still on.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,393
    You need to be more specific. Which demo you refer to?
     
  7. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    10
    Unity.Physics doesn't use anything from the project settings - those are specific to PhysX.

    Gravity in Unity.Physics defaults to (0,-9.81,0) unless there is a PhysicsStep component found on any entity. That component defines gravity and other simulation settings. For example if you look at the "Hello World" physics sample you will see a "Physics Settings" node in the scene with that component present.
     
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    424
    Well you picked a good weekend to release 0.2.2! Originally I was planning on doing another weekend jam, but the theme/rules ended up being too anti-DOTS_3D for me to be able to finish the only viable option I could think of. So I get to investigate the new updates instead! :D

    First, what hasn't changed which still needs improvement at some point (no surprises here really):
    • Stacking is not a thing yet.
    • Working directly with colliders still requires unsafe API in many cases.
    Side Note: The issue with static colliders invoking the transform system was fixed in the transform system.

    Performance:
    Performance was all over the place but generally better.

    The pool demo got really fast again. After the first couple frames, the spikes stayed under 250 ms compared to 1200 ms I saw earlier. I'm not really sure why that is. Too lazy to find out.

    The asteroid demo with the asteroid count bumped an order of magnitude still has the slightly slower broadphase that I saw in 0.2.0. However, the total simulation job time is way shorter in this version. While the simulation still peaked to 430 ms, it stabilized at 200 ms which wasn't the case in 0.2.0. The big bottleneck now is actually the single-threaded jobs which take nearly half the simulation time.

    Features and API:
    Burst collider generation is awesome. Otherwise there are a few other minor improvements. Nothing I didn't like.

    Internals:
    As I have mentioned before on this forum, I am writing my own physics solution heavily based on Unity.Physics but with different tradeoffs specific to the kinds of games I tend to make. However, I want to keep the low-level math and precision characteristics as similar to Unity.Physics as possible. So thanks to Unity Companion License, I've been deep-diving into the internals of the last missing piece in discrete collision detection which lives in ConvexConvexDistance.cs and ConvexHullBuilder.cs. I'm apparently an idiot because I still can't figure out what the point-simplex solver is doing in terms of machine unit precision compared to Johnson or signed-volume methods. But I definitely noticed the major changes to EPA which seems to be much more robust and also more readable code-wise. That's pretty awesome!

    Overall:
    I like this release. It isn't a huge update, but it also isn't versioned as such.
     
    steveeHavok and Antypodish like this.
  9. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,357
    Here is a quick and dirty implementation with complexity that scales far better. From I had to kill unity after 5+ minutes down to 300ms.

    https://gist.github.com/gamemachine/acfa5d22c770daaaed039d9efe9db23f
     
    Rory_Havok likes this.
  10. WAYN_Group

    WAYN_Group

    Joined:
    Mar 16, 2019
    Posts:
    62
    Hello,

    I updated my project to the latest of every package and faced the following issue:

    When using Physics preview - 0.2.2 with Entites preview - 0.1.1.
    Objects having a PhisicShape authoring component converted to entities through the conversion workflow trigger an error when inspected in the entity debugger and crashes the simulation loop.

    Errro message :
    InvalidCastException: Specified cast is not valid.
    Unity.Properties.Reflection.ReflectedMemberProperty`2[TContainer,TValue].GetValue (TContainer& container) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/Reflection/Properties/ReflectedMemberProperty.cs:24)
    Unity.Properties.PropertyVisitor.VisitProperty[TProperty,TContainer,TValue] (TProperty property, TContainer& container, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:54)
    Unity.Properties.Reflection.ReflectedPropertyBag`1+PropertyProxy`2[TContainer,TProperty,TValue].Accept[TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/Reflection/ReflectedPropertyBag.cs:19)
    Unity.Properties.Reflection.ReflectedPropertyBag`1[TContainer].Accept[TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/Reflection/ReflectedPropertyBag.cs:58)
    Unity.Properties.PropertyContainer.Visit[TContainer,TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyContainerVisit.cs:29)
    Unity.Properties.PropertyVisitor.TryVisitContainerWithAdapters[TProperty,TContainer,TValue] (TProperty property, TContainer& container, TValue& value, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:127)
    Unity.Properties.PropertyVisitor.VisitProperty[TProperty,TContainer,TValue] (TProperty property, TContainer& container, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:57)
    Unity.Properties.Reflection.ReflectedPropertyBag`1+PropertyProxy`2[TContainer,TProperty,TValue].Accept[TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/Reflection/ReflectedPropertyBag.cs:19)
    Unity.Properties.Reflection.ReflectedPropertyBag`1[TContainer].Accept[TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/Reflection/ReflectedPropertyBag.cs:58)
    Unity.Properties.PropertyContainer.Visit[TContainer,TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyContainerVisit.cs:29)
    Unity.Properties.PropertyVisitor.TryVisitContainerWithAdapters[TProperty,TContainer,TValue] (TProperty property, TContainer& container, TValue& value, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:127)
    Unity.Properties.PropertyVisitor.VisitProperty[TProperty,TContainer,TValue] (TProperty property, TContainer& container, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:57)
    Unity.Properties.Reflection.ReflectedPropertyBag`1+PropertyProxy`2[TContainer,TProperty,TValue].Accept[TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/Reflection/ReflectedPropertyBag.cs:19)
    Unity.Properties.Reflection.ReflectedPropertyBag`1[TContainer].Accept[TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/Reflection/ReflectedPropertyBag.cs:58)
    Unity.Properties.PropertyContainer.Visit[TContainer,TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyContainerVisit.cs:29)
    Unity.Properties.PropertyVisitor.TryVisitContainerWithAdapters[TProperty,TContainer,TValue] (TProperty property, TContainer& container, TValue& value, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:127)
    Unity.Properties.PropertyVisitor.VisitProperty[TProperty,TContainer,TValue] (TProperty property, TContainer& container, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:57)
    Unity.Properties.VisitCollectionElementCallback`1[TContainer].VisitProperty[TElementProperty,TElement] (TElementProperty property, TContainer& container, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:17)
    Unity.Entities.EntityContainerPropertyBag+ComponentsProperty+GetComponentDataCallback`1[TCallback].Invoke[TComponent] () (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities.Properties/EntityContainer.cs:65)
    Unity.Properties.PropertyBag`1[TContainer].Cast[TCallback] (TCallback& callback) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/IPropertyBag.cs:38)
    Unity.Entities.EntityContainerPropertyBag+ComponentsProperty.GetPropertyAtIndex[TGetter] (Unity.Entities.EntityContainer& container, System.Int32 index, Unity.Properties.ChangeTracker& changeTracker, TGetter getter) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities.Properties/EntityContainer.cs:227)
    Unity.Properties.PropertyVisitor.TryVisitCollectionWithAdapters[TProperty,TContainer,TValue] (TProperty property, TContainer& container, TValue& value, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:159)
    Unity.Properties.PropertyVisitor.VisitCollectionProperty[TProperty,TContainer,TValue] (TProperty property, TContainer& container, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyVisitor.cs:87)
    Unity.Entities.EntityContainerPropertyBag.Accept[TVisitor] (Unity.Entities.EntityContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities.Properties/EntityContainer.cs:265)
    Unity.Properties.PropertyContainer.Visit[TContainer,TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.ChangeTracker& changeTracker) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyContainerVisit.cs:29)
    Unity.Properties.PropertyContainer.Visit[TContainer,TVisitor] (TContainer& container, TVisitor visitor, Unity.Properties.IVersionStorage versionStorage) (at Library/PackageCache/com.unity.properties@0.6.2-preview/Runtime/Unity.Properties/PropertyContainerVisit.cs:15)
    Unity.Entities.Editor.EntitySelectionProxyEditor.OnInspectorGUI () (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities.Editor/EntityInspector/EntitySelectionProxyEditor.cs:44)
    UnityEditor.UIElements.InspectorElement+<CreateIMGUIInspectorFromEditor>c__AnonStorey1.<>m__0 () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorElement.cs:501)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)

    Reverting to Entites preview - 0.1.0 fix the issue.

    Seem to be linked to the jump from "com.unity.properties" 0.5.0-preview to 0.6.2-preview.
     
  11. WAYN_Group

    WAYN_Group

    Joined:
    Mar 16, 2019
    Posts:
    62
    Hello,

    After updating to Physics preview - 0.2.2, when casting a ray from a job I get the following error :

    InvalidOperationException: The previously scheduled job BoundingVolumeHierarchy:BuildFirstNLevelsJob writes to the NativeArray BuildFirstNLevelsJob.Ranges. You are trying to schedule a new job TargetSelectionSystem:TargetSelectionSystemJob, which reads from the same NativeArray (via TargetSelectionSystemJob.Data.CollisionWorld.Broadphase.m_StaticTree.Ranges). To guarantee safety, you must include BoundingVolumeHierarchy:BuildFirstNLevelsJob as a dependency of the newly scheduled job.
    Unity.Entities.JobForEachExtensions.Schedule (System.Void* fullData, Unity.Collections.NativeArray`1[T] prefilterData, System.Int32 unfilteredLength, System.Int32 innerloopBatchCount, System.Boolean isParallelFor, System.Boolean isFiltered, Unity.Entities.JobForEachExtensions+JobForEachCache& cache, System.Void* deferredCountData, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode) (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/IJobForEach.cs:431)
    Unity.Entities.JobForEachExtensions.ScheduleInternal_CC[T] (T& jobData, Unity.Entities.ComponentSystemBase system, Unity.Entities.EntityQuery query, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode) (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/IJobForEach.gen.cs:1920)
    Unity.Entities.JobForEachExtensions.Schedule[T] (T jobData, Unity.Entities.ComponentSystemBase system, Unity.Jobs.JobHandle dependsOn) (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/IJobForEach.gen.cs:811)
    TargetSelectionSystem.OnUpdate (Unity.Jobs.JobHandle inputDependencies) (at Assets/Scripts/Server/CombatModule/Systems/TargetSelectionSystem.cs:44)
    Unity.Entities.JobComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/ComponentSystem.cs:964)
    Unity.Entities.ComponentSystemBase.Update () (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/ComponentSystem.cs:315)
    Unity.Entities.ComponentSystemGroup.OnUpdate () (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/ComponentSystemGroup.cs:595)
    UnityEngine.Debug:LogException(Exception)
    Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/Stubs/Unity/Debug.cs:25)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/ComponentSystemGroup.cs:599)
    Unity.Entities.ComponentSystem:InternalUpdate() (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/ComponentSystem.cs:831)
    Unity.Entities.ComponentSystemBase:Update() (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/ComponentSystem.cs:315)
    Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.1.0-preview/Unity.Entities/ScriptBehaviourUpdateOrder.cs:144)

    My system is schedule to run after BuildPhysicsWorld
    Code (CSharp):
    1.  
    2.  
    3. [UpdateAfter(typeof(BuildPhysicsWorld))]
    4. public class TargetSelectionSystem : JobComponentSystem
    5. {
    6.  
    7.  
    8.  
    9.     [BurstCompile]
    10.     struct TargetSelectionSystemJob : IJobForEach<Target,PlayerInput>
    11.     {
    12.         [ReadOnly] public CollisionWorld CollisionWorld;
    13.         public void Execute(ref Target target,ref PlayerInput playerInput)
    14.         {
    15.            
    16.  
    17.             RaycastInput input = new RaycastInput()
    18.             {
    19.                 Start = playerInput.cameraPosition,
    20.                 End = playerInput.mouseInput.viewPointRayDirection,
    21.                 Filter = CollisionFilter.Default
    22.             };
    23.             RaycastHit hit;
    24.  
    25.             if (CollisionWorld.CastRay(input, out hit)){
    26.                 target.Entity = CollisionWorld.Bodies[hit.RigidBodyIndex].Entity;
    27.             }
    28.         }
    29.     }
    30.  
    31.     protected override JobHandle OnUpdate(JobHandle inputDependencies)
    32.     {
    33.         var job = new TargetSelectionSystemJob()
    34.         {
    35.             CollisionWorld = World.Active.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld.CollisionWorld
    36.         };
    37.      
    38.  
    39.         return job.Schedule(this, inputDependencies);
    40.     }
    41. }
    42.  
    Tried scheduling in LateSimulationGroup, same result...

    EDIT :

    Adding World.Active.GetOrCreateSystem<BuildPhysicsWorld>().FinalJobHandle.Complete(); before scheduling the job
    and
    job.Complete() just after scheduling it solve the issue for some reason...
     
    Last edited: Sep 8, 2019
  12. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    38
    Thanks for the answer.
    I have a few more questions about that.
    1.I can't find the fixs in version 0.2.2,will it get fixed in the next version?
    2.Can replace the type of parameter "constraints" of SimplexSolver.Solve to NativeSlice<SurfaceConstraintInfo> that we don't need to create temp NativeArray per chunk?
    3.How can I implement a version with the angular speed of CharacterControllerUtilities.CollideAndIntegrate that can support any collider shape?
     
  13. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    7
    1. Unfortunately it didn't make it for this release, but it will be available in the next one.
    2. We need to have a storage for constraints living somewhere, so allocation needs to happen. We could change the interface of the inner methods (like SimplexSolver.Solve) to use NativeSlice, but in the end at the top level it needs to point to some memory (NativeArray). It could be that I misunderstood your question completely, so please tell me if this doesn't make much sense.
    3. It is going to be tricky, since the solver doesn't support angular effects. Solving angular velocities is not nearly as trivial as solving linear velocities (check regular Solver.cs) and would require a new and more complex solver. Other parts (collide and integrate) should be fairly easy, you would just add angular integration and sub integration wherever there is linear movement. But the solver part is probably going to be a deal breaker. May I ask why you need this? Does your character need angular movement other than turning around vertical axis?
     
  14. jacquesalbert

    jacquesalbert

    Joined:
    Jul 9, 2019
    Posts:
    10
    Has anyone determined a good way to use CalculateDistance (or OverlapAabb) to return a list of all hits inside a job? Seems NativeList and Jobs don't agree with each other so I am trying to figure out how to parallelize that part.

    Edit: For anyone else who runs into it, I don't know what the simplest answer is to get a list of hits OUT of a job, but in my case I realized I just needed to use them IN the job, so declaring the list inside the job would do. That works just fine.
     
    Last edited: Sep 12, 2019 at 2:00 AM
  15. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    38
    1.SimplexSolver.Solve2d maybe has the same issue?
    upload_2019-9-10_14-7-31.png
     
  16. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    38
    2.Sorry my English is poor.
    In CharacterControllerBehaviour.cs:
    Code (CSharp):
    1.  
    2.             // Maximum number of hits character controller can store in world queries
    3.             const int maxQueryHits = 128;
    4.             var distanceHits = new NativeArray<DistanceHit>(maxQueryHits, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
    5.             var castHits = new NativeArray<ColliderCastHit>(maxQueryHits, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
    6.             var constraints = new NativeArray<SurfaceConstraintInfo>(4 * maxQueryHits, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
    7.  
    I have a lot of chunks that alloc in job maybe not good for me.
    I want to replace it to:
    Code (CSharp):
    1.  
    2.             int startIndex = chunkIndex << queryHitShilft, length = 1 << queryHitShilft;
    3.             var distanceHits = this.distanceHits.Slice(startIndex, length);
    4.             var castHits = this.castHits.Slice(startIndex, length);
    5.             var constraints = this.constraints.Slice(startIndex << 2, length << 2);
    6.  
    And alloc the array in main thread.
    3.Some time my character is a animal,like this:
    upload_2019-9-10_14-46-14.png
    It's collider is a box:
    upload_2019-9-10_14-47-5.png upload_2019-9-10_15-16-30.png
    And I need it stand in terrain surface:

    Most of time it work well.
    But some time it tunnel through the terrain when turning.
    I think it's due to the CollideAndIntegrate can not support angular well.Is there any other solution?
     
    Last edited: Sep 10, 2019
  17. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    110
    We don't really support asymmetric character shapes at the minute. Can a sphere be used for the collider?
     
  18. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    110
  19. rizu

    rizu

    Joined:
    Oct 8, 2013
    Posts:
    1,154
    @steveeHavok I'm heading that way. I'd love to attend your (?) Havok session but I may need to leave that one bit early as I gotta run for beta event right after it. I wish the schedules weren't this tight (missing lots of interesting DOTS talks now) but it is what it is :)
     
    steveeHavok likes this.
  20. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    7
    I'll try to reply to all 3 questions here.
    1. Yes, I realized it's a more general issue. If you look at these planes, you'll see that they are almost completely (but not entirely) opposing. Then your free axis is almost perfectly vertical, and in order to solve these constraints, you need a really high velocity to get out of that "squeezing" situation. I think we need to apply a more general fix up, even the previous fix didn't actually solve this problem, only reduced it. I'm thinking about introducing a maximum character velocity and clamping whatever comes out from the solver to that value. Thoughts? You can try that on your own and let me know how it works.
    2. I've started an internal discussion about this. We might be able to do something about it. Just to ask - how many characters do you have in your scene? Since this allocation is per chunk, and from what I've seen a chunk can contain quite a lot of characters, you should still have only one allocation if you have few characters (say less than 30).
    3. Yeah, unfortunately we haven't designed for asymmetric characters as @steveeHavok already mentioned. You can try to work around this if you are sure your tunneling issues are coming from turning. When wanting to turn, you can do a query (say CalculateDistance) and make sure new rotation isn't causing new penetrations. If it is, simply create a constraint for it and add it to the list. Or solve it separately using a simplex solver. Do you think this would work?
     
  21. astropuffin

    astropuffin

    Joined:
    May 16, 2015
    Posts:
    6
    I'm running into an issue with physics steps in my unit tests. My code works just fine running in the editor, but not during unit tests and I'm not sure why. Here's a standalone unit test I wrote to check that physics is both triggering AND updating the position of an entity.

    Code (CSharp):
    1. using System.Collections;
    2. using NUnit.Framework;
    3. using Unity.Entities;
    4. using Unity.Entities.Tests;
    5. using Unity.Mathematics;
    6. using Unity.Physics;
    7. using Unity.Physics.Extensions;
    8. using Unity.Physics.Systems;
    9. using Unity.Transforms;
    10. using UnityEngine.TestTools;
    11. using SphereCollider = Unity.Physics.SphereCollider;
    12.  
    13. namespace Tests.UnityScripts {
    14.     public class MotionTest :ECSTestsFixture {
    15.  
    16.         [UnityTest]
    17.         public IEnumerator ShouldUpdateTranslationOnPhysicsUpdate() {
    18.             var e = m_Manager.CreateEntity();
    19.             // Translation
    20.             m_Manager.AddComponentData(e, new Translation{Value = new float3(0,0,0)});
    21.             // Rotation
    22.             m_Manager.AddComponentData(e, new Rotation{Value = quaternion.identity});
    23.             // Collider
    24.             var spCollider = SphereCollider.Create(float3.zero, 1f);
    25.             m_Manager.AddComponentData(e, new PhysicsCollider {Value = spCollider});
    26.             // Add a non-zero linear Velocity
    27.             m_Manager.AddComponent<PhysicsVelocity>(e);
    28.             m_Manager.SetComponentData(e, new PhysicsVelocity{Angular = float3.zero, Linear = new float3(1,1,1)});
    29.             // Mass
    30.             m_Manager.AddComponent<PhysicsMass>(e);
    31.             m_Manager.SetComponentData(e, PhysicsMass.CreateDynamic(MassProperties.UnitSphere, 1f));
    32.  
    33.             var simulation = new Simulation();
    34.             var stepInput = new SimulationStepInput {
    35.                 World = World.Active.GetOrCreateSystem<BuildPhysicsWorld>().PhysicsWorld,
    36.                 TimeStep = 1000f,
    37.                 ThreadCountHint = PhysicsStep.Default.ThreadCountHint,
    38.                 NumSolverIterations = PhysicsStep.Default.SolverIterationCount,
    39.                 Gravity = 10f,
    40.                 SynchronizeCollisionWorld = true
    41.             };
    42.             simulation.Step(stepInput);
    43.  
    44.             Assert.AreNotEqual(float3.zero, e.GetPosition());
    45.             simulation.Dispose();
    46.             yield break;
    47.         }
    48.     }
    49. }
    By attaching the debugger I was able to confirm that the physics update step is indeed being run, and it detects my dynamic body for physics. However, the position isn't changing. I see the same behavior with this simulation code and running Update() directly on the active BuildPhysicsWorld. I feel like I'm misunderstanding something fundamental about how physics updates the component values after calculation.
     
  22. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    10
    Hello @astropuffin,

    In a normal simulation step there are 3 ECS systems for physics:
    * BuildPhysicsWorld - builds the physics world from the entity world.
    * StepPhysicsWorld - steps the physics world. Does nothing to the entity world.
    * ExportPhysicsWorld - writes the new transforms and velocities back to the entity world.

    Your test is missing the last step.

    (It is totally fine to use PhysicsWorld and Simulation directly without using ECS, but in that case you need to populate the world and do something with the simulation results yourself.)

    Also FYI, Gravity is a float3 so you are setting (10,10,10) here, and time step of 1000 seconds is massive :)
     
    florianhanke likes this.
  23. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    39
    I just updated my project from v0.2.0 -> v0.2.2. And I've noticed pressing the play button takes a lot longer to load around ~10-15 seconds (Mississippi style). I also tried doing the same for the UnityPhysicsSamples, and see similar results. Any ideas?
     
  24. astropuffin

    astropuffin

    Joined:
    May 16, 2015
    Posts:
    6
    Thanks for the help, and that makes sense that I'm missing a system somehow. When I attach a debugger, my code is running the ExportPhysicsSystem OnUpdate method. I tried adding the following just in case, but I still don't have updated entities.

    Code (CSharp):
    1. var exportPhysicsWorld = World.GetOrCreateSystem<ExportPhysicsWorld>();
    2. exportPhysicsWorld.Update();
    The issue seems to be that the ExportPhysicsWorld has an empty PhysicsWorld, but I can't seem to figure out how to tell it to use the right one. Maybe a job dependency? I'll keep searching there, but if you happen to have some code off the top of your head that's in the right direction (even if it's not 100% correct) that would be nice. :)

    My goal is to use pure ECS, even though the hybrid approach might be a little easier right now. ;)
     
  25. rizu

    rizu

    Joined:
    Oct 8, 2013
    Posts:
    1,154
    I've noticed a lot longer delay on my project too but didn't connect it to physics package upgrade.
     
  26. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    10
    @hardcodednumber, @rizu - the delay when pressing play is due to the synchronous Burst compile of the collider creation jobs. These are marked synchronous so that the conversion pipeline uses the Bursted versions for much faster collider builds. When the editor caches Burst compiled functions, instead of building everything every time you play, then this should disappear (IIRC that is coming soon in the Burst package, or maybe in newer editor versions).

    @astropuffin - try manually update()'ing the BuildPhysicsWorld system after you have added your components. As I said, all 3 systems must run if you things to be automatic, otherwise you must manually populate the physics world.
     
    thelebaron and rizu like this.
  27. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,393
    @Rory_Havok, I have just noticed now, you are M$ employee. That is some new change? Does Havok physics engine were purchased by M$ as well? Will that affect pricing plan, for Havok physics in Unity? If so, does that will affect positively by any chance?
     
  28. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    10
    Havok has been owned by Microsoft since 2015. Nothing new. I'm just trying using this badge in my profile, not sure if I like it or not TBH :)
     
    Antypodish likes this.
  29. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    667
    Is Havok having the same performance as Physx 4.1?
     
  30. WAYN_Group

    WAYN_Group

    Joined:
    Mar 16, 2019
    Posts:
    62
    Found a way to solve the issue "InvalidOperationException: The previously scheduled job BoundingVolumeHierarchy:BuildFirstNLevelsJob writes to the NativeArray" without relying on the complete() method :

    Code (CSharp):
    1.         var job = new SomeJob().Schedule(this, JobHandle.CombineDependencies(inputDependencies, World.Active.GetOrCreateSystem<BuildPhysicsWorld>().FinalJobHandle));
    2.  
    3.  
    4.         job = JobHandle.CombineDependencies(job, World.Active.GetOrCreateSystem<BuildPhysicsWorld>().FinalJobHandle);
    5.         m_EntityCommandBufferSystem.AddJobHandleForProducer(job);
    6.  
    7.         return job;
    8.     }
    9.  
     
  31. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    39
    @Rory_Havok And the more you know (TM). Thanks for the info.
     
  32. astropuffin

    astropuffin

    Joined:
    May 16, 2015
    Posts:
    6
    Hmm. Yeah that was the first thing I tried before tried the simulation approach and coming here.

    Code (CSharp):
    1. using System.Collections;
    2. using NUnit.Framework;
    3. using Unity.Entities.Tests;
    4. using Unity.Mathematics;
    5. using Unity.Physics;
    6. using Unity.Physics.Extensions;
    7. using Unity.Physics.Systems;
    8. using Unity.Transforms;
    9. using UnityEngine.TestTools;
    10. using SphereCollider = Unity.Physics.SphereCollider;
    11.  
    12. namespace Tests.UnityScripts {
    13.     public class MotionTest :ECSTestsFixture {
    14.  
    15.         [UnityTest]
    16.         public IEnumerator ShouldUpdateTranslationOnPhysicsUpdate() {
    17.             var e = m_Manager.CreateEntity();
    18.             // Translation
    19.             m_Manager.AddComponentData(e, new Translation{Value = new float3(0,0,0)});
    20.             // Rotation
    21.             m_Manager.AddComponentData(e, new Rotation{Value = quaternion.identity});
    22.             // Collider
    23.             var spCollider = SphereCollider.Create(float3.zero, 1f);
    24.             m_Manager.AddComponentData(e, new PhysicsCollider {Value = spCollider});
    25.             // Add a non-zero linear Velocity
    26.             m_Manager.AddComponent<PhysicsVelocity>(e);
    27.             m_Manager.SetComponentData(e, new PhysicsVelocity{Angular = float3.zero, Linear = new float3(1,1,1)});
    28.             // Mass
    29.             m_Manager.AddComponent<PhysicsMass>(e);
    30.             m_Manager.SetComponentData(e, PhysicsMass.CreateDynamic(MassProperties.UnitSphere, 1f));
    31.  
    32.             var buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    33.             buildPhysicsWorld.Update();
    34.  
    35.             Assert.AreNotEqual(float3.zero, e.GetPosition());
    36.             yield break;
    37.         }
    38.     }
    39. }
    I'll keep digging :/
     
  33. Secto_Argonia

    Secto_Argonia

    Joined:
    Aug 5, 2019
    Posts:
    1
    I'm trying to put a player through the spawn system, using about the same prefab as in the example of Joint 4b with LimitDOFJoint. The problem is that if you convert the player directly from the Hierarchy, the Joint entity is created, and if through the SpawnJob, then no. I'm a little stuck here. It is unclear why Joint Entity is not created on instance Instantiate in SpawnJob?

    Code (CSharp):
    1.  
    2. struct SpawnJob : IJobForEachWithEntity<PlayerSpawner, LocalToWorld>
    3.     {
    4.         public EntityCommandBuffer CommandBuffer;
    5.        
    6.         public void Execute(Entity entity, int index,
    7.                             [ReadOnly] ref PlayerSpawner spawner,
    8.                             [ReadOnly] ref LocalToWorld location)
    9.         {
    10.            
    11.             var instance = CommandBuffer.Instantiate(spawner.Prefab);
    12.             var position = math.transform(location.Value, new float3(0, 0, 0));
    13.             CommandBuffer.SetComponent(instance, new Translation { Value = position });
    14.             CommandBuffer.DestroyEntity(entity);
    15.         }
    16.     }
    Code (CSharp):
    1. public class PlayerSpawnerProxy : MonoBehaviour, IDeclareReferencedPrefabs, IConvertGameObjectToEntity
    2. {
    3.     public GameObject Prefab;
    4.  
    5.     public void DeclareReferencedPrefabs(List<GameObject> gameObjects)
    6.     {
    7.        gameObjects.Add(Prefab);
    8.     }
    9.  
    10.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    11.     {
    12.         var spawnerData = new PlayerSpawner
    13.         {
    14.             Prefab = conversionSystem.GetPrimaryEntity(Prefab)
    15.         };
    16.         dstManager.AddComponentData(entity, spawnerData);
    17.        
    18.     }
    19. }
     

    Attached Files:

  34. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,357
    On the terrain colliders, are we supposed to use the scale as an indirect way to control resolution?
     
  35. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    10
    Currently yes, that transforms from the heighmap's integer space to the collider's local space.
    Note the terrain collider is a work in progess, the API may change when we finish it (adding authoring support, multiple material support, holes, etc).
     
  36. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,357
    I was referring to the quad sizes.

    In our case we made some higher level design changes just recently that have us moving from custom meshes to the terrain system. So I have a real use case I've been working through converting.

    First thing was partitioning the terrain in some way had to be a thing. We have runtime height updates. Updating the entire collider on a 2k terrain is untenable. Easy enough to solve I just put in grid based partitioning. But the quad resolution is still an issue. Quite a few terrains including ours have large contiguous areas that are the same height.

    But I'm not sure quad resolution is really the best solution now. With partitioning I can control the partition size, and I think just inserting a single quad for partitions where the height is the same might actually be the more flexible overall solution.

    Lots of partitions doesn't really impact load time if done right. Ie bake out the colliders as raw pointer data. Runtime load data directly into a collider pointer and batch instantiate the entities. With that the difference between 1 and several hundred colliders is negligible. If you create them at runtime from height data, it's a very different story.