Search Unity

What would the source of a Physics Material look like?

Discussion in 'Scripting' started by TechSquid, Jan 21, 2020.

  1. TechSquid

    TechSquid

    Joined:
    Jan 12, 2020
    Posts:
    3
    Hello all,

    I am working on my first Unity project mostly for the purposes of getting better at C# first.

    I have created my own little physics engine and I want to replicate the Physics Material functionality of RidgidBody but I am not sure of how exactly the best way to achieve this is.



    The built-in Physics Materials currently do not accommodate the type of data for my engine I need.

    What I have:
    • `MyPhysicsBody.cs`
    • `MyPhysicsMaterial.cs`

    `MyPhysicsBody.cs` is akin to RigidBody and requires a public `MyPhysicsMaterial` to work.

    My question now is mostly how to supply this material because my initial hunch has one snag.


    MyPhysicsMaterial.cs
    Code (CSharp):
    1. public class MyPhysicsMaterial
    2. {
    3.     public string x;
    4.     public float y;
    5.     public float z;
    6.  
    7.     public MyPhysicsMaterial(string x, float y,  float z)
    8.     {
    9.         this.x = x;
    10.         this.y = y;
    11.         this.z = z;
    12.     }
    13. }
    So this approach seems to make the most sense to me as the developer could then dynamically create a material. However, I am not sure how I can provide a set of in-built materials.

    What I believe is the correct answer may be to create a class for each material and have it inherit the `MyPhysicsMaterial` class, but this seems cumbersome and would lack auto-completion.

    I think I would like to have something like this available:

    Code (CSharp):
    1. MyPhysicsBody.material = MyPhysicsMaterial.Wood()
    So I added that as a method:


    Code (CSharp):
    1.     public MyPhysicsMaterial Aluminum()
    2.     {
    3.         return new MyPhysicsMaterial("Aluminum", y, z);
    4.     }
    That would probably work fine and I do think is maybe my preferred method but this means that the developer wouldn't be able to choose the material from the inspector as far as I can tell?

    Edit:

    I actually just discovered you can not actually have a class field in the inspector, meaning having a class for each material won't work either. So at the moment, I can find no way to implement a system similar to physics materials where I could have a small library of "materials" for the dev to swap in.

    It may be worth noting that at the moment, a "material" in my engine at the current moment is only 3 variables that.

    Any help is majorly appreciated. Thank you.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,527
    In Unity physics materials are assets.

    To have custom assets, you inherit from ScriptableObject.

    Then you could create various assets in your project of type MyPhysicsMaterial (which inherited from ScriptableObject) named Aluminum, Wood, etc.

    You can then drag those onto your MyPhysicsBody via the inspector.

    ...

    Though I wonder why? Is there something lacking in the built in physics engine that you need? Is there no way to develop something to augment the existing physics engine to get it? I can't see how a custom built amateur physics engine is going to perform anywhere as good as the built in one since the built in one is PhysX for 3d or Box2d for 2d... both of which are very robust and well built engines that have been around for quite some time with tons of support.