//--------------------------------------------------------------------
 public SimModel(GameModel model, float mass, SimObjectType simObjectType)
     : base(mass, simObjectType)
 {
     this.model = model;
     this.currPosition = model.Translate;
     this.prevPosition = currPosition;
 }
        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]));
            }
        }
Esempio n. 3
0
        private void InitChainScene()
        {
            //chain attributes
            int numSegments = 30;
            float separation = 0.65f;

            //load in segments
            GameModel[] segments = new GameModel[numSegments];
            for (int i = 0; i < numSegments; i++)
            {
                segments[i] = modelComponent.LoadGameModel("chainSegment");

                //reduce the size of each segment
                segments[i].Scale = 0.3f * Vector3.One;

                //position each segment downwards incrementally to form the chain
                segments[i].TranslateY = -i * separation;

                //rotate the even segments by 90 degrees in Y axis
                segments[i].RotateY = (i % 2) * 90.0f;
            }

            //create a chain sim
            float totalMass = 1.0f;
            float stiffness = 0.3f;
            float damping = 0.01f;
            chainSim = new ChainSim(this, segments, totalMass, stiffness, damping);

            //add in a global force generator: gravity
            Gravity gravity = new Gravity(new Vector3(0, -9.81f * 5, 0));
            chainSim.AddGlobalForceGenerator(gravity);

            //constrain the head point so that we can control it
            chainHeadPoint = new PointConstraint(Vector3.Zero, chainSim.SimObjects[0]);
            chainSim.Constraints.Add(chainHeadPoint);

            //create a integrator and assign it to the sim
            float drag = 0.005f;
            Integrator integrator = new VerletNoVelocityIntegrator(this, drag);
            chainSim.Integrator = integrator;
        }