Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Unity.Physics interactions at runtime

Discussion in 'DOTS Dev Blitz Day 2023 - Q&A' started by HBG-Mathieu, Aug 23, 2023.

  1. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Hello,

    First of all, I want to point out some troubles I had with the documentation and give you some context.
    I was searching for a way to add a "physic body" at runtime, and never thought that the setup will be showed under the "interacting with the bodies" section. In fact the whole manual is so focus on the authoring/baking pipeline that it almost feel like it's not designed for runtime stuff at the moment. And there was no examples for dynamic creation too.
    Even most of the authoring tools are not in the package but in the samples, does it mean that we should really dig into the package to make our owns? But the manual is quite light for this I think.

    So by discussing on the forum and on Discord, I come to the conclusion that there was 2 possibilities :
    - Using a prefab but disabling the physic simulation (half dynamic)
    - Or adding/removing the components myself, because the only one complex to setup (PhysicMass) have an utility fonction for it. Even the smoothing components didn't need a special setup.
    Do you have other ways to do so?

    Disabling the physic would, in fact, be more useful to existing simulated entities that I want to temporary remove from the simulation. And that's the main question here : how do it correctly?
    Nothing on the documentation or in the samples, only information sharing on social network. And the popular option is to remove the WorldPhysicsIndex (or change the value to an "empty" world). That's working so far, but the entity is still used by pretty much all physic systems, just not the BuildWorld. Is that really the clean way to do it? Maybe the simulate tag ?

    Thanks !
     
    Last edited: Aug 24, 2023
  2. vertxxyz

    vertxxyz

    Joined:
    Oct 29, 2014
    Posts:
    99
    No, they aim to completely unify the authoring with built-in. Currently that's not complete, so the previous authoring components that have all the features are available in the samples.
     
    HBG-Mathieu likes this.
  3. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    If you use a different PhysicsWorldIndex to disable a rigid body, choosing a non-default one (0), the rigid body will not be used by the engine.
    Where do you see it used still? That might be a bug.
     
  4. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    For many examples of instantiating entity prefabs at runtime or generally creating Unity Physics content at runtime, have a look at the PhysicsSamples project.
     
  5. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    As for the documentation, there is indeed a dedicated section for how to create bodies in code here.
    upload_2023-8-24_12-6-6.png

    Let us know if something specific is missing on that page that we should add or whether there are other ways we could improve this page.

    What is also very important is to read the data components section to get an overview of how rigid bodies and colliders are represented as entities and components.
     
    HBG-Mathieu likes this.
  6. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Oh I didn't see the data components section, it's quite important indeed ! It was just a feedback, but having creation/setup stuff inside a section called interractions did misslead me. Tha doesn't excuse the fact that I shoudl have read the whole manual ! But it's also true that not everyone will have the will to do so.
    More interresting, maybe, I think adding a link to the data components section inside the create body paragraph would be wise. Because, honestly, after I read the page I thought that there was no more information linked to the creation !
    That maybe not the most interresting thread for the dev blitz day though... sorry !
     
    daniel-holz likes this.
  7. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    upload_2023-8-24_22-5-52.png

    That's the relationships of the sphere in the ColliderParade - Basic after I removed the PhysicWorldIndex (the Simulation System is from me). So there is no longer the BuildPhysicWorld unlike the others entities in the scene (which are falling), but there is pretty much every other systems. But maybe it's perfectly fine, at least it works as intended. But that seems weird as a clean solution to temporary "remove" an entity from the physic simulation, if it's still called by pretty much all the physics systems.

    I just wanted to double check, especially because you can run into issues if you think it's going to work with every configurationg from PhyscBody authoring : smoothing components needs to be removed.

    But if it's the way to go, I'm fine with it ^^
     
  8. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Is it supposed to be explicit on this page ? Because that's what I read, but none of the samples even speak about spawning, at least prefabs. Do you have a specific one in mind?
    I hope you don't think I'm agressive/bothering, I honestly didn't find it on this page.

    Thanks for everything anyway !
     
  9. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    Sorry I didn't provide a direct example from the sample project. I'll find one and get back to you!
     
  10. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    The reason for this is that we use an entity query for physics colliders in all these systems to ensure their jobs are cleanly chained and to prevent data races.
    So while the sphere (which still contains a PhysicsCollider component and hence is part of that query) is still shown to have a relationship to these systems, it is not processed by these systems since they all use the built physics world for processing rather than the actual query and the sphere will not show up in the physics world.

    So it's all good.
     
    HBG-Mathieu likes this.
  11. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    Linking the data components page with the creating bodies in code example is a good idea. It will help people understand the code example better.
    Might actually put the example in the same section.
     
    HBG-Mathieu likes this.
  12. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    @HBG-Mathieu : Sorry for the delay. For spawning prefabs with physics, have a look at the Planet Gravity demo.
    The AsteroidSpawnSystem uses as base class the SpawnRandomObjectsSystem which instantiates prefabs here:

    Code (CSharp):
    1. EntityManager.Instantiate(spawnSettings.Prefab, instances);
    This base class is used in other demos as base class for systems that spawn entity prefab instances.
     
    HBG-Mathieu likes this.
  13. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    No need to be sorry ^^
    Thanks for the information ! But I guess I should assume there is none for creation "from scratch". Could be a good candidate for a new sample if the team go the time one day !
    Thanks for your time !
     
  14. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    My pleasure.

    What do you mean with "from scratch".
     
  15. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Like in the "Creating bodies from scratch" section there
    In fact it's saying that it's similar to something in the samples... but that gif... My guess is that it was removed ?
    Maybe also with the smoothing variations.
     
  16. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    There is a gif. Is that the one?
    upload_2023-8-30_12-52-4.png


    Also, the documentation presents both cases, an instantiation from a prefab, creating Entity prefab instances using the Instantiate function, and one that creates Entity rigid bodies from scratch.
    The latter only creates a single sphere and then disables the system, not updating it anymore after the first OnUpdate call. There is no gif for that, but the system is really trivial anyways and there wouldn't be much of interest to see here.

    Are you looking for another code example, maybe how to create an entity prefab "from scratch" in code and then instantiate that entity prefab?
     
  17. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Ah right the gif if for the other section, stupid me.

    I don't need the sample code, I know about it now ^^
    I'm just pointing out that I think it's weird to not have a setup example "from scratch" as simple it is (most samples are anyway). The modify folder show how to interract with the components, maybe they could be created "from scratch".

    I feel like I'm been stubborn on something that bothers only me, sorry ^^
     
  18. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    Not at all. All good.
    You've provided valid feedback. Thank you!
     
    HBG-Mathieu likes this.