Search Unity

Unity Physics Discussion

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

  1. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    137
    FYI, we have a fix for this and I'll post more details asap.
     
    Knightmore likes this.
  2. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    Thanks for the info. Would it solve your problem if bodies in the dynamic tree could opt out of simulation altogether (as opposed to getting skipped over in the simulation loop on account of their having infinite mass)? What shortcomings would you still face in that case?

    Stacking: This is still being worked on but needs some more work. We expect to have these improvements sometime this quarter, but I wouldn't count on it in the next release necessarily.
    Colliders: (see below)
    Activating transform system: Can you clarify what you're seeing? If you have only static bodies nothing should be writing back to the Transform system, but it's possible we missed some permission
    The pool demo is a known issue on our end, which should improve with Unity 2019.3. That said, its performance won't really be optimal until we have more control over the specific threads we simulate on, but no ETA on that quite yet. @steveeHavok will investigate the planet gravity demo. Can you verify how you are measuring this? Is it in a player or only in the Editor?

    The Newton's cradle and abacus? Both of these will return eventually. Or are you missing something else?

    Can you clarify what you're seeing, and which scene you're seeing it in?

    I'll make sure is gets documented better, but the demo for modifying colliders is intended to help unblock people right now. We agree this should not be how users will interact with them ultimately, for the reasons you noted. The current proposal is that we do something like add a checkbox to PhysicsShape that lets users opt into run-time mutability. It would basically ensure safety at the cost of duplicating data, and would do something like add e.g., `PhysicsShapeMaterial` and `SphereShape` to the entity from which the (leaf) shape originates. That said, We have some higher priority items for this quarter, so I wouldn't expect us to get to this until Q4.
     
    Last edited: Jul 23, 2019
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Just Static and dynamic isn't granular enough. You can have bodies that never change, some that change occasionally, and some every frame. Unless you potentially want to be rebuilding large number of colliders every frame that don't need rebuilt, you need a way to create groups of static bodies that can be rebuilt separately.

    Plus, for a range of query only use cases pulling in the transform system doesn't really add value and it actually takes some away. Like a sync point free design when just using worlds and queries only, is rather straight forward.

    Bigger picture I think intentionally preventing custom worlds is short sighted. Worlds are the best/easiest path to using BVH trees for one. They are in some ways already entertwined. And a lot of use cases that really just want a BVH with queries won't want all the higher level flow. They aren't using it as a physics system per say. It would be a shame IMO if the BVH became a thing you couldn't use easily without pulling in the entire physics system.
     
    SamOld likes this.
  4. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    Thanks this is helpful feedback. I think it makes sense for us to ultimately provide a scaffold to make that easier.

    Just to be clear, the intention behind the 0.2.0 access modifier changes was not to prevent custom worlds, nor to indefinitely hide BVH. We only made an informed guess based on feedback from some users taking advantage of low-level functionality about what might be safe to close off for now. We simply wanted to take time to more carefully consider the design of how these things are exposed to users.

    That said, could you please give me a complete list of all types/members that you need public access for again? Basically just embed the package, make all changes to get your project working, and give me a diff or a bulleted list. I think it makes sense to re-add them in 0.2.1 (in e.g., Unity.Physics.LowLevel or similar). When/if we need to adjust how their public interfaces look we can follow a proper deprecation path with the script updater. But this way we can at least get you productive with our latest release again.
     
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Sure not a problem, at a glance it looked liked maybe just a line or two but I'll post the details once I get the time here.
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    500
    How about I just show you what I am seeing. Only difference is the existence of the Physics Shape.
    upload_2019-7-23_20-10-59.png upload_2019-7-23_20-13-7.png

    Admittedly I'm not measuring with the greatest of techniques. The pool demo I just run and drag the sliders with a frame counter. The asteroids demo I type another 9 in the number to spawn, hit play with the profiler hidden but recording, wait for the asteroids to stop hitting the planet and spin around once, then pause and look at the jobs in the frame. I have Burst enabled with synchronous compilation and safety checks disabled. I was mostly doing this to see if there were any surprise optimizations but instead I noticed the narrow phase and simulation took significantly longer and the broadphase took as long as the 9999 in 0.0.2, which in 0.1.0 99999 was still several milliseconds faster.

    Mostly the abacus. That's a really impressive demo. I tried doing similar things in other physics engines and everything jittered into an explosive mess. I get you probably removed the demo because you are redoing some APIs. I don't make these review posts to complain about things missing and regressions. I just make them to note the changes I observe and stir up discussion which seems to lead to good feedback.

    Tests/ContinuousTest
    This caught my eye since continuous rotational collision detection is not easy. So if there is something buried inside the simulation that does this, I would love to know and see if the solution can be applied to higher orders with my root solver. I didn't spend the time to figure out if the test actually did what it is supposed to do or not. It's low on my priority list right now.

    I look forward to watching this develop.

    This is the other major tradeoff I am making with my physics solution. Multiple collision worlds can be built freely at any time by passing in an EntityQuery. I also have a rule where a collision world can only have one collisionBehaviorType (static, kinematic, simulated, continuous, and trigger versions of each). I'm very curious how you are doing this with Unity.Physics since when I tried it I was disappointed with the clunkiness of it.
     
  7. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    I only really solved for the use cases we had, I'm imagining it might actually need some adjustment once we go beyond it. I posted most of the build steps a few posts back. Our entire world abstraction, everything and that includes add/remove as well as some helper methods for set/combine dependencies is under 250 LOC.

    There are clunky parts, although most I would characterize more as unfinished to varying degrees.

    I also think some challenges are not physics specific per say. Like how will they make dependency management automatic for queries in jobs. I don't think you would get any argument from Unity in saying that api's designed for mass consumption need automatic dependency handling. So flows that are impacted by that like queries will be interesting to see what they come up with. Like the Physx batch based approach was a huge pain in my ass. It was actually one of the main reasons I was enticed to try Unity.Physics to start with.
     
  8. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    Thanks for all the details!

    The physics conversion systems (currently) add Translation and Rotation back to things marked with StaticOptimizeEntity, so it results in the static objects matching the transform systems' queries (and hence appearing in the debugger per your screen shots), even though the Transform systems aren't actually doing any work on them (since their chunk versions are not changing). I can trivially add an extra query group for statically optimized entities so we don't have to do this, but in practice all that means is that you would have no Transform system overhead if everything in your scene is marked as statically optimized. You would presumably still get the same base overhead if there is at least one non-static object in your scene.
     
  9. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    8
    Hey, how about putting static objects that never change into a compound collider? Compound colliders have their own hierarchies that will not get rebuilt since none of their parts changed, and are only represented in the broad phase as single objects, so they don't affect the rebuild time a lot. So, in your case, you could have this one compound collider mentioned above and other static objects that change in the broad phase.

    Does that sound feasible? Since most of your objects are not ever changing (if I understood correctly), that should take a big load from the tree building time and get you the performance you want for this.
     
  10. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,947
    Last edited: Jul 24, 2019
  11. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    500
    Where are you seeing this change filter in the TRSToLocalToWorldSystem?
     
  12. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,947
    Just wanted to add that this API is REALLY inconvienient comparing to the default Physics API.

    Constructing new blob, constructing collider, adjusting collider sizes when query changes isn't nice to do each time query is needed in the the code.

    I think single blob collider should be re-used per query.

    Hope to see some improvements in the future.
     
  13. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    I'm assuming a compound collider is associated with a single body? All of our colliders except for the terrain colliders link back to a unique logical entity of some type we need to be able to identify from a query.
     
    SamOld likes this.
  14. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    97
    @snacktime's scenario raises an interesting point. The physics engine is doing a lot more than physics. Actually, most of what I'm using it for is just spacial querying for entities, and the API isn't currently very nice for that.

    The physics BHV is very useful for other querying. Examples include raycast based gun hit detection, raycast based vision checks for the AI, getting all nearby enemy units for the AI to attack, and proximity detectors for lights.

    These things are not well handled by the current API, as they require us to work with physics objects when it would be far more natural to deal with entities. Collision layers are not eloquent for gameplay spacial querying.

    I'll give an example of some logic for my turret API. The bold items require BVH queries. I want to fetch all entities that:
    • Are within a radius (or maybe a forward facing vision cone)
    • Have a
      UnitTag
      component
    • Have a
      UnitTeam
      shared component with a value different to that of the turret
    • Do not have a
      DeadTag
      component
    • Are within line of sight of the turret
    To implement this today, I would need to first fetch all physics bodies within a radius. Optionally I could optimise this a bit by using a collision layer for units, although semantically I find that a little inelegant because it means I have to anticipate this query when I'm setting up my units. After I had fetched all bodies, I would then need to fetch the entity ID for each, manually check each entity to see if it matches my query, and then perform the LoS check with a raycast for each matching entity. That's a lot of low level physics hassle for just getting nearby enemy units to shoot at.

    I'd like to see the spacial query system offer direct support for these types of ECS queries. There are two main components to this; the ability to filter collisions by entity query instead of collision layer, and the ability to get results back as resolved entities and components rather than bodies.

    Working out APIs for this is a little bit tricky, but I might try to put an extension method version together at some point.

    The BVH is obviously useful outside of pure physics, and the API should embrace that and integrate it as much as possible.
     
  15. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    97
    Yes. I think I may have mentioned this before, but we really need convenience methods for
    SphereCast(float3 position, float radius, float3 dir)
    and similar. Needing a collider handy for these things is a pain.
     
  16. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    All of the calls to GetArchetypeChunkComponentType() in OnUpdate() pass true for the isReadOnly parameter.

    Agreed. It's on our list this quarter to provide simpler methods for querying with primitive shapes.

    We expect this to be a common requirement for compounds, so plan to provide a means to do this. Even if it's not ultimately right for your current use case, we'd welcome suggestions of how this could look nice.

    This is another thing we're trying to accomplish for this quarter. The current thinking is to make it look like the fluent query API available to ComponentSystem today. (e.g., possibly something like CollisionWorld.OverlapSphere(position, radius).WithAll<Health>().WithNone<Dead>().ForEach((ref Health health) => health.Value -= damage); as well as probably a manual EntityQuery-based variant).
     
    steveeHavok, xVergilx and SamOld like this.
  17. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    97
    That's fantastic, and in fact the exact API I was envisioning and hoping for. That would be great.

    What's not clear from your example is whether this would allow multiple spacial queries to be combined. For the record, I would like that to be possible. To extend your example, that might look something like this

    Code (CSharp):
    1. CollisionWorld.OverlapSphere(position, radius).IsFirstRayHit(from: position, filter: collisionFilter).WithAll<Health>().WithNone<Dead>().ForEach((ref Health health) => health.Value -= damage);
    The intent here being to allow things like LoS checks to be built into complex spacial queries.

    If this could be built in a manner that was readily extendable with extension methods, that would be fantastic. Bring on APIs that are effectively LINQ to entities and LINQ to spacial queries.
     
    Adam-Mechtley likes this.
  18. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    97
    What will the actual exposed collider API look like? I would assume that we're doing away with unsafe pointers. I proposed an API earlier in this thread that I believe would handle this nicely, and would fit well with this model of having some colliders shared and others owned by the individual entities. I'm curious whether that's the route you're going down, as in my limited experiments I've found that API to be very nice to use.
     
  19. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    Thanks for the feedback! I've documented this on our internal issue tracking requirements for this task.

    Implementation details haven't been investigated in detail yet, but however it shakes I would like it to a) not require an unsafe context and b) be handled in a predictable, idiomatically ECS way.
     
    SamOld likes this.
  20. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    500
    I'm not asking about BuildPhysicsWorld.

    I think you are making the assumption that TRSToLocalToWorldSystem uses change filtering. But TRSToLocalToWorldSystem does not use any change filtering in the latest publicly released version of Entities.
     
  21. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    This is TRSLocalToWorldSystem.OnUpdate() in entities p33, which is required by Unity.Physics. (It also looks the same in our latest master.)

    Code (CSharp):
    1. protected override JobHandle OnUpdate(JobHandle inputDeps)
    2. {
    3.     var rotationType = GetArchetypeChunkComponentType<Rotation>(true);
    4.     var compositeRotationTyoe = GetArchetypeChunkComponentType<CompositeRotation>(true);
    5.     var translationType = GetArchetypeChunkComponentType<Translation>(true);
    6.     var nonUniformScaleType = GetArchetypeChunkComponentType<NonUniformScale>(true);
    7.     var scaleType = GetArchetypeChunkComponentType<Scale>(true);
    8.     var compositeScaleType = GetArchetypeChunkComponentType<CompositeScale>(true);
    9.     var localToWorldType = GetArchetypeChunkComponentType<LocalToWorld>(false);
    10.     var trsToLocalToWorldJob = new TRSToLocalToWorld()
    11.     {
    12.         RotationType = rotationType,
    13.         CompositeRotationType = compositeRotationTyoe,
    14.         TranslationType = translationType,
    15.         ScaleType = scaleType,
    16.         NonUniformScaleType = nonUniformScaleType,
    17.         CompositeScaleType = compositeScaleType,
    18.         LocalToWorldType = localToWorldType
    19.     };
    20.     var trsToLocalToWorldJobHandle = trsToLocalToWorldJob.Schedule(m_Group, inputDeps);
    21.     return trsToLocalToWorldJobHandle;
    22. }
     
  22. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    8
    It is in fact a single body (and single entity), but you could identify different parts of it being hit. You would have the same RigidBodyIndex, but the ColliderKey will be different, and that's what is used for identifying compound collider's individual instances. You can take a look at collider keys documentation. Although it's referring to meshes here, same applies for compound colliders.
     
    Last edited: Jul 25, 2019
  23. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    97
    That's a dead link. That repo doesn't appear to be public.
     
  24. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    137
    SamOld likes this.
  25. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Ya I was wondering about that when you posted. Good to keep in mind. Practically speaking I can't justify moving to that right now, something I might do though the next time we have a reason to refactor what we have.

    Maybe also worth nothing that most of our colliders that never change, that's an optimization. The logical entities they represent do get destroyed. But the context allows for maintaining an ignore index using an int2 position as the key.
     
  26. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    97
    I had no idea all that documentation existed! I've Googled for the physics package docs before, but for some reason Google is only turning up the @0.0 version. Maybe I'm the only one who didn't know, but you should advertise this very clearly online somewhere as it's difficult to find.

    Obviously I should have clicked the link in the package manager though, I'm an idiot.
     
  27. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    8
    Sorry about that, edited the original post with a proper link.
     
    steveeHavok and SamOld like this.
  28. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    500
    This is exactly the code that I am looking at, which is why I am so confused by these statements:
    Me: Where are you seeing the change filter in the TRSToLocalToWorldSystem?
    A) That second statement isn't even true. Look at LocalToWorld.
    B) Just marking the ArchetypeChunkComponentTypes as readonly does not automatically apply change filtering. If it did, that would cause all sorts of problems for systems that require processing all entities regardless of change (such as searching for an entity closest to a point passed into the job.

    Do you see why I don't understand your arguments?
     
  29. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    Sorry I confused the issue, you're right. (I guess I'm not very good at carrying on multiple deferred conversations D: ) I guess my original point was:
    • BuildPhysicsWorld only calls GetArchetypeChunkComponentType() with readonly, so will not bump the chunk versions for static bodies
    • ExportPhysicsWorld only writes back dynamic bodies, so will not bump chunk version for static bodies
    • The first check in TRSLocalToWorld job is:
    Code (CSharp):
    1. bool changed =
    2.     chunk.DidChange(TranslationType, LastSystemVersion) ||
    3.     chunk.DidChange(NonUniformScaleType, LastSystemVersion) ||
    4.     chunk.DidChange(ScaleType, LastSystemVersion) ||
    5.     chunk.DidChange(CompositeScaleType, LastSystemVersion) ||
    6.     chunk.DidChange(RotationType, LastSystemVersion) ||
    7.     chunk.DidChange(CompositeRotationType, LastSystemVersion);
    8. if (!changed)
    9. {
    10.     return;
    11. }
     
    Last edited: Jul 25, 2019
  30. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Actually I did end up thinking of a reason to use compound colliders for our largest group. We already have a system for baking mesh colliders, serializing the raw pointer data and writing it to disk. So we could do the same thing with a single large compound collider which would save all the individual collider creation on scene load.
     
  31. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    500
    This code is not in Entities 33. So I guess the next release of Entities will fix this issue. I look forward to it!
     
    Adam-Mechtley likes this.
  32. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    40
    Hi,I make a repo here:


    Need to do something before that:
    1.Set Fixed Timestep=0.2
    upload_2019-7-26_16-22-19.png
    2.Import my scripts(ConvertPhysicsSystemToFixedUpdate.unitypackage) which to make all physics systems update in FixedUpdate.
    3.Play the UnityPhysicsExamples/Hello World.
     

    Attached Files:

    steveeHavok likes this.
  33. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    40
    Need to replace the floor to plane first.
    upload_2019-7-26_16-48-14.png
     
    steveeHavok likes this.
  34. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,801
    steveeHavok likes this.
  35. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,801
    I haven't investigated physics scripts yet, but my question is, what would be best approach to make multiple fixed joints?
    Something like in classic OOP, where parent is RigidBody, and children are colliders of that RigidBody. So is acting as decomposed complex mesh collider.

    I saw one fixed joint on joints parade, but I see is not really fixed. It has some slack movements, when pushing fast enough.

    My thought would be, to detect collision of children entities, but apply actual collision force only to parent entity.
    Is it feasible with collision events?

    What would be now best alternative approach, to update many children positions, just as fixed joints.

    Edit:
    I just have noticed compound sample. Probably best option for large number of children?
     
    Last edited: Jul 27, 2019
  36. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,801
    Regarding current state of physics, would be my assumption correct, if I say, that collision atm are rather expensive?
    I have noticing significant lag / slowdown, where multiple meshes collide. Current unity physX doing much better.
    But I hope to overtake eventually :)

    Profiler shows spikes, during first collisions, after simulation starts.
    upload_2019-7-27_3-31-9.png
    upload_2019-7-27_3-32-18.png

    Even without collisions, most of demo examples, has quite a bit chunk of CPU taken. 3.47ms as per Entity Debugger SepPhysicsWolrd shows (highlighted).
    upload_2019-7-27_3-36-50.png
    Is that because fixed update time, which syncs? Or something else? I.e. early stage of physics dev.

    What I am missing?
     
  37. Knightmore

    Knightmore

    Joined:
    May 11, 2012
    Posts:
    226
    Awesome. Is it planned for any update of the package or is it possible to solve this with a single file exchange?
     
  38. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    Hey there! Just wanted to clarify:
    1. Given that, do you still need BoundingVolumeHierarchy made public again in the short-term? Or does using compounds for your static group offer a better solution fo you right now?
    2. Can you tell me more about the system for baking mesh colliders and serializing pointers? Is there something that SubScenes are not solving for you?
     
  39. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Compound colliders would I think improve some of what we have, but limit us in some areas also. But mostly right now we have something that works and I just can't justify refactoring it until I have some other good reason to touch that part of the code. I haven't even had the chance to pull in 0.2.0 and confirm what specific changes are necessary. Honestly using the source for 0.2.0 and just making the modifications ourselves is not really a big deal.

    We serialize the Collider unsafe pointer using the binary serialization in the entities package. Our deserialize returns a BlobAssetReference. While our terrain is static, we have two other use cases where we serialize and save colliders to a local cache. So since we already have that might as well use it for terrain colliders. Subscenes would just add another flow but not add any value really.
     
  40. Adam-Mechtley

    Adam-Mechtley

    Unity Technologies

    Joined:
    Feb 5, 2007
    Posts:
    191
    Okay perfect. Please just follow up if you encounter other issues before we re-add it to public API.

    And thanks for the info on how you're de/serializing.
     
  41. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    40
    Which one should i choose between BoxCollider and CapsuleCollider from a performance perspective?
     
  42. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,722
    From memory best to worst performance of collision maths is
    sphere > capsule > box
    With sphere being about 80% faster than capsule and capsule about 10% faster than box.

    Have not looked at this implementation but it'd probably be somewhat safe to assume it'd be similar.
     
  43. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    40
    I have this question because them both contain a ConvexHull(so that boxCollider have the "convexRadius" parameter).
    I guess capsule like "line segment" that maybe faster than box?
     
  44. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Ok so got around to updating to 0.2.0. The quick fix for being able to build custom worlds was to make the following public.

    StaticLayerChangeInfo
    Broadphase
    CollisionWorld.Broadphase

    It seems to me maybe StaticLayerChangeInfo shouldn't even exist. It's lifetime seems to be directly linked to Broadphase, it's only ever used as input to Broadphase building. Unless I'm missing something shouldn't it just be an internal implementation in Broadphase?

    Really BuildPhysicsWorld shouldn't have access to Broadphase directly either it seems. Yes it's the most direct path there but isn't BuildPhysicsWorld correctly just a consumer like any other end user system might be? It's an integration point between physics and ECS. Unless that changes and for some reason internal world logic starts directly interacting with ECS, it seems more granular namespacing might be a good way to make that explicit and force separation of concerns.
     
    SamOld and Adam-Mechtley like this.
  45. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,722
    Is there a new version of Physics coming soon~ with the 0.1 release of entities package? Getting errors now.

    Code (CSharp):
    1. ...\PackageCache\com.unity.entities@0.1.0-preview\Unity.Entities\Blobs.cs(291,13): error: The IL opcode instruction `IL_0001: ldarga` is not supported by burst
    2. at Unity.Entities.BlobAssetReference`1<Unity.Physics.Collider>.op_Inequality(Unity.Entities.BlobAssetReference`1<Unity.Physics.Collider> lhs, Unity.Entities.BlobAssetReference`1<Unity.Physics.Collider> rhs) (at ...\PackageCache\com.unity.entities@0.1.0-preview\Unity.Entities\Blobs.cs:291)
    3. at BovineLabs.Vision.Jobs.CheckObservationsJob.GetAABB(Unity.Physics.Aabb* $___struct_ret, BovineLabs.Vision.Jobs.CheckObservationsJob* this, int i, Unity.Collections.NativeArray`1<Unity.Physics.PhysicsCollider>* colliders, Unity.Collections.NativeArray`1<Unity.Rendering.WorldRenderBounds>* bounds, Unity.Mathematics.float3 position, Unity.Mathematics.quaternion rotation)
    This only appears every 2nd time entering play mode, every other time is fine and I have not tested a build yet either.

    Or is this an Entities issue?

    -edit- made separate post https://forum.unity.com/threads/0-1-entities-burst-exception.719267/ as I'm not sure this is actually a physics issue.
     
    Last edited: Jul 30, 2019
  46. linfuqing

    linfuqing

    Joined:
    May 11, 2015
    Posts:
    40
    Hi,is this a bug and will be fixed?
    this's my temporary solutions:
    Code (CSharp):
    1.     [BurstCompile]
    2.     private struct Rollback : IJobParallelFor
    3.     {
    4.         public uint terrainMask;
    5.  
    6.         [ReadOnly]
    7.         public CollisionWorld collisionWorld;
    8.         [ReadOnly]
    9.         public NativeArray<Unity.Transforms.Translation> translations;
    10.         [ReadOnly]
    11.         public NativeArray<Unity.Transforms.Rotation> rotations;
    12.         [ReadOnly]
    13.         public NativeArray<PhysicsRigidbodyIndex> physicsRigidbodyIndices;
    14.  
    15.         public NativeSlice<MotionData> motionDatas;
    16.  
    17.         public unsafe void Execute(int index)
    18.         {
    19.             int rigidbodyIndex = physicsRigidbodyIndices[index].value;
    20.             var motionData = motionDatas[rigidbodyIndex];
    21.  
    22.             float3 position = math.transform(math.RigidTransform(rotations[index].Value, translations[index].Value), motionData.BodyFromMotion.pos);
    23.  
    24.             RaycastInput raycastInput = default;
    25.             raycastInput.Start = position;
    26.             raycastInput.End = motionData.WorldFromMotion.pos;
    27.             raycastInput.Filter = collisionWorld.Bodies[rigidbodyIndex].Collider->Filter;
    28.             raycastInput.Filter.CollidesWith = terrainMask;
    29.             if (collisionWorld.CastRay(raycastInput))
    30.             {
    31.                 motionData.WorldFromMotion.pos = position;
    32.                 motionDatas[index] = motionData;
    33.             }
    34.         }
    35.     }
    36.  
     
  47. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    So why is StepPhysicsWorld chewing up so much main thread time? I'm seeing a consistent just over 3ms testing with a range of 1 - 1000 bodies. Seems rather large for editor only overhead. Profiler just shows a single child GC.Alloc with 0 cpu time.

    Also, for simple kinematic setups where I just want to move bodies via the Translation and do things like overlap/distance queries, it seems like using dynamic bodies but just disabling StepPhysicsWorld would work?
     
    Antypodish likes this.
  48. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,721
    You can turn off JobsDebugger & Leak detection for a huge speedup in the editor.
     
  49. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Ya disabling those made a big difference in this case, far more then normal I would say.

    Another issue I'm seeing is the DebugStream system is causing massive main thread usage when StepPhysicsWorld is enabled.

    With 1000 dynamic bodies at zero velocity it's using around 17ms main thread average. On startup it jumps into the hundreds or low thousands for a bit. If there is no PhysicsDebugDisplay component in the scene the initial startup frames are actually worse then if it's present with nothing checked.
     
  50. voidwave

    voidwave

    Joined:
    Jul 16, 2012
    Posts:
    5
    when creating a CapsuleCollider or a SphereCollider in code, and setting the center to something other than float3.zero. the object goes through other colliders. is that a bug? or am i missing something?
    the only thing i'm changing is the center, nothing else. the collisionfilters are the same, so is everything else.

    Code (CSharp):
    1.  
    2. //Doesn't Collide
    3. BlobAssetReference<Unity.Physics.Collider> unitCapsuleCollider =
    4.         Unity.Physics.CapsuleCollider.Create(new float3(0, 0.5f, 0), new float3(0, 1f, 0), 0.5f,
    5.         new CollisionFilter()
    6.         {
    7.             BelongsTo = MainConfig.UnitBelongsTo,
    8.             CollidesWith = MainConfig.UnitCollidesWith,
    9.             GroupIndex = 0
    10.         },
    11.         material: new Unity.Physics.Material { Flags = Unity.Physics.Material.MaterialFlags.EnableCollisionEvents });
    12.  
    13.  
    14. //Does Collide
    15. BlobAssetReference<Unity.Physics.Collider> unitCapsuleCollider =
    16.         Unity.Physics.CapsuleCollider.Create(new float3(0, 0, 0), new float3(0, 1f, 0), 0.5f,
    17.         new CollisionFilter()
    18.         {
    19.             BelongsTo = MainConfig.UnitBelongsTo,
    20.             CollidesWith = MainConfig.UnitCollidesWith,
    21.             GroupIndex = 0
    22.         },
    23.         material: new Unity.Physics.Material { Flags = Unity.Physics.Material.MaterialFlags.EnableCollisionEvents });
    24.  
    Edit: collisions with static objects stops working, collisions with dynamic objects seems fine.
     
    Last edited: Aug 1, 2019