public ChainSim(Game game, GameModel[] ropeSegmentModels, float totalMass, float stiffness, float damping) : base(game) { //create sim data float sphereMass = totalMass / ropeSegmentModels.Length; SimModel[] simObjs = new SimModel[ropeSegmentModels.Length]; for (int i = 0; i < ropeSegmentModels.Length; i++) { simObjs[i] = new SimModel(ropeSegmentModels[i], sphereMass, SimObjectType.ACTIVE); this.AddSimObject(simObjs[i]); } //attach springs between the sim objects for (int i = 1; i < ropeSegmentModels.Length; i++) { this.AddSpring(stiffness, damping, simObjs[i - 1], simObjs[i]); this.Constraints.Add(new LengthConstraint((simObjs[i - 1].Model.Translate - simObjs[i].Model.Translate).Length(), simObjs[i - 1], simObjs[i])); } }
private void InitSpringScene() { //load in a cube and a sphere GameModel stationaryCube = modelComponent.LoadGameModel(@"cube"); stationaryCube.TranslateY = 5; GameModel movingSphere = modelComponent.LoadGameModel(@"sphere"); //create a spring sim springSim = new Simulation(this); //create sim objects from the loaded models float sphereMass = 1.0f; movingSphereSimObj = new SimModel(movingSphere, sphereMass, SimObjectType.ACTIVE); springSim.AddSimObject(movingSphereSimObj); stationaryCubeSimObj = new SimModel(stationaryCube, 1000.0f, SimObjectType.PASSIVE); springSim.AddSimObject(stationaryCubeSimObj); //add in a global force generator: gravity Gravity gravity = new Gravity(new Vector3(0, -9.81f, 0)); springSim.AddGlobalForceGenerator(gravity); //add in a global force generator: air float dragCoefficient = 0.5f; Medium air = new Medium(dragCoefficient); springSim.AddGlobalForceGenerator(air); //attach a spring between the sim objects float stiffness = 8.0f; float damping = 0.1f; springSim.AddSpring(stiffness, damping, stationaryCubeSimObj, movingSphereSimObj); //create an integrator and assign it to the sim ForwardEulerIntegrator integrator = new ForwardEulerIntegrator(this); springSim.Integrator = integrator; //init the line from cube to sphere that represents the spring line3DComponent.StartPosition = stationaryCube.Translate; line3DComponent.EndPosition = movingSphere.Translate; line3DComponent.Color = Color.White; }