// Update is called once per frame void Update() { for (int i = 0; i != mBodies.Count; ++i) { DeformableModel body = mBodies[i]; body.ShapeMatch(); body.CalculateParticleVelocities(h); body.PerformRegionDamping(); body.ApplyParticleVelocities(h); for (int j = 0; j != body.mParticles.Count; ++j) { SmParticle particle = body.mParticles[j]; particle.mF += vGravity; if (particle.mX.y < 0.0f) { particle.mF.y -= particle.mX.y; //particle.mF = Vector3.zero; particle.mV = Vector3.zero; particle.mX.y = 0.0f; //Debug.Log("Touch[" + j.ToString() + "]"); } } } }
// Use this for initialization void Start() { InitCells(); ValidateCells(); if (null == mDeformableModel) { mDeformableModel = new DeformableModel(mSpacing); mDeformableModel.W = W;//Random.Range(1, 6); mDeformableModel.Alpha = 0.9f; mDeformableModel.RegionDamping = 0.2f; int width = mCellInfos.GetLength(0); int height = mCellInfos.GetLength(1); int depth = mCellInfos.GetLength(2); for (int x = 0; x != width; ++x) { for (int y = 0; y != height; ++y) { for (int z = 0; z != depth; ++z) { SmCellInfo info = mCellInfos[x, y, z]; if (info.mValid) { mDeformableModel.AddCell(new Point3(x, y, z)); } } } } mDeformableModel.Complete(); /* * float velocityMax = 25.0f; * float velocityHalf = velocityMax / 2.0f; * * * Vector3 randomVelocity = new Vector3(0, * Random.Range(0.5f, 1.0f) * velocityMax - velocityHalf, * 0); * * for (int i = 0; i != mDeformableModel.mParticles.Count; ++i) * { * SmParticle particle = mDeformableModel.mParticles[i]; * particle.mX.y += 5.0f; * particle.mV += (randomVelocity); * } * * * Debug.LogWarning("W[" + mDeformableModel.W.ToString() + "], Width[" + width.ToString() + "], Height[" + height.ToString() + "], Depth[" + depth.ToString() + "], Velocity[" + randomVelocity.ToString() + "]"); */ } }
void OnDrawGizmos() { for (int i = 0; i != mBodies.Count; ++i) { DeformableModel body = mBodies[i]; for (int j = 0; j != body.mParticles.Count; ++j) { SmParticle p = body.mParticles[j]; Gizmos.color = Color.red; Gizmos.DrawSphere(p.mX, 0.1f); } } }
void AddBody() { GameObject goPenguin = GameObject.Find("Penguin"); if (null == goPenguin) { return; } SmBody smBody = goPenguin.GetComponent <SmBody>(); if (null == smBody) { return; } DeformableModel body = new DeformableModel(new Vector3(smBody.Spacing, smBody.Spacing, smBody.Spacing)); //body.W = 1; //body.Alpha = Random.Range(0.1f, 1.0f); //body.RegionDamping = Random.Range(0.1f, 1.0f); body.W = 1; body.Alpha = 0.75f; body.RegionDamping = 0.25f; mBodies.Add(body); //int width = Random.Range(0, 3) + 2; //int height = Random.Range(0, 20) + 2; //int depth = Random.Range(0, 3) + 2; int width = smBody.mCellInfos.GetLength(0); int height = smBody.mCellInfos.GetLength(1); int depth = smBody.mCellInfos.GetLength(2); for (int x = 0; x != width; ++x) { for (int y = 0; y != height; ++y) { for (int z = 0; z != depth; ++z) { SmBody.SmCellInfo info = smBody.mCellInfos[x, y, z]; if (info.mValid) { //body.AddParticle(new Point3(x, y, z)); body.AddCell(new Point3(x, y, z)); } } } } body.Complete(); float velocityMax = 25.0f; float velocityHalf = velocityMax / 2.0f; Vector3 randomVelocity = new Vector3(Random.Range(0.0f, 1.0f) * velocityMax - velocityHalf, Random.Range(0.5f, 1.0f) * velocityMax - velocityHalf, Random.Range(0.0f, 1.0f) * velocityMax - velocityHalf); //Vector3 randomVelocity = new Vector3(0.5f * velocityMax - velocityHalf, // 0.5f * velocityMax - velocityHalf, // 0.5f * velocityMax - velocityHalf); //Vector3 randomVelocity = new Vector3(9.0f, 2.8f, -6.8f); for (int i = 0; i != body.mParticles.Count; ++i) { SmParticle particle = body.mParticles[i]; particle.mX.y += 5.0f; particle.mV += (randomVelocity); } Debug.LogWarning("W[" + body.W.ToString() + "], Width[" + width.ToString() + "], Height[" + height.ToString() + "], Depth[" + depth.ToString() + "], Velocity[" + randomVelocity.ToString() + "]"); }