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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Custom cloth physics

Discussion in 'Scripting' started by MacluMan, Nov 25, 2013.

  1. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    Hello community

    I'm interested in doing a simple system of cloth physics like this, very basic, by vertices as particles or bones.

    I wonder if anyone can help me translate this code I found on xbdev.net for c + + or unityscipt (js)
    http://www.xbdev.net/physics/Verlet/index.php

    thanks
     
  2. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
  3. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Let me explain why you have gotten no help so far in hopes that it will in turn help you.

    -Effort
    Have you tried the cloth simulator in unity? My guess is no. Therefore I assume you put little effort into doing it yourself, a big turn off for people who would know how to do this. Nonetheless the cloth simulator probably wouldn't have worked out in the way you want.

    -Past Skill Level
    Your asking for code to be translated to one of Unity's languages. Therefore, I assume you don't know jack squat about programming; which means "helping you = doing it for you". Another BIG turn off.

    -"It's easy/simple"
    When someone says it's easy but can't do it themselves, people get annoyed and start to think your naive. Especially when it's nothing but easy.

    -Writes four sentences
    If you want our effort, we need yours. Help us help you.

    -Demanding Code Instead of Help
    We work all day long, why do we want to do yours? We are love giving angels though and will help point in the direction needed with some code samples, but your in for a nasty surprise if you want to Ctrl-C/Ctrl-V your way through game development.

    Maybe now you can see why no one has replied to your thread except one grouchy couch potato.
     
    Magiichan likes this.
  4. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    I answer

    -Effort
    yes, I already tried the cloth simulator unity, not achieved, the skinned cloth never worked well, bad experience for me.

    -Past Skill Level
    I do not understand enough, I make my own codes but this is beyond my understanding.

    -"It's easy/simple"
    Apologize, I do not mean that it is easy to do, I mean to many things are not taken into account as cloth collition, only physical simple.
    basic physics,I expressed myself badly.

    -Writes four sentences
    I do not write much because some people get bored from reading and if they see a many letters does not want to read.

    -Demanding Code Instead of Help
    Do not pretend that do for me, just ask a help would be great anything a clue how I can do it.

    I could find this, but I did scrap:-(
    http://unitycoder.com/blog/2012/07/25/verlet-integration-softbody/

    Change some things, but do not understand how I can achieve
     
    Last edited: Dec 2, 2013
  5. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Solid reply! To keep things simple, this is an advanced task here. Something that is going to take trial and error and a lot of time. This is some advanced math alone involved, let alone programming. You have to start it, you have to work on it. If you get stuck in a specific topic we can help, but not one person here is going to put in the needed hours to guide you along.

    Your best bet is to look for some research papers from big name studios, Nvidia, or universities. They are hard to find, but very useful as they will guide you along in theory, you will have to convert logic into code.

    Good Luck.
     
  6. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    Is true, you're absolutely right, I'll try do it myself, if I stuck, ask.

    thanks.
     
  7. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    First step accomplished :D

    I have currently achieved changing some things to code
    you can see that there is still no detection colition, I'm using bones to move each vertex of the mesh.

    I will continue doing tests
     

    Attached Files:

    Last edited: Dec 4, 2013
    Magiichan likes this.
  8. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Looks like a great start to me! Pretty impressed, keep it up!
     
  9. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    Hey i have news

    Already achieve detection of Collition implement, using a "simplest method", radius of spheres.

    I am very happy with the results but still there a problem, if the cloth is going too fast going through the sphera, will try to resolve this.

    When I said "simple" I meant this.

    $clothColl2.gif.gif
     
    landon912 and Magiichan like this.
  10. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Looks nice. Some vertex dampening would be nice though. But looks great so far. If you keep the work up you could sell this on the asset store for some extra pocket change, but only if you keep improving! :p There's a nice motivator for ya!
     
  11. olkeencole

    olkeencole

    Joined:
    Oct 2, 2009
    Posts:
    17
    How far did you get on this? This looks really good. You mentioned that you are using bones to move the vertices. What does the bone structure look like underneath?
     
  12. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    If anyone is interested in the code, just ask me and I'll post.
    Maybe you can solve the problem of high consumption of resources, I think it has too many loops, and for that reason the frames go down.
     
  13. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Ya, please post it. I'll try to look into it.
     
  14. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    sorry for the delay, i could not find the final code (with detection of motor vehicle crash) but this is the beginning

    Please help me achieve this, the fabric is very unstable and too stretched.


    You need a model like this


    Code (CSharp):
    1. // converted to unity with some modifications - mgear - http://unitycoder.com/blog/
    2. // original source: http://www.xbdev.net/physics/Verlet/index.php
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7.     // Some helper containers
    8.     struct Constraint
    9.     {
    10.          public int         index0;
    11.          public int         index1;
    12.          public float restLength;
    13.     };
    14.  
    15.     struct Point
    16.     {
    17.         public Vector3 curPos;
    18.         public Vector3 oldPos;
    19.         public bool    unmovable;
    20.     };
    21.  
    22. public class Verlet3 : MonoBehaviour {
    23.  
    24.     /*public Transform v1;
    25.     public Transform v2;
    26.     private Vector3 x1;
    27.     private Vector3 x2;*/
    28.  
    29.     public Transform[] bones;
    30.     private Transform[] Ctrls;
    31.     public Transform Obj;
    32.  
    33.     private Vector3 p2;
    34.     private Vector3 p3;
    35.     private Vector3 p4;
    36.     private Vector3 p5;
    37.     private Vector3 pz;
    38.  
    39.     //Code Snippet for cloth simulation
    40.  
    41.     // Some defines to determine how our cloth will look
    42. //    private int g_width          = 30;
    43. //    private int  g_height         = 30;
    44. //    private float g_gap                  = 0.5f;
    45. //    private float g_disOffFloor  = 15.0f;
    46. //    private bool g_floorCollisions = false;
    47.     private float gravity = -100.8f;
    48.  
    49.     private float softness = 0.01135f*0.80f;
    50.  
    51.     public      int               m_numPoints;
    52.     private Point[]           m_points;
    53.     private  int               m_numConstraints;
    54.     private Vector3  m_offset;
    55.     private float    m_scale;  
    56.  
    57.     //Constraint*  m_constraints;
    58.     private Constraint[]  m_constraints;
    59.  
    60.  
    61. //    private Mesh mesh;
    62.  
    63.  
    64.     // Use this for initialization
    65.     void Start () {
    66.      
    67.         Ctrls = Obj.GetComponentsInChildren<Transform>();
    68.      
    69.         //Cloth();
    70.         Create();
    71.  
    72.     }
    73.  
    74.     // Update is called once per frame
    75.     void Update () {
    76.      
    77.         VerletIntegrate(Time.deltaTime);
    78.         SatisfyConstraints();
    79.         Draw();
    80.                  
    81.         pz = Ctrls[1].transform.position;
    82.         p2 = Ctrls[2].transform.position;
    83.         p3 = Ctrls[3].transform.position;
    84.         p4 = Ctrls[4].transform.position;
    85.         p5 = Ctrls[5].transform.position;
    86.        
    87.     }
    88.  
    89.     void Draw()
    90.     {
    91.             UpdateVerts();
    92.  
    93.              // Draw a wire mesh for our constraints
    94.     }
    95.  
    96.  
    97.  
    98.  
    99.         void VerletIntegrate(float dt)
    100.         {
    101.             for (int i=0; i<m_numPoints; i++)
    102.             {
    103.                 if (!m_points[i].unmovable)
    104.                 {
    105.                     Vector3 oldPos = m_points[i].oldPos;
    106.                     Vector3 curPos = m_points[i].curPos;
    107.                     Vector3 a = new Vector3(0,gravity,0);
    108.  
    109. //                    curPos = 2*curPos - oldPos + a*dt*dt;
    110.                     curPos = 1.995f*curPos - 0.995f*oldPos + a*dt*dt;
    111.  
    112.                     m_points[i].oldPos = m_points[i].curPos;
    113.                     m_points[i].curPos = curPos;
    114.                 }
    115.             }
    116.         }
    117.    
    118.  
    119.     void SatisfyConstraints()
    120.     {
    121.         const int numIterations = 1;
    122.  
    123.         for (int i=0; i<numIterations; i++)
    124.         {
    125.             for (int k=0; k< m_numConstraints; k++)
    126.             {
    127.                 // Constraint 1 (Floor)
    128.                 for (int v=0; v<m_numPoints; v++)
    129.                 {
    130.                     m_points[0].curPos = pz;
    131.                     m_points[1].curPos = p2;
    132.                     m_points[2].curPos = p3;
    133.                     m_points[3].curPos = p4;
    134.                     m_points[4].curPos = p5;
    135.                  
    136.                  
    137.                     /*if (m_points[7].curPos.y == 0.0f) {
    138.                         m_points[7].curPos.y = 0.2f;
    139.                     }*/
    140.                 }
    141.  
    142.                 // Constraint 2 (Links)
    143.                 Constraint c = m_constraints[k];
    144.                 Vector3 p0 = m_points[c.index0].curPos;
    145.                 Vector3 p1 = m_points[c.index1].curPos;
    146.                 Vector3 delta = p1-p0;
    147.                 float len = delta.magnitude;
    148. //                float diff = (len - c->restLength) / len;
    149.                 float diff = (len - c.restLength) / len;
    150.                 p0+= delta*softness*diff;
    151.                 p1-= delta*softness*diff;
    152.  
    153.              
    154.                 if (p0.y>-110)
    155.                 {
    156.                     m_points[c.index0].curPos=p0;
    157.                     m_points[c.index1].curPos=p1;
    158.                 }
    159.  
    160.                 if (m_points[c.index0].unmovable)
    161.                 {
    162.                     m_points[c.index0].curPos = m_points[c.index0].oldPos;
    163.                 }
    164.                 if (m_points[c.index1].unmovable)
    165.                 {
    166.                     m_points[c.index1].curPos = m_points[c.index1].oldPos;
    167.                 }
    168.              
    169.  
    170.             }
    171.         }
    172.  
    173.     }
    174.  
    175.     void UpdateVerts()
    176.     {
    177. //        IDirect3DVertexBuffer* vb = m_drawModel.GetVertexBuffer();
    178. //        DrawModel::CUSTOMVERTEX *pVertex    = NULL;
    179. //        vb->Lock(0,0, (void**)&pVertex, 0);
    180.         //Vector3[] bones = mesh.vertices;
    181.         //Transform[] bones = transform.GetComponentsInChildren<Transform>();
    182.         for (int i=0; i<(int)m_numPoints; i++)
    183.         {
    184. //            pVertex[i].x = m_points[i].curPos.x;
    185. //            pVertex[i].y = m_points[i].curPos.y;
    186. //            pVertex[i].z = m_points[i].curPos.z;
    187.             bones[i].transform.position = m_points[i].curPos;
    188.         }
    189.      
    190.         /*mesh.vertices = vertices;
    191.         mesh.RecalculateNormals();
    192.         mesh.RecalculateBounds();*/
    193.      
    194. //        vb->Unlock();
    195.     }
    196.  
    197.    
    198.     void Create()
    199.     {
    200. //        m_modelData = md;
    201. //        m_drawModel.Create(md, pDevice);
    202.  
    203.         //m_offset = new Vector3(0,12,0);
    204.         //m_scale    = 5f;
    205.      
    206.  
    207.         // get verts count
    208.         if (bones == null) {
    209.             bones = transform.GetComponentsInChildren<Transform>();
    210.             print ("El array de bones fue creado automaticamente");
    211.         } else {        print ("El array de bones fue creado manualmente");        }
    212.      
    213.      
    214.         m_numPoints = bones.Length;
    215.      
    216.      
    217.      
    218.         m_points = new Point[m_numPoints];
    219.  
    220.         m_numConstraints = (m_numPoints * m_numPoints) - m_numPoints;
    221.         m_constraints = new Constraint[m_numConstraints];
    222.      
    223.      
    224.      
    225.         for (int i=0; i<m_numPoints; i++)
    226.         {
    227.             //float * data = &md->v[i*3];
    228.             //D3DXVECTOR3* pos = (D3DXVECTOR3*)data;
    229.             Vector3 pos = bones[i].transform.position;
    230.             m_points[i].curPos = pos;
    231.             m_points[i].oldPos = pos;
    232.             m_points[i].unmovable = false;
    233.         }
    234.      
    235.         // create constraints
    236.         int c = 0;
    237.      
    238.         for (int i=0; i<m_numPoints; i++)
    239.         {
    240.             for (int k=0; k<m_numPoints; k++)
    241.             {
    242.                 if (i!=k)
    243.                 {
    244.  
    245.                     float len = (m_points[i].curPos - m_points[k].curPos).magnitude;
    246.  
    247.                     m_constraints[c].restLength = len;
    248.                     m_constraints[c].index0 = i;
    249.                     m_constraints[c].index1 = k;
    250.                     c++;
    251.                 }
    252.             }
    253.         }
    254.     }
    255. }
     
  15. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    687
    Have you looked at trying Shroud? AFAIK, it's the only other unity cloth solution out there.
     
  16. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    Would be great but I have no cash, i also do not like the idea of it being a plugin.
     
  17. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    Heeyyyy some progress?
     
  18. JumboWu

    JumboWu

    Joined:
    Nov 25, 2014
    Posts:
    9
    Hi MacluMan,
    Whether you could give us a simple code or that demo? And I am also using bones to move each vertex of mesh.
     
  19. MacluMan

    MacluMan

    Joined:
    Nov 25, 2013
    Posts:
    23
    want you a sample scene or script?, if you need the script is up.
     
  20. metalfinger

    metalfinger

    Joined:
    Jul 14, 2015
    Posts:
    1
    Can you send a Scene for this :D :)