//Adds a mesh of spring joints in one of 2 configurations //Both softbody style objects, bridges and ropes can be made using this method //very demanding on the cpu public void AddSpringJointGrid(Vector2 pos, int size, int _x, int _y, bool staticTopRow, Texture2D tex, bool isCircle, bool allToAll, float stiff, float rest, float damp, String name = "Spring") { Object[,] objs = new Object[_x, _y]; //SpringJoint[] springs = new SpringJoint[2 * size * (size - 1)]; SpringJoint spring = new SpringJoint(); bool isStatic = staticTopRow; Random ran = new Random((int)DateTime.Now.Ticks * 100); bool bridge; if (_y == 1) { bridge = true; } else { bridge = false; } for (int y = 0; y < _y; y++) { //objs[0, y] = AddBox("Spring" + mSpringJointList.Count + 1, (pos.X + (spring.RestLength + 1f) * x), (pos.Y + (spring.RestLength + 1f) * y), size, size, Color.SeaGreen, 0.3f, 15f,true, true, true); for (int x = 0; x < _x; x++) { if (tex != null) { objs[x, y] = AddBox(name, (pos.X + (tex.Height) * x), (pos.Y + (tex.Height) * y), tex, 0.01f, 3f, isStatic, !isStatic, true); if (isCircle) { objs[x, y].HitBox = new Circle(new Vector2(pos.X, pos.Y), tex.Width / 2, objs[x, y]); } if (isStatic) { objs[x, y].HitBox = null; } } else { if (x == 0 && staticTopRow || x == _x - 1 && staticTopRow) { isStatic = true; } else { isStatic = false; } objs[x, y] = AddBox(name, (pos.X + (size) * x), (pos.Y + (size) * y), size - 1, size - 1, Color.Green * 0.4f, 0.0f, 30f, isStatic, !false, true, false); objs[x, y].SetFriction(0, 0); if (isCircle) { objs[x, y].HitBox = new Circle(new Vector2(pos.X, pos.Y), size / 2, objs[x, y]); } if (x == (int)(_x / 2) || x == (int)(1) || x == (int)(_x - 2)) { if (y == 0) { objs[x, y].Pathable = true; } } } //if (allToAll) objs[x, y].Renderable = false; //objs[x, y] = AddBox(name, (pos.X + (spring.RestLength + 0f) * x), (pos.Y + (spring.RestLength + 0f) * y),texList[2], 0.5f, 10, isStatic, false, true); } // isStatic = false; } int count = 0; if (_x == 1 || _y == 1 || !allToAll) { #region crosshatch joints for (int x = 0; x < _x; x++) { for (int y = 0; y < _y; y++) { if (x != _x - 1) { spring = new SpringJoint(stiff, (objs[x, y].Position - objs[x + 1, y].Position).Length() + rest, damp); spring.BodyA = objs[x, y]; spring.BodyB = objs[x + 1, y]; spring.IsBridge = bridge; mSpringJointList.Add(spring); count++; } if (y != _y - 1) { spring = new SpringJoint(20, (objs[x, y].Position - objs[x, y + 1].Position).Length() + rest, damp); spring.BodyA = objs[x, y]; spring.BodyB = objs[x, y + 1]; spring.IsBridge = bridge; mSpringJointList.Add(spring); count++; } if (y < _y - 1 && x < _x - 1) { spring = new SpringJoint(20, (objs[x, y].Position - objs[x + 1, y + 1].Position).Length() + rest, damp); spring.BodyA = objs[x, y]; spring.BodyB = objs[x + 1, y + 1]; spring.IsBridge = bridge; mSpringJointList.Add(spring); count++; } if (y > 0 && x < _x - 1) { spring = new SpringJoint(20, (objs[x, y].Position - objs[x + 1, y - 1].Position).Length() + rest, damp); spring.BodyA = objs[x, y]; spring.BodyB = objs[x + 1, y - 1]; spring.IsBridge = bridge; mSpringJointList.Add(spring); count++; } } } #endregion } else { #region all to all joints foreach (Object body in objs) { foreach (Object body2 in objs) { if (body == body2) { continue; } spring = new SpringJoint(stiff, (body.Position - body2.Position).Length(), damp); spring.IsBridge = bridge; if ((body.Position - body2.Position) * Vector2.UnitX == Vector2.Zero) // { spring.RestLength = (body.Position - body2.Position).Length() + rest; spring.Stiffness = stiff / 10; } else { spring.Dampen = damp * 50; } spring.BodyA = body; spring.BodyB = body2; mSpringJointList.Add(spring); count++; } } #endregion } }