Unity未完成的布料模拟

 

这次未完成的原因和上次SPH一样,经验不足,参数不知道应该设置为多少,所以质点到处飞qwq....

using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update

    public GameObject ball;

    private GameObject[] balls = new GameObject[100];
    private const int  width = 3;
    private const int  height = 3;
    private float speedscaler = 0.0001f;

    private float gravity = -9.8f;
    private float restlength = 1.0f;
    private float k1 = .01f;

    private Vector3[] speed = new Vector3[width * height];
    private int[][] Str = new int[width * height][];
    private int[][] Cro = new int[width * height][];
    private int[][] Bend = new int[width * height][];

    private struct aball
    {
        GameObject ball;
    }
    void Start()
    {
        for(int i = 0;i < height;i++)
        {
            for(int j = 0;j < width;j++)
            {
                int myidx = i * width + j;
                balls[myidx] = Instantiate<GameObject>(ball, new Vector3(j-4,i,0), Quaternion.identity);
                speed[myidx] = new Vector3(0.0f,0.0f,0.0f);
                Str[myidx] = new int[4] { -1, -1, -1, -1 };
                Cro[myidx] = new int[4] { -1, -1, -1, -1 };
                Bend[myidx] = new int[4] { -1, -1, -1, -1 };


            }
        }
        // build links
        //link1
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                int myidx = i * width + j;

                if (j > 0)
                    Str[myidx][0] = myidx - 1;
                if (j < width - 1)
                    Str[myidx][1] = myidx + 1;
                if (i > 0)
                    Str[myidx][2] = myidx - width;
                if (i < height - 1)
                    Str[myidx][3] = myidx + width;

                if (j > 1)
                    Cro[myidx][0] = myidx - 2;
                if (j < width - 2)
                    Cro[myidx][1] = myidx + 2;
                if (i > 1)
                    Cro[myidx][2] = myidx - width * 2;
                if (i < height - 2)
                    Cro[myidx][3] = myidx + width * 2;

                if (j > 1 && i > 1)
                    Bend[myidx][0] = myidx - 1 - width;
                if (j < width - 1 && i > 1)
                    Bend[myidx][1] = myidx + 1 - width;
                if (j > 1 && i < height - 1)
                    Bend[myidx][2] = myidx - 1 + width;
                if (j < width - 1 && i < height - 1)
                    Bend[myidx][3] = myidx + 1 + width;

            }
        }
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        for (int i = 0; i < height - 1; i++)
        {
            for (int j = 0; j < width; j++)
            {
                int myidx = i * width + j;
                Vector3 mepos = balls[myidx].GetComponent<Transform>().position;
                Vector3 hispos;
                Vector3 GravityForce = new Vector3(0.0f, -9.8f * speedscaler, 0.0f);
                speed[myidx] += GravityForce;

                for(int k = 0;k < 4;k++)
                {
                    if (Str[myidx][k] != -1)
                    {
                        int hisidx = Str[myidx][k];
                        Debug.Log("myidx = " + myidx + ", hisidx = " + hisidx);
                        Vector3 StrForce = new Vector3();
                        hispos = balls[hisidx].GetComponent<Transform>().position;
                        float exceedlength = Vector3.Distance(mepos, hispos) - restlength;
                        if(exceedlength > 0.001f)
                        {
                            StrForce = Vector3.Normalize(hispos - mepos) * exceedlength * k1;
                        }
                        else if(exceedlength < 0.001f)
                        {
                            StrForce = Vector3.Normalize(mepos - hispos) * exceedlength * k1;
                        }
                        speed[myidx] += StrForce ;
                    }
                    if (Cro[myidx][k] != -1)
                    {
                        int hisidx = Cro[myidx][k];
                        Debug.Log("myidx = " + myidx + ", hisidx = " + hisidx);
                        Vector3 CroForce = new Vector3();
                        hispos = balls[hisidx].GetComponent<Transform>().position;
                        float exceedlength = Vector3.Distance(mepos, hispos) - restlength * 2;
                        if (exceedlength > 0.001f)
                        {
                            CroForce = Vector3.Normalize(hispos - mepos) * exceedlength * k1;
                        }
                        else if (exceedlength < 0.001f)
                        {
                            CroForce = Vector3.Normalize(mepos - hispos) * exceedlength * k1;
                        }
                        speed[myidx] += CroForce;
                    }
                    if (Bend[myidx][k] != -1)
                    {
                        int hisidx = Bend[myidx][k];
                        Debug.Log("myidx = " + myidx + ", hisidx = " + hisidx);
                        Vector3 BendForce = new Vector3();
                        hispos = balls[hisidx].GetComponent<Transform>().position;
                        float exceedlength = Vector3.Distance(mepos, hispos) - restlength * Mathf.Sqrt(2.0f);
                        if (exceedlength > 0.001f)
                        {
                            BendForce = Vector3.Normalize(hispos - mepos) * exceedlength * k1;
                        }
                        else if (exceedlength < 0.001f)
                        {
                            BendForce = Vector3.Normalize(mepos - hispos) * exceedlength * k1;
                        }
                        speed[myidx] += BendForce;
                    }
                }
                Vector3 windForce = new Vector3();
                windForce.x = UnityEngine.Random.Range(0, 0.0004f);
                windForce.y = UnityEngine.Random.Range(0, 0.0004f);
                speed[myidx] += windForce;


                balls[myidx].GetComponent<Transform>().Translate(speed[myidx]);

            }
        }
       
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值