public void AddSpring(Spring spring) { if (!_springList.Contains(spring)) { _springList.Add(spring); } }
private void ProcessAddedItems() { //Add any new geometries for (int i = 0; i < geomAddList.Count; i++) { if (!geomList.Contains(geomAddList[i])) { geomAddList[i].isRemoved = false; geomList.Add(geomAddList[i]); //Add the new geometry to the broad phase collider. _broadPhaseCollider.Add(geomAddList[i]); } } geomAddList.Clear(); //Add any new bodies for (int i = 0; i < bodyAddList.Count; i++) { if (!bodyList.Contains(bodyAddList[i])) { bodyList.Add(bodyAddList[i]); } } bodyAddList.Clear(); //Add any new controllers for (int i = 0; i < controllerAddList.Count; i++) { if (!controllerList.Contains(controllerAddList[i])) { controllerList.Add(controllerAddList[i]); } } controllerAddList.Clear(); //Add any new joints for (int i = 0; i < jointAddList.Count; i++) { if (!jointList.Contains(jointAddList[i])) { jointList.Add(jointAddList[i]); } } jointAddList.Clear(); //Add any new springs for (int i = 0; i < springAddList.Count; i++) { if (!springList.Contains(springAddList[i])) { springList.Add(springAddList[i]); } } springAddList.Clear(); }
/// <summary> /// Links the bodies. /// </summary> /// <param name="type">The type of Joint to link with.</param> public void LinkBodies(LinkType type, float min, float max, float springConstant, float dampingConstant) { RevoluteJoint revoluteJoint; PinJoint pinJoint, d; SliderJoint sliderJoint; LinearSpring linearSpring, a; Vector2 midPoint; float midDeltaX; float midDeltaY; for (int i = 0; i < _bodies.Count; i++) { if (i < _bodies.Count - 1) { if (_bodies[i].Position.X < _bodies[i + 1].Position.X) { midDeltaX = Math.Abs(_bodies[i].Position.X - _bodies[i + 1].Position.X) * 0.5f; // find x axis midpoint } else { midDeltaX = (_bodies[i + 1].Position.X - _bodies[i].Position.X) * 0.5f; // find x axis midpoint } if (_bodies[i].Position.Y < _bodies[i + 1].Position.Y) { midDeltaY = Math.Abs(_bodies[i].Position.Y - _bodies[i + 1].Position.Y) * 0.5f; // find x axis midpoint } else { midDeltaY = (_bodies[i + 1].Position.Y - _bodies[i].Position.Y) * 0.5f; // find x axis midpoint } midPoint = new Vector2(_bodies[i].Position.X + midDeltaX, _bodies[i].Position.Y + midDeltaY); // set midPoint switch (type) { case LinkType.RevoluteJoint: revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(_bodies[i], _bodies[i + 1], midPoint); revoluteJoint.BiasFactor = 0.2f; revoluteJoint.Softness = 0.01f; _joints.Add(revoluteJoint); break; case LinkType.LinearSpring: linearSpring = SpringFactory.Instance.CreateLinearSpring(_bodies[i], new Vector2(-_width / 2.0f, 0), _bodies[i + 1], new Vector2(_width / 2.0f, 0), springConstant, dampingConstant); if (i >= 1) { a = (LinearSpring)_springs[i - 1]; linearSpring.RestLength = Vector2.Distance(a.AttachPoint2, linearSpring.AttachPoint1); } _springs.Add(linearSpring); break; case LinkType.PinJoint: pinJoint = JointFactory.Instance.CreatePinJoint(_bodies[i], new Vector2(-_width / 2.0f, 0), _bodies[i + 1], new Vector2(_width / 2.0f, 0)); pinJoint.BiasFactor = 0.2f; pinJoint.Softness = 0.01f; if (i >= 1) { d = (PinJoint)_joints[i - 1]; pinJoint.TargetDistance = Vector2.Distance(d.Anchor2, pinJoint.Anchor1); } _joints.Add(pinJoint); break; case LinkType.SliderJoint: sliderJoint = JointFactory.Instance.CreateSliderJoint(_bodies[i], new Vector2(-_width / 2.0f, 0), _bodies[i + 1], new Vector2(_width / 2.0f, 0), min, max); sliderJoint.BiasFactor = 0.2f; sliderJoint.Softness = 0.01f; _joints.Add(sliderJoint); break; default: //should never get here break; } } } if (_loop) { if (_bodies[0].Position.X < _bodies[_bodies.Count - 1].Position.X) { midDeltaX = Math.Abs(_bodies[0].Position.X - _bodies[_bodies.Count - 1].Position.X) * 0.5f; // find x axis midpoint } else { midDeltaX = (_bodies[_bodies.Count - 1].Position.X - _bodies[0].Position.X) * 0.5f; // find x axis midpoint } if (_bodies[0].Position.Y < _bodies[_bodies.Count - 1].Position.Y) { midDeltaY = Math.Abs(_bodies[0].Position.Y - _bodies[_bodies.Count - 1].Position.Y) * 0.5f; // find x axis midpoint } else { midDeltaY = (_bodies[_bodies.Count - 1].Position.Y - _bodies[0].Position.Y) * 0.5f; // find x axis midpoint } midPoint = new Vector2(_bodies[0].Position.X + midDeltaX, _bodies[0].Position.Y + midDeltaY); // set midPoint switch (type) { case LinkType.RevoluteJoint: revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(_bodies[0], _bodies[_bodies.Count - 1], midPoint); revoluteJoint.BiasFactor = 0.2f; revoluteJoint.Softness = 0.01f; _joints.Add(revoluteJoint); break; case LinkType.LinearSpring: linearSpring = SpringFactory.Instance.CreateLinearSpring(_bodies[0], new Vector2(-_width / 2.0f, 0), _bodies[_bodies.Count - 1], new Vector2(_width / 2.0f, 0), springConstant, dampingConstant); linearSpring.RestLength = _width; _springs.Add(linearSpring); break; case LinkType.PinJoint: pinJoint = JointFactory.Instance.CreatePinJoint(_bodies[0], new Vector2(-_width / 2.0f, 0), _bodies[_bodies.Count - 1], new Vector2(_width / 2.0f, 0)); pinJoint.BiasFactor = 0.2f; pinJoint.Softness = 0.01f; pinJoint.TargetDistance = _width; _joints.Add(pinJoint); break; case LinkType.SliderJoint: sliderJoint = JointFactory.Instance.CreateSliderJoint(_bodies[0], new Vector2(-_width / 2.0f, 0), _bodies[_bodies.Count - 1], new Vector2(_width / 2.0f, 0), min, max); sliderJoint.BiasFactor = 0.2f; sliderJoint.Softness = 0.01f; _joints.Add(sliderJoint); break; default: //should never get here break; } } }
/// <summary> /// Processes the added geometries, springs, joints, bodies and controllers. /// </summary> private void ProcessAddedItems() { //Add any new geometries _tempCount = _geomAddList.Count; for (int i = 0; i < _tempCount; i++) { if (!GeomList.Contains(_geomAddList[i])) { _geomAddList[i].InSimulation = true; GeomList.Add(_geomAddList[i]); //Add the new geometry to the broad phase collider. _broadPhaseCollider.Add(_geomAddList[i]); } } _geomAddList.Clear(); //Add any new bodies _tempCount = _bodyAddList.Count; for (int i = 0; i < _tempCount; i++) { if (!BodyList.Contains(_bodyAddList[i])) { BodyList.Add(_bodyAddList[i]); } } _bodyAddList.Clear(); //Add any new controllers _tempCount = _controllerAddList.Count; for (int i = 0; i < _tempCount; i++) { if (!ControllerList.Contains(_controllerAddList[i])) { ControllerList.Add(_controllerAddList[i]); } } _controllerAddList.Clear(); //Add any new joints _tempCount = _jointAddList.Count; for (int i = 0; i < _tempCount; i++) { if (!JointList.Contains(_jointAddList[i])) { JointList.Add(_jointAddList[i]); } } _jointAddList.Clear(); //Add any new springs _tempCount = _springAddList.Count; for (int i = 0; i < _tempCount; i++) { if (!SpringList.Contains(_springAddList[i])) { SpringList.Add(_springAddList[i]); } } _springAddList.Clear(); }