Exemple #1
0
 /// <summary>
 /// Default constructor, with shape-matching ON.
 /// </summary>
 /// <param name="w">World object to add this body to</param>
 /// <param name="s">ClosedShape for this body</param>
 /// <param name="massPerPoint">mass per PointMass</param>
 /// <param name="gasPressure">amount of gas inside the body</param>
 /// <param name="shapeSpringK">shape-matching spring constant</param>
 /// <param name="shapeSpringDamp">shape-matching spring damping</param>
 /// <param name="edgeSpringK">spring constant for edges</param>
 /// <param name="edgeSpringDamp">spring damping for edges</param>
 /// <param name="pos">global position</param>
 /// <param name="angleInRadians">global angle</param>
 /// <param name="scale">scale</param>
 /// <param name="kinematic">kinematic control boolean</param>
 public PressureBody(World w, ClosedShape s, float massPerPoint, float gasPressure, float shapeSpringK, float shapeSpringDamp, float edgeSpringK, float edgeSpringDamp, Vector2 pos, float angleInRadians, Vector2 scale, bool kinematic)
     : base(w, s, massPerPoint, shapeSpringK, shapeSpringDamp, edgeSpringK, edgeSpringDamp, pos, angleInRadians, scale, kinematic)
 {
     mGasAmount = gasPressure;
     mNormalList = new Vector2[mPointMasses.Count];
     mEdgeLengthList = new float[mPointMasses.Count];
 }
 // Use this for initialization
 void Awake()
 {
     Application.targetFrameRate = 60;
     _world = new JelloPhysics.World();
     CreateFloor();
     _physicsThread = new Thread(UpdatePhysics){Name = "PhysicsThread"};
     _physicsThread.Start();
 }
Exemple #3
0
        /// <summary>
        /// default constructor.
        /// </summary>
        /// <param name="w">world to add this body to (done automatically)</param>
        public Body(World w)
        {
            mAABB = new AABB();
            mBaseShape = null;
            mGlobalShape = null;
            mPointMasses = new List<PointMass>();
            mScale = Vector2.One;
            mIsStatic = false;
            mKinematic = false;

            mMaterial = 0;

            w.addBody(this);
        }
Exemple #4
0
        /// <summary>
        /// Create a Springbody with shape matching set to OFF.
        /// </summary>
        /// <param name="w">World to add this body to.</param>
        /// <param name="shape">ClosedShape shape for this body</param>
        /// <param name="massPerPoint">mass per PointMass.</param>
        /// <param name="edgeSpringK">spring constant for edges.</param>
        /// <param name="edgeSpringDamp">spring damping for edges</param>
        /// <param name="pos">global position of the body</param>
        /// <param name="angleinRadians">global angle of the body</param>
        /// <param name="scale">scale</param>
        /// <param name="kinematic">kinematic control boolean</param>
        public SpringBody(World w, ClosedShape shape, float massPerPoint, float edgeSpringK, float edgeSpringDamp, Vector2 pos, float angleinRadians, Vector2 scale, bool kinematic)
            : base(w, shape, massPerPoint, pos, angleinRadians, scale, kinematic)
        {
            mShapeMatchingOn = false;
            mSprings = new List<InternalSpring>();

            base.setPositionAngle(pos, angleinRadians, scale);

            mEdgeSpringK = edgeSpringK;
            mEdgeSpringDamp = edgeSpringDamp;
            mShapeSpringK = 0.0f;
            mShapeSpringDamp = 0.0f;

            // build default springs.
            _buildDefaultSprings();
        }
        /// <summary>
        /// Create a SpringBody with shape matching turned ON.
        /// </summary>
        /// <param name="w"></param>
        /// <param name="shape">ClosedShape shape for this body</param>
        /// <param name="massPerPoint">mass per PointMass.</param>
        /// <param name="shapeSpringK">shape-matching spring constant</param>
        /// <param name="shapeSpringDamp">shape-matching spring damping</param>
        /// <param name="edgeSpringK">spring constant for edges.</param>
        /// <param name="edgeSpringDamp">spring damping for edges</param>
        /// <param name="pos">global position</param>
        /// <param name="angleinRadians">global angle</param>
        /// <param name="scale">scale</param>
        /// <param name="kinematic">kinematic control boolean</param>
        public void Setup(World w, ClosedShape shape, float massPerPoint, float shapeSpringK, float shapeSpringDamp, float edgeSpringK, float edgeSpringDamp, Vector2 pos, float angleinRadians, Vector2 scale, bool kinematic)
        {
            base.Setup (w, shape, massPerPoint, pos, angleinRadians, scale, kinematic);
            mSprings = new List<InternalSpring>();

            base.setPositionAngle(pos, angleinRadians, scale);

            mShapeMatchingOn = true;
            mShapeSpringK = shapeSpringK;
            mShapeSpringDamp = shapeSpringDamp;
            mEdgeSpringK = edgeSpringK;
            mEdgeSpringDamp = edgeSpringDamp;

            // build default springs.
            _buildDefaultSprings();
        }
        /// <summary>
        /// create a body, and set its shape and position immediately - with individual masses for each PointMass.
        /// </summary>
        /// <param name="w">world to add this body to (done automatically)</param>
        /// <param name="shape">closed shape for this body</param>
        /// <param name="pointMasses">list of masses for each PointMass</param>
        /// <param name="position">global position of the body</param>
        /// <param name="angleInRadians">global angle of the body</param>
        /// <param name="scale">local scale of the body</param>
        /// <param name="kinematic">whether this body is kinematically controlled.</param>
        public void Setup(World w, ClosedShape shape, List<float> pointMasses, Vector2 position, float angleInRadians, Vector2 scale, bool kinematic)
        {
            mAABB = new AABB();
            DerivedPos = position;
            DerivedAngle = angleInRadians;
            mLastAngle = DerivedAngle;
            mScale = scale;
            mMaterial = 0;
            mIsStatic = false;
            mKinematic = kinematic;

            mPointMasses = new List<PointMass>();
            setShape(shape);
            for (int i = 0; i < mPointMasses.Count; i++)
                mPointMasses [i].Mass = pointMasses [i];

            updateAABB(0f, true);

            w.addBody(this);
        }
        /// <summary>
        /// create a body, and set its shape and position immediately
        /// </summary>
        /// <param name="w">world to add this body to (done automatically)</param>
        /// <param name="shape">closed shape for this body</param>
        /// <param name="massPerPoint">mass for each PointMass to be created</param>
        /// <param name="position">global position of the body</param>
        /// <param name="angleInRadians">global angle of the body</param>
        /// <param name="scale">local scale of the body</param>
        /// <param name="kinematic">whether this body is kinematically controlled</param>
        public void Setup(World w, ClosedShape shape, float massPerPoint, Vector2 position, float angleInRadians, Vector2 scale, bool kinematic)
        {
            mAABB = new AABB();
            DerivedPos = position;
            DerivedAngle = angleInRadians;
            mLastAngle = DerivedAngle;
            mScale = scale;
            mMaterial = 0;
            mIsStatic = float.IsPositiveInfinity(massPerPoint);
            mKinematic = kinematic;

            mPointMasses = new List<PointMass>();
            setShape(shape);
            for (int i = 0; i < mPointMasses.Count; i++)
                mPointMasses [i].Mass = massPerPoint;

            updateAABB(0f, true);

            w.addBody(this);
            List<Vector2> points = new List<Vector2>();
            foreach (PointMass m in mPointMasses)
            {
                points.Add(m.Position);
            }
        }
Exemple #8
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            #if XBOX360
            graphics.PreferredBackBufferWidth = this.Window.ClientBounds.Width;
            graphics.PreferredBackBufferHeight = this.Window.ClientBounds.Height;
            graphics.PreferMultiSampling = true;
            #else
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            #endif
            graphics.ApplyChanges();

            screenWidth = graphics.GraphicsDevice.Viewport.Width;
            screenHeight = graphics.GraphicsDevice.Viewport.Height;

            // setup camera
            cameraPos = new Vector3(-2f, -15f, 25f);
            cameraLookVector = Vector3.Forward;

            // basic effect used for drawing all objects, based on vertexcolor.
            lineEffect = new BasicEffect(graphics.GraphicsDevice, null);
            lineEffect.LightingEnabled = false;
            lineEffect.VertexColorEnabled = true;
            lineEffect.Alpha = 1.0f;

            // initialize physics world
            mWorld = new World();

            // lists to keep track of bodies.
            mSpringBodies = new List<DraggableSpringBody>();
            mPressureBodies = new List<DraggablePressureBody>();
            mStaticBodies = new List<Body>();

            // STATIC COLLISION SHAPE
            // all Closed Shape objects are assumed to be a list of lines going from point to point, with the last point
            // connecting back to the first point to close the shape.  This is a simple rectangle to represent the ground for this demo.
            // ClosedShape objects are automatically "centered" when you call finish(), and that center becomes the center when
            // setting the position of the object.
            ClosedShape stat = new ClosedShape();
            stat.begin();
            stat.addVertex(new Vector2(-20f,-1f));
            stat.addVertex(new Vector2(-20f, 1f));
            stat.addVertex(new Vector2(20f, 1f));
            stat.addVertex(new Vector2(20f, -1f));
            stat.finish();

            // creating the body.  Since this is a static body, we can use the base class Body.
            // setting the mass per point to PositiveInfinity makes it immobile / static.
            Body b = new Body(mWorld, stat, float.PositiveInfinity, new Vector2(0f, -19f), 0, Vector2.One, false);
            mStaticBodies.Add(b);

            // this is a more complex body, in the shape of a capital "I", and connected with many internal springs.
            ClosedShape shape = new ClosedShape();
            shape.begin();
            shape.addVertex(new Vector2(-1.5f, 2.0f));
            shape.addVertex(new Vector2(-0.5f, 2.0f));
            shape.addVertex(new Vector2(0.5f, 2.0f));
            shape.addVertex(new Vector2(1.5f, 2.0f));
            shape.addVertex(new Vector2(1.5f, 1.0f));
            shape.addVertex(new Vector2(0.5f, 1.0f));
            shape.addVertex(new Vector2(0.5f, -1.0f));
            shape.addVertex(new Vector2(1.5f, -1.0f));
            shape.addVertex(new Vector2(1.5f, -2.0f));
            shape.addVertex(new Vector2(0.5f, -2.0f));
            shape.addVertex(new Vector2(-0.5f, -2.0f));
            shape.addVertex(new Vector2(-1.5f, -2.0f));
            shape.addVertex(new Vector2(-1.5f, -1.0f));
            shape.addVertex(new Vector2(-0.5f, -1.0f));
            shape.addVertex(new Vector2(-0.5f, 1.0f));
            shape.addVertex(new Vector2(-1.5f, 1.0f));
            shape.finish();

            // draggablespringbody is an inherited version of SpringBody that includes polygons for visualization, and the
            // ability to drag the body around the screen with the cursor.
            for (int x = -8; x <= 8; x += 4)
            {
                DraggableSpringBody body = new DraggableSpringBody(mWorld, shape, 1f, 150.0f, 5.0f, 300.0f, 15.0f, new Vector2(x, 0), 0.0f, Vector2.One);
                body.addInternalSpring(0, 14, 300.0f, 10.0f);
                body.addInternalSpring(1, 14, 300.0f, 10.0f);
                body.addInternalSpring(1, 15, 300.0f, 10.0f);
                body.addInternalSpring(1, 5, 300.0f, 10.0f);
                body.addInternalSpring(2, 14, 300.0f, 10.0f);
                body.addInternalSpring(2, 5, 300.0f, 10.0f);
                body.addInternalSpring(1, 5, 300.0f, 10.0f);
                body.addInternalSpring(14, 5, 300.0f, 10.0f);
                body.addInternalSpring(2, 4, 300.0f, 10.0f);
                body.addInternalSpring(3, 5, 300.0f, 10.0f);
                body.addInternalSpring(14, 6, 300.0f, 10.0f);
                body.addInternalSpring(5, 13, 300.0f, 10.0f);
                body.addInternalSpring(13, 6, 300.0f, 10.0f);
                body.addInternalSpring(12, 10, 300.0f, 10.0f);
                body.addInternalSpring(13, 11, 300.0f, 10.0f);
                body.addInternalSpring(13, 10, 300.0f, 10.0f);
                body.addInternalSpring(13, 9, 300.0f, 10.0f);
                body.addInternalSpring(6, 10, 300.0f, 10.0f);
                body.addInternalSpring(6, 9, 300.0f, 10.0f);
                body.addInternalSpring(6, 8, 300.0f, 10.0f);
                body.addInternalSpring(7, 9, 300.0f, 10.0f);

                // polygons!
                body.addTriangle(0, 15, 1);
                body.addTriangle(1, 15, 14);
                body.addTriangle(1, 14, 5);
                body.addTriangle(1, 5, 2);
                body.addTriangle(2, 5, 4);
                body.addTriangle(2, 4, 3);
                body.addTriangle(14, 13, 6);
                body.addTriangle(14, 6, 5);
                body.addTriangle(12, 11, 10);
                body.addTriangle(12, 10, 13);
                body.addTriangle(13, 10, 9);
                body.addTriangle(13, 9, 6);
                body.addTriangle(6, 9, 8);
                body.addTriangle(6, 8, 7);
                body.finalizeTriangles(Color.SpringGreen, Color.Navy);

                mSpringBodies.Add(body);
            }

            // pressure body. similar to a SpringBody, but with internal pressurized gas to help maintain shape.
            JelloPhysics.ClosedShape ball = new ClosedShape();
            ball.begin();
            for (int i = 0; i < 360; i += 20)
            {
                ball.addVertex(new Vector2((float)Math.Cos(MathHelper.ToRadians((float)-i)), (float)Math.Sin(MathHelper.ToRadians((float)-i))));
            }
            ball.finish();

            // make many of these.
            for (int x = -10; x <= 10; x+=5)
            {
                DraggablePressureBody pb = new DraggablePressureBody(mWorld, ball, 1.0f, 40.0f, 10.0f, 1.0f, 300.0f, 20.0f, new Vector2(x, -12), 0, Vector2.One);
                pb.addTriangle(0, 10, 9);
                pb.addTriangle(0, 9, 1);
                pb.addTriangle(1, 9, 8);
                pb.addTriangle(1, 8, 2);
                pb.addTriangle(2, 8, 7);
                pb.addTriangle(2, 7, 3);
                pb.addTriangle(3, 7, 6);
                pb.addTriangle(3, 6, 4);
                pb.addTriangle(4, 6, 5);
                pb.addTriangle(17, 10, 0);
                pb.addTriangle(17, 11, 10);
                pb.addTriangle(16, 11, 17);
                pb.addTriangle(16, 12, 11);
                pb.addTriangle(15, 12, 16);
                pb.addTriangle(15, 13, 12);
                pb.addTriangle(14, 12, 15);
                pb.addTriangle(14, 13, 12);
                pb.finalizeTriangles((x==-10) ? Color.Teal : Color.Maroon);
                mPressureBodies.Add(pb);
            }

            // default cursor position, etc.
            mCursorPos = new Vector2(cameraPos.X, cameraPos.Y);

            base.Initialize();
        }