这次未完成的原因和上次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]);
}
}
}
}