Custom cloth physics

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)
xbdev.net 2000 - 2024 (c) - Tutorials - Demos - Software, Game Development, Engineering, Data Mining, File Formats,Programming, Machine Learning

thanks

Come on guys help me with this, I need to achieve the physical like this jacket.

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.

1 Like

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:-(

Change some things, but do not understand how I can achieve

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.

Is true, you’re absolutely right, I’ll try do it myself, if I stuck, ask.

thanks.

First step accomplished :smile:

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

1440607--77310--$Clothz3.gif

1 Like

Looks like a great start to me! Pretty impressed, keep it up!

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.

1444773--77818--$clothColl2.gif.gif

2 Likes

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! :stuck_out_tongue: There’s a nice motivator for ya!

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?

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.

Ya, please post it. I’ll try to look into it.

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

// converted to unity with some modifications - mgear - http://unitycoder.com/blog/
// original source: http://www.xbdev.net/physics/Verlet/index.php

using UnityEngine;
using System.Collections;

    // Some helper containers
    struct Constraint
    {
         public int         index0;
         public int         index1;
         public float restLength;
    };
  
    struct Point
    {
        public Vector3 curPos;
        public Vector3 oldPos;
        public bool    unmovable;
    };

public class Verlet3 : MonoBehaviour {
  
    /*public Transform v1;
    public Transform v2;
    private Vector3 x1;
    private Vector3 x2;*/
  
    public Transform[] bones;
    private Transform[] Ctrls;
    public Transform Obj;

    private Vector3 p2;
    private Vector3 p3;
    private Vector3 p4;
    private Vector3 p5;
    private Vector3 pz;
  
    //Code Snippet for cloth simulation

    // Some defines to determine how our cloth will look
//    private int g_width          = 30;
//    private int  g_height         = 30;
//    private float g_gap                  = 0.5f;
//    private float g_disOffFloor  = 15.0f;
//    private bool g_floorCollisions = false;
    private float gravity = -100.8f;

    private float softness = 0.01135f*0.80f;
  
    public      int               m_numPoints;
    private Point[]           m_points;
    private  int               m_numConstraints;
    private Vector3  m_offset;
    private float    m_scale;  

    //Constraint*  m_constraints;
    private Constraint[]  m_constraints;
  

//    private Mesh mesh;

  
    // Use this for initialization
    void Start () {
      
        Ctrls = Obj.GetComponentsInChildren<Transform>();
      
        //Cloth();
        Create();
  
    }
  
    // Update is called once per frame
    void Update () {
      
        VerletIntegrate(Time.deltaTime);
        SatisfyConstraints();
        Draw();
                  
        pz = Ctrls[1].transform.position;
        p2 = Ctrls[2].transform.position;
        p3 = Ctrls[3].transform.position;
        p4 = Ctrls[4].transform.position;
        p5 = Ctrls[5].transform.position;
       
    }
  
    void Draw()
    {
            UpdateVerts();

             // Draw a wire mesh for our constraints
    }
  



        void VerletIntegrate(float dt)
        {
            for (int i=0; i<m_numPoints; i++)
            {
                if (!m_points[i].unmovable)
                {
                    Vector3 oldPos = m_points[i].oldPos;
                    Vector3 curPos = m_points[i].curPos;
                    Vector3 a = new Vector3(0,gravity,0);

//                    curPos = 2*curPos - oldPos + a*dt*dt;
                    curPos = 1.995f*curPos - 0.995f*oldPos + a*dt*dt;

                    m_points[i].oldPos = m_points[i].curPos;
                    m_points[i].curPos = curPos;
                }
            }
        }
    

    void SatisfyConstraints()
    {
        const int numIterations = 1;

        for (int i=0; i<numIterations; i++)
        {
            for (int k=0; k< m_numConstraints; k++)
            {
                // Constraint 1 (Floor)
                for (int v=0; v<m_numPoints; v++)
                {
                    m_points[0].curPos = pz;
                    m_points[1].curPos = p2;
                    m_points[2].curPos = p3;
                    m_points[3].curPos = p4;
                    m_points[4].curPos = p5;
                  
                  
                    /*if (m_points[7].curPos.y == 0.0f) {
                        m_points[7].curPos.y = 0.2f;
                    }*/
                }

                // Constraint 2 (Links)
                Constraint c = m_constraints[k];
                Vector3 p0 = m_points[c.index0].curPos;
                Vector3 p1 = m_points[c.index1].curPos;
                Vector3 delta = p1-p0;
                float len = delta.magnitude;
//                float diff = (len - c->restLength) / len;
                float diff = (len - c.restLength) / len;
                p0+= delta*softness*diff;
                p1-= delta*softness*diff;

              
                if (p0.y>-110)
                {
                    m_points[c.index0].curPos=p0;
                    m_points[c.index1].curPos=p1;
                }

                if (m_points[c.index0].unmovable)
                {
                    m_points[c.index0].curPos = m_points[c.index0].oldPos;
                }
                if (m_points[c.index1].unmovable)
                {
                    m_points[c.index1].curPos = m_points[c.index1].oldPos;
                }
              

            }
        }

    }
  
    void UpdateVerts()
    {
//        IDirect3DVertexBuffer* vb = m_drawModel.GetVertexBuffer();
//        DrawModel::CUSTOMVERTEX *pVertex    = NULL;
//        vb->Lock(0,0, (void**)&pVertex, 0);
        //Vector3[] bones = mesh.vertices;
        //Transform[] bones = transform.GetComponentsInChildren<Transform>();
        for (int i=0; i<(int)m_numPoints; i++)
        {
//            pVertex[i].x = m_points[i].curPos.x;
//            pVertex[i].y = m_points[i].curPos.y;
//            pVertex[i].z = m_points[i].curPos.z;
            bones[i].transform.position = m_points[i].curPos;
        }
      
        /*mesh.vertices = vertices;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();*/
      
//        vb->Unlock();
    }

    
    void Create()
    {
//        m_modelData = md;
//        m_drawModel.Create(md, pDevice);

        //m_offset = new Vector3(0,12,0);
        //m_scale    = 5f;
      

        // get verts count
        if (bones == null) {
            bones = transform.GetComponentsInChildren<Transform>();
            print ("El array de bones fue creado automaticamente");
        } else {        print ("El array de bones fue creado manualmente");        }
      
      
        m_numPoints = bones.Length;
      
      
      
        m_points = new Point[m_numPoints];

        m_numConstraints = (m_numPoints * m_numPoints) - m_numPoints;
        m_constraints = new Constraint[m_numConstraints];
      
      
      
        for (int i=0; i<m_numPoints; i++)
        {
            //float * data = &md->v[i*3];
            //D3DXVECTOR3* pos = (D3DXVECTOR3*)data;
            Vector3 pos = bones[i].transform.position;
            m_points[i].curPos = pos;
            m_points[i].oldPos = pos;
            m_points[i].unmovable = false;
        }
      
        // create constraints
        int c = 0;
      
        for (int i=0; i<m_numPoints; i++)
        {
            for (int k=0; k<m_numPoints; k++)
            {
                if (i!=k)
                {

                    float len = (m_points[i].curPos - m_points[k].curPos).magnitude;

                    m_constraints[c].restLength = len;
                    m_constraints[c].index0 = i;
                    m_constraints[c].index1 = k;
                    c++;
                }
            }
        }
    }
}

Have you looked at trying Shroud? AFAIK, it’s the only other unity cloth solution out there.

Would be great but I have no cash, i also do not like the idea of it being a plugin.

Heeyyyy some progress?

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.

want you a sample scene or script?, if you need the script is up.

Can you send a Scene for this :smile: :slight_smile: