Search Unity

How would you approach creating a very simple custom physics system in Unity?

Discussion in 'General Discussion' started by Rienhl, Aug 29, 2016.

  1. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    41
    I'm teaching my nephew and a friend of him how to make games with Unity.
    They are picking up everything rather fast and next weekend I want to introduce them to Physics Systems.

    I don't want to jump into Unity's Physics right away. Better than that, I believe that getting them to create their VERY simple physics would really provide some understandment about how Physics work in general. I don't want them to be restricted to JUST using Unity's system. Later on, I would show them Unity's Rigidbodies, Colliders, Forces, etc...

    They are highschool seniors, so they feel comfortable with Newtonian Mechanics, Kinematics, etc...

    How would you guys approach the development of a very simple Physics System within Unity which implements:
    • Gravity
    • Simple Collision Detection (with AABB)
    • Velocity + Acceleration
    • Adding Simple Forces
    • Drag
    Any ideas are welcome. Also, if you happen to know about some sample code or a tutorial. I've looked around for a bit and didn't find a suitable on for what I want.

    EDIT: Forgot to mention that I'll be implementing a 2D System!
     
    Last edited: Aug 29, 2016
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,589
    The easiest physics you can implement is particle physics. In this case no rotation is involved, so everything is pretty much trivial and the whole "physical" system will fit in a few lines of code.

    Code (csharp):
    1.  
    2. a = f/m;
    3. v = v + a * deltaT;
    4. pos = pos + vel * deltaT + a * deltaT* deltaT/2;
    5.  
    And that's it. There's nothing else to implement. Do that for every particle and you have physics.

    Thing will get more complicated when you decide to implement constraints or rotational effects.

    If you want to "properly" implement physics, the easy way would be to implement them in 2d, because rotation is easier to handle.

    In 3d, you'll be dealing with inertia tensors and will be better off using a physics engine. (There are chris hecker's papers on rigid body dynamics which you may find interesting:
    http://chrishecker.com/Rigid_body_dynamics )

    However, even with only particle physics you can implement some fun demos in 3d space.

    Those are:
    1. Cloth. (Cloth is particles connected by springs).
    2. "Fake" ragdolls implemented as a particles connected by springs/constraints. You can even make ragdolls this way (use few particles for each limb, derive orientation for limb using math), they'll feel a bit "fake", and won't be "bouncy", but they'll stil be ragdolls. This kind of technique was used inHitman1/Hitman 2 games.
     
    Deleted User and Rienhl like this.
  3. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    41
    Thanks! I forgot to mention that I'm definitely planning on implementing it on 2D!
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,589
    Well, try implementing 3d cloth someday. Those are fun.

    The simplest thing in 2d is something akin to a basic billiard game without rotational effects.... or (since you want forces and gravity) you could make a simple game where player controls ballista/catapult. You could add wind, air ffriction and whatever you want there, plus implement bouncing behavior.

    Both should be fairly easy to implement too. Just make one master MonoBehavior, do calculations and there and directly set object positions in Update() method.
     
    Rienhl likes this.
  5. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    41
    What about the usage of mass? I understand that Unity simulates a "void" when using the Physics System and mass doesn't actual apply (except you're creating a vehicle working with joints, springs, etc...)
    How would you approach that?
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,589
    Never heard anything like that. Unity rigidbodies bodies have mass. Changing their mass makes a difference.

    Mass applies in the sense that F = ma.

    Knowing sum of all forces currently applied to a rigidbody you can calculate body acceleration as a = F/m.
     
    angrypenguin likes this.
  7. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    41
    Ok, it's been some time since I last used Unity's Physics and I might be confused. Somehow I have the idea that I used to change the mass of Rigidbodies and it had no visible effects. Then someone, somewhere, came by with that "weird-void-space" explanation. Anyways, I felt stupid at that time because I didn't get it. Now I just feel stupid because I payed attention to that explanation haha.

    Thanks for your help!
     
  8. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,589
    That doesn't ring any bells for me. It sorta vaguely reminds me of something I heard regarding use of matrices in inverse kinematic, but that probably isn't it.

    Brief online search shows mentions of "void space" being used to describe "space within rigidbody bounding volume that is not occupied by the rigidbody".... but I'm not sure how that could be related to mass not being important.

    So, I have no idea what that "weird void space" explanation could've been.
     
    Rienhl likes this.
  9. schmosef

    schmosef

    Joined:
    Mar 6, 2012
    Posts:
    816
    I've completed this course at Udemy: Game Physics - Extend Unity 3D's Physics Engine in C# Code

    It ticks most of your boxes. It starts with building a custom physics system and gradually works toward using Unity physics directly. However, it is 3D based and it doesn't cover collision yet.

    The instructor is planning on redoing the course in a year or two to add more topics but is working on getting a few other courses finished first. His Unity courses are very popular on Udemy and the Udemy community has built up a wishlist of courses for him to develop.

    I have not taken any HS or Uni physics courses and always felt my weak and informal foundation in this area was hampering my ability to get what I needed out of Unity's physics system.

    This course was very helpful for me. I really liked the pacing and overall style of the instructor. The course filled in some important gaps in my knowledge and I'm getting much more satisfaction our of using physics in Unity now.

    It's a relatively short course as well.

    Edit: The price right now is much higher than I paid for it. It looks like it's currenly $100 CAD. I paid $15 CAD during a sale. I think Udemy has sales fairly often. They also started emailing coupons to me after I created a free account and added courses to my wishlist.
     
    Last edited: Aug 30, 2016