private void CreateFloor()
        {
            // static ground object.
            GameObject ground = new GameObject ();
            ClosedShape groundShape = new ClosedShape ();
            groundShape.begin();
            groundShape.addVertex(new Vector2(-10f, -2f));
            groundShape.addVertex(new Vector2(-10f, 2f));
            groundShape.addVertex(new Vector2(10f, 2f));
            groundShape.addVertex(new Vector2(10f, -2f));
            groundShape.finish();

            // make the body.
            Body groundBody = new Body();
            groundBody.Setup(_world, groundShape, float.PositiveInfinity, new Vector2(0f, -5f), 0f, Vector2.one, false);
        }
Exemplo n.º 2
0
        private void CreateFloor()
        {
            // static ground object.
            GameObject  ground      = new GameObject();
            ClosedShape groundShape = new ClosedShape();

            groundShape.begin();
            groundShape.addVertex(new Vector2(-10f, -2f));
            groundShape.addVertex(new Vector2(-10f, 2f));
            groundShape.addVertex(new Vector2(10f, 2f));
            groundShape.addVertex(new Vector2(10f, -2f));
            groundShape.finish();

            // make the body.
            Body groundBody = new Body();

            groundBody.Setup(_world, groundShape, float.PositiveInfinity, new Vector2(0f, -5f), 0f, Vector2.one, false);
        }
Exemplo n.º 3
0
        private void Start()
        {
            this.shape = GetComponent <Shape> ();

            SpriteRenderer    sprite = gameObject.GetComponent <SpriteRenderer>();
            PolygonCollider2D col    = gameObject.AddComponent <PolygonCollider2D>();

            Vector2[] points = col.points;

            Destroy(col);

            ClosedShape shape = new ClosedShape();

            shape.begin();

            int i = this.shape.Points.Length - 1, l = 0;

            for (; i > l; i -= detail)
            {
                shape.addVertex(this.shape.Points[i]);
            }

            if (i != l)
            {
                shape.addVertex(this.shape.Points[0]);
            }

            shape.finish();

            switch (type)
            {
            case JellyType.Spring:
                AddSpringBody(shape);
                break;

            case JellyType.Pressure:
                AddPressureBody(shape);
                break;
            }

            _spriteTexture = sprite.sprite.texture;
            sprite.enabled = false;
        }
        private void Start()
        {
            this.shape = GetComponent<Shape> ();

            SpriteRenderer sprite = gameObject.GetComponent<SpriteRenderer>();
            PolygonCollider2D col = gameObject.AddComponent<PolygonCollider2D>();

            Vector2[] points = col.points;

            Destroy(col);

            ClosedShape shape = new ClosedShape();

            shape.begin();

            int i = this.shape.Points.Length-1, l = 0;
            for(;i>l;i-=detail)
            {
                shape.addVertex( this.shape.Points[i]);
            }

            if(i!=l)
            {
                shape.addVertex(this.shape.Points[0]);
            }

            shape.finish();

            switch (type)
            {
            case JellyType.Spring:
                AddSpringBody(shape);
                break;
            case JellyType.Pressure:
                    AddPressureBody(shape);
                    break;
            }

            _spriteTexture = sprite.sprite.texture;
            sprite.enabled = false;
        }
Exemplo n.º 5
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();
        }