Example #1
0
        void CreatePlatform()
        {
            // Define the dynamic body.
            var bodyDef = new b2BodyDef();

            bodyDef.type = b2BodyType.b2_staticBody;             //or you could use b2_staticBody

            bodyDef.position.Set(initialLocation.X / Constants.PTM_RATIO, initialLocation.Y / Constants.PTM_RATIO);

            var shape = new b2PolygonShape();

            var num = 4;

            b2Vec2[] vertices =
            {
                new b2Vec2(-102.0f / Constants.PTM_RATIO, -49.5f / Constants.PTM_RATIO),
                new b2Vec2(-113.0f / Constants.PTM_RATIO, -81.5f / Constants.PTM_RATIO),
                new b2Vec2(113.0f / Constants.PTM_RATIO,  -84.5f / Constants.PTM_RATIO),
                new b2Vec2(106.0f / Constants.PTM_RATIO, -47.5f / Constants.PTM_RATIO)
            };

            shape.Set(vertices, num);


            // Define the dynamic body fixture.
            var fixtureDef = new b2FixtureDef();

            fixtureDef.shape       = shape;
            fixtureDef.density     = 1.0f;
            fixtureDef.friction    = 0.3f;
            fixtureDef.restitution = 0.1f;

            CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);

            //CONTINUING TO ADD BODY SHAPE....

            // THIS IS THE Sling base....

            //row 1, col 1
            var num2 = 4;

            b2Vec2[] vertices2 =
            {
                new b2Vec2(41.0f / Constants.PTM_RATIO,  -6.5f / Constants.PTM_RATIO),
                new b2Vec2(35.0f / Constants.PTM_RATIO, -57.5f / Constants.PTM_RATIO),
                new b2Vec2(57.0f / Constants.PTM_RATIO, -65.5f / Constants.PTM_RATIO),
                new b2Vec2(49.0f / Constants.PTM_RATIO, -7.5f / Constants.PTM_RATIO)
            };

            shape.Set(vertices2, num2);
            fixtureDef.shape = shape;
            body.CreateFixture(fixtureDef);
        }
        public void createShapeWithDictionary(PlistDictionary dict, PlistArray shapePoints, b2Body body, CCNode node, LHScene scene, CCPoint scale)
        {
            _shapeID   = dict ["shapeID"].AsInt;
            _shapeName = dict ["name"].AsString;

            int flipx = scale.X < 0 ? -1 : 1;
            int flipy = scale.Y < 0 ? -1 : 1;


            for (int f = 0; f < shapePoints.Count; ++f)
            {
                PlistArray fixPoints = shapePoints [f].AsArray;
                int        count     = fixPoints.Count;
                if (count > 2)
                {
                    b2Vec2[]       verts    = new b2Vec2[count];
                    b2PolygonShape shapeDef = new b2PolygonShape();

                    int i = 0;
                    for (int j = count - 1; j >= 0; --j)
                    {
                        int idx = (flipx < 0 && flipy >= 0) || (flipx >= 0 && flipy < 0) ? count - i - 1 : i;

                        String  pointStr = fixPoints [j].AsString;
                        CCPoint point    = CCPoint.Parse(pointStr);

                        point.X *= scale.X;
                        point.Y *= scale.Y;

                        point.Y = -point.Y;

                        b2Vec2 vec = new b2Vec2(point.X, point.Y);

                        verts[idx] = vec;
                        ++i;
                    }

                    if (LHValidateCentroid(verts, count))
                    {
                        shapeDef.Set(verts, count);

                        b2FixtureDef fixture = new b2FixtureDef();

                        LHSetupb2FixtureWithInfo(fixture, dict);

                        fixture.userData = this;
                        fixture.shape    = shapeDef;
                        body.CreateFixture(fixture);
                    }
                }
            }
        }
Example #3
0
        private void CreateGround()
        {
            // Define the dynamic body.
            var bodyDef = new b2BodyDef();

            bodyDef.type = b2BodyType.b2_staticBody;             //or you could use b2_staticBody

            bodyDef.position.Set(initialLocation.X / Constants.PTM_RATIO, initialLocation.Y / Constants.PTM_RATIO);

            b2PolygonShape shape = new b2PolygonShape();

            int num = 4;

            b2Vec2[] vertices =
            {
                new b2Vec2(-1220.0f / Constants.PTM_RATIO,  54.0f / Constants.PTM_RATIO),
                new b2Vec2(-1220.0f / Constants.PTM_RATIO, -52.0f / Constants.PTM_RATIO),
                new b2Vec2(1019.0f / Constants.PTM_RATIO,  -52.0f / Constants.PTM_RATIO),
                new b2Vec2(1019.0f / Constants.PTM_RATIO, 54.0f / Constants.PTM_RATIO)
            };

            shape.Set(vertices, num);


            // Define the dynamic body fixture.
            var fixtureDef = new b2FixtureDef();

            fixtureDef.shape       = shape;
            fixtureDef.density     = 1.0f;
            fixtureDef.friction    = 1.0f;
            fixtureDef.restitution = 0.1f;

            CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);

            if (!TheLevel.SharedLevel.IS_RETINA)
            {
                //non retina adjustment
                sprite.ScaleX = 1.05f;
            }
            else
            {
                // retina adjustment

                sprite.ScaleX = 2.05f;
                sprite.ScaleY = 2.0f;
            }
        }
Example #4
0
        public CollisionFiltering()
        {
            // Ground body
            {
                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));

                b2FixtureDef sd = new b2FixtureDef();
                sd.shape    = shape;
                sd.friction = 0.3f;

                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            // Small triangle
            b2Vec2[] vertices = new b2Vec2[3];
            vertices[0].Set(-1.0f, 0.0f);
            vertices[1].Set(1.0f, 0.0f);
            vertices[2].Set(0.0f, 2.0f);
            b2PolygonShape polygon = new b2PolygonShape();

            polygon.Set(vertices, 3);

            b2FixtureDef triangleShapeDef = new b2FixtureDef();

            triangleShapeDef.shape   = polygon;
            triangleShapeDef.density = 1.0f;

            triangleShapeDef.filter.groupIndex   = k_smallGroup;
            triangleShapeDef.filter.categoryBits = k_triangleCategory;
            triangleShapeDef.filter.maskBits     = k_triangleMask;

            b2BodyDef triangleBodyDef = new b2BodyDef();

            triangleBodyDef.type = b2BodyType.b2_dynamicBody;
            triangleBodyDef.position.Set(-5.0f, 2.0f);

            b2Body body1 = m_world.CreateBody(triangleBodyDef);

            body1.CreateFixture(triangleShapeDef);

            // Large triangle (recycle definitions)
            vertices[0] *= 2.0f;
            vertices[1] *= 2.0f;
            vertices[2] *= 2.0f;
            polygon.Set(vertices, 3);
            triangleShapeDef.filter.groupIndex = k_largeGroup;
            triangleBodyDef.position.Set(-5.0f, 6.0f);
            triangleBodyDef.fixedRotation = true; // look at me!

            b2Body body2 = m_world.CreateBody(triangleBodyDef);

            body2.CreateFixture(triangleShapeDef);

            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-5.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape p = new b2PolygonShape();
                p.SetAsBox(0.5f, 1.0f);
                body.CreateFixture(p, 1.0f);

                b2PrismaticJointDef jd = new b2PrismaticJointDef();
                jd.BodyA       = body2;
                jd.BodyB       = body;
                jd.enableLimit = true;
                jd.localAnchorA.Set(0.0f, 4.0f);
                jd.localAnchorB.SetZero();
                jd.localAxisA.Set(0.0f, 1.0f);
                jd.lowerTranslation = -1.0f;
                jd.upperTranslation = 1.0f;

                m_world.CreateJoint(jd);
            }

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);
            b2FixtureDef boxShapeDef = new b2FixtureDef();

            boxShapeDef.shape       = polygon;
            boxShapeDef.density     = 1.0f;
            boxShapeDef.restitution = 0.1f;

            boxShapeDef.filter.groupIndex   = k_smallGroup;
            boxShapeDef.filter.categoryBits = k_boxCategory;
            boxShapeDef.filter.maskBits     = k_boxMask;

            b2BodyDef boxBodyDef = new b2BodyDef();

            boxBodyDef.type = b2BodyType.b2_dynamicBody;
            boxBodyDef.position.Set(0.0f, 2.0f);

            b2Body body3 = m_world.CreateBody(boxBodyDef);

            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxShapeDef.filter.groupIndex = k_largeGroup;
            boxBodyDef.position.Set(0.0f, 6.0f);

            b2Body body4 = m_world.CreateBody(boxBodyDef);

            body4.CreateFixture(boxShapeDef);

            // Small circle
            b2CircleShape circle = new b2CircleShape();

            circle.Radius = 1.0f;

            b2FixtureDef circleShapeDef = new b2FixtureDef();

            circleShapeDef.shape   = circle;
            circleShapeDef.density = 1.0f;

            circleShapeDef.filter.groupIndex   = k_smallGroup;
            circleShapeDef.filter.categoryBits = k_circleCategory;
            circleShapeDef.filter.maskBits     = k_circleMask;

            b2BodyDef circleBodyDef = new b2BodyDef();

            circleBodyDef.type = b2BodyType.b2_dynamicBody;
            circleBodyDef.position.Set(5.0f, 2.0f);

            b2Body body5 = m_world.CreateBody(circleBodyDef);

            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle.Radius = circle.Radius * 2.0f;
            circleShapeDef.filter.groupIndex = k_largeGroup;
            circleBodyDef.position.Set(5.0f, 6.0f);

            b2Body body6 = m_world.CreateBody(circleBodyDef);

            body6.CreateFixture(circleShapeDef);
        }
Example #5
0
        void CreateEnemy()
        {
            // Define the dynamic body.
            var bodyDef = b2BodyDef.Create();

            bodyDef.type = b2BodyType.b2_dynamicBody;             //or you could use b2_staticBody

            bodyDef.fixedRotation = isRotationFixed;

            bodyDef.position.Set(initialLocation.X / Constants.PTM_RATIO, initialLocation.Y / Constants.PTM_RATIO);

            b2PolygonShape shape       = new b2PolygonShape();
            b2CircleShape  shapeCircle = new b2CircleShape();

            if (shapeCreationMethod == CreationMethod.DiameterOfImageForCircle)
            {
                var   tempSprite     = new CCSprite(spriteImageName);
                float radiusInMeters = (tempSprite.ContentSize.Width / Constants.PTM_RATIO) * 0.5f;

                shapeCircle.Radius = radiusInMeters;
            }


            else if (shapeCreationMethod == CreationMethod.ShapeOfSourceImage)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                      //top left corner
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                     //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                      //bottom right corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)                        //top right corner
                };
                shape.Set(vertices, num);
            }
            else if (shapeCreationMethod == CreationMethod.ShapeOfSourceImageButSlightlySmaller)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -2) * .8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) * .8f / Constants.PTM_RATIO),                  //top left corner
                    new b2Vec2((tempSprite.ContentSize.Width / -2) * .8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) * .8f / Constants.PTM_RATIO),                 //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) * .8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) * .8f / Constants.PTM_RATIO),                  //bottom right corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) * .8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) * .8f / Constants.PTM_RATIO)                    //top right corner
                };
                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Triangle)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 3;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO), //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),  //bottom right corner
                    new b2Vec2(0.0f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)                                  // top center of image
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.TriangleRightAngle)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 3;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                        //top right corner
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                       //top left corner
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO)                       //bottom left corner
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Trapezoid)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                         //top of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                        //top of image, 1/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                       //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                        //bottom right corner
                };

                shape.Set(vertices, num);
            }


            else if (shapeCreationMethod == CreationMethod.Hexagon)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 6;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),  //top of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                 // left, center
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO), //bottom of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),  //bottom of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                  // right, center
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)    //top of image, 3/4's across
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Pentagon)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 5;
                b2Vec2[] vertices =
                {
                    new b2Vec2(0 / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                                    //top of image, center
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                 // left, center
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO), //bottom of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),  //bottom of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                  // right, center
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Octagon)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 8;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -6) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                        //use the source image octogonShape.png for reference
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / -6) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 6) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / -6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / 6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 6) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)
                };

                shape.Set(vertices, num);
            }
            else if (shapeCreationMethod == CreationMethod.Parallelogram)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                      //top of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                     //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                      //bottom of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)                        //top right corner
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.CustomCoordinates1)                 //use your own custom coordinates from a program like Vertex Helper Pro

            {
                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2(-64.0f / Constants.PTM_RATIO,  16.0f / Constants.PTM_RATIO),
                    new b2Vec2(-64.0f / Constants.PTM_RATIO, -16.0f / Constants.PTM_RATIO),
                    new b2Vec2(64.0f / Constants.PTM_RATIO,  -16.0f / Constants.PTM_RATIO),
                    new b2Vec2(64.0f / Constants.PTM_RATIO, 16.0f / Constants.PTM_RATIO)
                };
                shape.Set(vertices, num);
            }

            // Define the dynamic body fixture.
            var fixtureDef = b2FixtureDef.Create();

            if (shapeCreationMethod == CreationMethod.DiameterOfImageForCircle)
            {
                fixtureDef.shape = shapeCircle;
            }
            else
            {
                fixtureDef.shape = shape;
            }

            fixtureDef.density     = theDensity;
            fixtureDef.friction    = 0.3f;
            fixtureDef.restitution = 0.1f;              //how bouncy basically

            CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);


            var blinkInterval = Cocos2D.CCRandom.Next(3, 8); // range 3 to 8

            Schedule(Blink, blinkInterval);                  //comment this out if you never want to show the blink
        }
Example #6
0
        public Cantilever()
        {
            b2Body ground = null;

            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 20.0f;

                b2WeldJointDef jd = new b2WeldJointDef();

                b2Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-14.5f + 1.0f * i, 5.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(-15.0f + 1.0f * i, 5.0f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(1.0f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 20.0f;

                b2WeldJointDef jd = new b2WeldJointDef();
                jd.frequencyHz  = 5.0f;
                jd.dampingRatio = 0.7f;

                b2Body prevBody = ground;
                for (int i = 0; i < 3; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-14.0f + 2.0f * i, 15.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(-15.0f + 2.0f * i, 15.0f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 20.0f;

                b2WeldJointDef jd = new b2WeldJointDef();

                b2Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-4.5f + 1.0f * i, 5.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    if (i > 0)
                    {
                        b2Vec2 anchor = new b2Vec2(-5.0f + 1.0f * i, 5.0f);
                        jd.Initialize(prevBody, body, anchor);
                        m_world.CreateJoint(jd);
                    }

                    prevBody = body;
                }
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 20.0f;

                b2WeldJointDef jd = new b2WeldJointDef();
                jd.frequencyHz  = 8.0f;
                jd.dampingRatio = 0.7f;

                b2Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(5.5f + 1.0f * i, 10.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    if (i > 0)
                    {
                        b2Vec2 anchor = new b2Vec2(5.0f + 1.0f * i, 10.0f);
                        jd.Initialize(prevBody, body, anchor);
                        m_world.CreateJoint(jd);
                    }

                    prevBody = body;
                }
            }

            for (int i = 0; i < 2; ++i)
            {
                b2Vec2[] vertices = new b2Vec2[3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);

                b2PolygonShape shape = new b2PolygonShape();
                shape.Set(vertices, 3);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 1.0f;

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-8.0f + 8.0f * i, 12.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }

            for (int i = 0; i < 2; ++i)
            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.5f;

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 1.0f;

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-6.0f + 6.0f * i, 10.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
Example #7
0
        public CharacterCollision()
        {
            // Ground body
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Collinear edges with no adjacency information.
            // This shows the problematic case where a box shape can hit
            // an internal vertex.
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-8.0f, 1.0f), new b2Vec2(-6.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.Set(new b2Vec2(-6.0f, 1.0f), new b2Vec2(-4.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.Set(new b2Vec2(-4.0f, 1.0f), new b2Vec2(-2.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Chain shape
            {
                b2BodyDef bd = new b2BodyDef();
                bd.angle = 0.25f * b2Settings.b2_pi;
                b2Body ground = m_world.CreateBody(bd);

                b2Vec2[] vs = new b2Vec2[4];
                vs[0].Set(5.0f, 7.0f);
                vs[1].Set(6.0f, 8.0f);
                vs[2].Set(7.0f, 8.0f);
                vs[3].Set(8.0f, 7.0f);
                b2ChainShape shape = new b2ChainShape();
                shape.CreateChain(vs, 4);
                ground.CreateFixture(shape, 0.0f);
            }

            // Square tiles. This shows that adjacency shapes may
            // have non-smooth collision. There is no solution
            // to this problem.
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(1.0f, 1.0f, new b2Vec2(4.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsBox(1.0f, 1.0f, new b2Vec2(6.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsBox(1.0f, 1.0f, new b2Vec2(8.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
            }

            // Square made from an edge loop. Collision should be smooth.
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);

                b2Vec2[] vs = new b2Vec2[4];
                vs[0].Set(-1.0f, 3.0f);
                vs[1].Set(1.0f, 3.0f);
                vs[2].Set(1.0f, 5.0f);
                vs[3].Set(-1.0f, 5.0f);
                b2ChainShape shape = new b2ChainShape();
                shape.CreateLoop(vs, 4);
                ground.CreateFixture(shape, 0.0f);
            }

            // Edge loop. Collision should be smooth.
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(-10.0f, 4.0f);
                b2Body ground = m_world.CreateBody(bd);

                b2Vec2[] vs = new b2Vec2[10];
                vs[0].Set(0.0f, 0.0f);
                vs[1].Set(6.0f, 0.0f);
                vs[2].Set(6.0f, 2.0f);
                vs[3].Set(4.0f, 1.0f);
                vs[4].Set(2.0f, 2.0f);
                vs[5].Set(0.0f, 2.0f);
                vs[6].Set(-2.0f, 2.0f);
                vs[7].Set(-4.0f, 3.0f);
                vs[8].Set(-6.0f, 2.0f);
                vs[9].Set(-6.0f, 0.0f);
                b2ChainShape shape = new b2ChainShape();
                shape.CreateLoop(vs, 10);
                ground.CreateFixture(shape, 0.0f);
            }

            // Square character 1
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(-3.0f, 8.0f);
                bd.type          = b2BodyType.b2_dynamicBody;
                bd.fixedRotation = true;
                bd.allowSleep    = false;

                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.5f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.Defaults();
                fd.shape   = shape;
                fd.density = 20.0f;
                body.CreateFixture(fd);
            }

            // Square character 2
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(-5.0f, 5.0f);
                bd.type          = b2BodyType.b2_dynamicBody;
                bd.fixedRotation = true;
                bd.allowSleep    = false;

                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.25f, 0.25f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.Defaults();
                fd.shape   = shape;
                fd.density = 20.0f;
                body.CreateFixture(fd);
            }

            // Hexagon character
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(-5.0f, 8.0f);
                bd.type          = b2BodyType.b2_dynamicBody;
                bd.fixedRotation = true;
                bd.allowSleep    = false;

                b2Body body = m_world.CreateBody(bd);

                float    angle    = 0.0f;
                float    delta    = b2Settings.b2_pi / 3.0f;
                b2Vec2[] vertices = new b2Vec2[6];
                for (int i = 0; i < 6; ++i)
                {
                    vertices[i].Set(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle));
                    angle += delta;
                }

                b2PolygonShape shape = new b2PolygonShape();
                shape.Set(vertices, 6);

                b2FixtureDef fd = new b2FixtureDef();
                fd.Defaults();
                fd.shape   = shape;
                fd.density = 20.0f;
                body.CreateFixture(fd);
            }

            // Circle character
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(3.0f, 5.0f);
                bd.type          = b2BodyType.b2_dynamicBody;
                bd.fixedRotation = true;
                bd.allowSleep    = false;

                b2Body body = m_world.CreateBody(bd);

                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.5f;

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 20.0f;
                body.CreateFixture(fd);
            }

            // Circle character
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(-7.0f, 6.0f);
                bd.type       = b2BodyType.b2_dynamicBody;
                bd.allowSleep = false;

                m_character = m_world.CreateBody(bd);

                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.25f;

                b2FixtureDef fd = new b2FixtureDef();
                fd.Defaults();
                fd.shape    = shape;
                fd.density  = 20.0f;
                fd.friction = 1.0f;
                m_character.CreateFixture(fd);
            }
        }
Example #8
0
        void CreateObject()
        {
            // Define the dynamic body.
            var bodyDef = b2BodyDef.Create();

            bodyDef.type = b2BodyType.b2_dynamicBody;             //or you could use b2_staticBody

            bodyDef.position.Set(initialLocation.X / Constants.PTM_RATIO, initialLocation.Y / Constants.PTM_RATIO);

            var shape       = new b2PolygonShape();
            var shapeCircle = new b2CircleShape();

            if (shapeCreationMethod == CreationMethod.DiameterOfImageForCircle)
            {
                var tempSprite     = new CCSprite(spriteImageName);
                var radiusInMeters = (tempSprite.ContentSize.Width / Constants.PTM_RATIO) * 0.5f;

                shapeCircle.Radius = radiusInMeters;
            }


            else if (shapeCreationMethod == CreationMethod.ShapeOfSourceImage)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                      //top left corner
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                     //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                      //bottom right corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)                        //top right corner
                };
                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Triangle)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 3;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO), //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),  //bottom right corner
                    new b2Vec2(0.0f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)                                  // top center of image
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.TriangleRightAngle)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 3;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                        //top right corner
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                       //top left corner
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO)                       //bottom left corner
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Trapezoid)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                        //top of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                       //top of image, 1/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                      //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                       //bottom right corner
                };

                shape.Set(vertices, num);
            }


            else if (shapeCreationMethod == CreationMethod.Hexagon)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 6;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),  //top of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                 // left, center
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO), //bottom of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),  //bottom of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                  // right, center
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)    //top of image, 3/4's across
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Pentagon)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 5;
                b2Vec2[] vertices =
                {
                    new b2Vec2(0 / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                                    //top of image, center
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                 // left, center
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO), //bottom of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),  //bottom of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO),                                  // right, center
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.Octagon)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 8;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -6) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                        //use the source image octogonShape.png for reference
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / -6) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 6) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / -6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / 6) / Constants.PTM_RATIO),
                    new b2Vec2((tempSprite.ContentSize.Width / 6) / Constants.PTM_RATIO,  (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)
                };

                shape.Set(vertices, num);
            }
            else if (shapeCreationMethod == CreationMethod.Parallelogram)
            {
                var tempSprite = new CCSprite(spriteImageName);

                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2((tempSprite.ContentSize.Width / -4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO),                      //top of image, 1/4 across
                    new b2Vec2((tempSprite.ContentSize.Width / -2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                     //bottom left corner
                    new b2Vec2((tempSprite.ContentSize.Width / 4) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2) / Constants.PTM_RATIO),                      //bottom of image, 3/4's across
                    new b2Vec2((tempSprite.ContentSize.Width / 2) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2) / Constants.PTM_RATIO)                        //top right corner
                };

                shape.Set(vertices, num);
            }

            else if (shapeCreationMethod == CreationMethod.CustomCoordinates1)                 //use your own custom coordinates from a program like Vertex Helper Pro

            {
                var      num      = 4;
                b2Vec2[] vertices =
                {
                    new b2Vec2(-64.0f / Constants.PTM_RATIO,  16.0f / Constants.PTM_RATIO),
                    new b2Vec2(-64.0f / Constants.PTM_RATIO, -16.0f / Constants.PTM_RATIO),
                    new b2Vec2(64.0f / Constants.PTM_RATIO,  -16.0f / Constants.PTM_RATIO),
                    new b2Vec2(64.0f / Constants.PTM_RATIO, 16.0f / Constants.PTM_RATIO)
                };
                shape.Set(vertices, num);
            }



            // Define the dynamic body fixture.
            var fixtureDef = b2FixtureDef.Create();

            if (shapeCreationMethod == CreationMethod.DiameterOfImageForCircle)
            {
                fixtureDef.shape = shapeCircle;
            }
            else
            {
                fixtureDef.shape = shape;
            }

            fixtureDef.density     = theDensity;
            fixtureDef.friction    = 0.3f;
            fixtureDef.restitution = 0.1f;

            CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);


            if (angle != 0)
            {
                int    currentAngle     = (int)body.Angle;
                b2Vec2 locationInMeters = body.Position;
                body.SetTransform(locationInMeters, CCMacros.CCDegreesToRadians(currentAngle + angle));
            }

            if (IsStatic)
            {
                MakeBodyStatic();
            }
        }
Example #9
0
        public DumpShell()
        {
            b2Vec2 g = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);

            m_world.Gravity = g;
            b2Body[]  bodies = new b2Body[3];
            b2Joint[] joints = new b2Joint[2];
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(1.304347801208496e+01f, 2.500000000000000e+00f);
                bd.angle = 0.000000000000000e+00f;
                bd.linearVelocity.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping   = 5.000000000000000e-01f;
                bd.angularDamping  = 5.000000000000000e-01f;
                bd.allowSleep      = true;
                bd.awake           = true;
                bd.fixedRotation   = false;
                bd.bullet          = false;
                bd.active          = true;
                bd.gravityScale    = 1.000000000000000e+00f;
                bodies[0]          = m_world.CreateBody(bd);

                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction            = 1.000000000000000e+00f;
                    fd.restitution         = 5.000000000000000e-01f;
                    fd.density             = 1.000000000000000e+01f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits     = 65535;
                    fd.filter.groupIndex   = 0;
                    b2PolygonShape shape = new b2PolygonShape();
                    b2Vec2[]       vs    = new b2Vec2[8];
                    vs[0].Set(-6.900000095367432e+00f, -3.000000119209290e-01f);
                    vs[1].Set(2.000000029802322e-01f, -3.000000119209290e-01f);
                    vs[2].Set(2.000000029802322e-01f, 2.000000029802322e-01f);
                    vs[3].Set(-6.900000095367432e+00f, 2.000000029802322e-01f);
                    shape.Set(vs, 4);

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
            }
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(8.478260636329651e-01f, 2.500000000000000e+00f);
                bd.angle = 0.000000000000000e+00f;
                bd.linearVelocity.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping   = 5.000000000000000e-01f;
                bd.angularDamping  = 5.000000000000000e-01f;
                bd.allowSleep      = true;
                bd.awake           = true;
                bd.fixedRotation   = false;
                bd.bullet          = false;
                bd.active          = true;
                bd.gravityScale    = 1.000000000000000e+00f;
                bodies[1]          = m_world.CreateBody(bd);

                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction            = 1.000000000000000e+00f;
                    fd.restitution         = 5.000000000000000e-01f;
                    fd.density             = 1.000000000000000e+01f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits     = 65535;
                    fd.filter.groupIndex   = 0;
                    b2PolygonShape shape = new b2PolygonShape();
                    b2Vec2[]       vs    = new b2Vec2[8];
                    vs[0].Set(-3.228000104427338e-01f, -2.957000136375427e-01f);
                    vs[1].Set(6.885900020599365e+00f, -3.641000092029572e-01f);
                    vs[2].Set(6.907599925994873e+00f, 3.271999955177307e-01f);
                    vs[3].Set(-3.228000104427338e-01f, 2.825999855995178e-01f);
                    shape.Set(vs, 4);

                    fd.shape = shape;

                    bodies[1].CreateFixture(fd);
                }
            }

            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_staticBody;
                bd.position.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angle = 0.000000000000000e+00f;
                bd.linearVelocity.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping   = 0.000000000000000e+00f;
                bd.angularDamping  = 0.000000000000000e+00f;
                bd.allowSleep      = true;
                bd.awake           = true;
                bd.fixedRotation   = false;
                bd.bullet          = false;
                bd.active          = true;
                bd.gravityScale    = 1.000000000000000e+00f;
                bodies[2]          = m_world.CreateBody(bd);

                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction            = 1.000000000000000e+01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits     = 65535;
                    fd.filter.groupIndex   = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius     = 9.999999776482582e-03f;
                    shape.Vertex0    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1    = new b2Vec2(4.452173995971680e+01f, 1.669565200805664e+01f);
                    shape.Vertex2    = new b2Vec2(4.452173995971680e+01f, 0.000000000000000e+00f);
                    shape.Vertex3    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction            = 1.000000000000000e+01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits     = 65535;
                    fd.filter.groupIndex   = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius     = 9.999999776482582e-03f;
                    shape.Vertex0    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1    = new b2Vec2(0.000000000000000e+00f, 1.669565200805664e+01f);
                    shape.Vertex2    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex3    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction            = 1.000000000000000e+01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits     = 65535;
                    fd.filter.groupIndex   = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius     = 9.999999776482582e-03f;
                    shape.Vertex0    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1    = new b2Vec2(0.000000000000000e+00f, 1.669565200805664e+01f);
                    shape.Vertex2    = new b2Vec2(4.452173995971680e+01f, 1.669565200805664e+01f);
                    shape.Vertex3    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction            = 1.000000000000000e+01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits     = 65535;
                    fd.filter.groupIndex   = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius     = 9.999999776482582e-03f;
                    shape.Vertex0    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex2    = new b2Vec2(4.452173995971680e+01f, 0.000000000000000e+00f);
                    shape.Vertex3    = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
            }

            {
                b2PrismaticJointDef jd = new b2PrismaticJointDef();
                jd.BodyA            = bodies[1];
                jd.BodyB            = bodies[0];
                jd.CollideConnected = false;
                jd.localAnchorA.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                jd.localAnchorB.Set(-1.219565200805664e+01f, 0.000000000000000e+00f);
                jd.localAxisA.Set(-1.219565200805664e+01f, 0.000000000000000e+00f);
                jd.referenceAngle   = 0.000000000000000e+00f;
                jd.enableLimit      = true;
                jd.lowerTranslation = -2.000000000000000e+01f;
                jd.upperTranslation = 0.000000000000000e+00f;
                jd.enableMotor      = true;
                jd.motorSpeed       = 0.000000000000000e+00f;
                jd.maxMotorForce    = 1.000000000000000e+01f;
                joints[0]           = m_world.CreateJoint(jd);
            }
            {
                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.BodyA            = bodies[1];
                jd.BodyB            = bodies[2];
                jd.CollideConnected = false;
                jd.localAnchorA.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                jd.localAnchorB.Set(8.478260636329651e-01f, 2.500000000000000e+00f);
                jd.referenceAngle = 0.000000000000000e+00f;
                jd.enableLimit    = false;
                jd.lowerAngle     = 0.000000000000000e+00f;
                jd.upperAngle     = 0.000000000000000e+00f;
                jd.enableMotor    = false;
                jd.motorSpeed     = 0.000000000000000e+00f;
                jd.maxMotorTorque = 0.000000000000000e+00f;
                joints[1]         = m_world.CreateJoint(jd);
            }
        }
Example #10
0
        public Revolute()
        {
            b2Body ground;
            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                //fd.filter.categoryBits = 2;

                ground.CreateFixture(fd);
            }

            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.5f;

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                b2RevoluteJointDef rjd = new b2RevoluteJointDef();

                bd.position.Set(-10.0f, 20.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                float w = 100.0f;
                body.AngularVelocity = w;
                body.LinearVelocity  = new b2Vec2(-8.0f * w, 0.0f);

                rjd.Initialize(ground, body, new b2Vec2(-10.0f, 12.0f));
                rjd.motorSpeed       = 1.0f * b2Settings.b2_pi;
                rjd.maxMotorTorque   = 10000.0f;
                rjd.enableMotor      = false;
                rjd.lowerAngle       = -0.25f * b2Settings.b2_pi;
                rjd.upperAngle       = 0.5f * b2Settings.b2_pi;
                rjd.enableLimit      = true;
                rjd.CollideConnected = true;

                m_joint = (b2RevoluteJoint)m_world.CreateJoint(rjd);
            }

            {
                b2CircleShape circle_shape = new b2CircleShape();
                circle_shape.Radius = 3.0f;

                b2BodyDef circle_bd = new b2BodyDef();
                circle_bd.type = b2BodyType.b2_dynamicBody;
                circle_bd.position.Set(5.0f, 30.0f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.density         = 5.0f;
                fd.filter.maskBits = 1;
                fd.shape           = circle_shape;

                m_ball = m_world.CreateBody(circle_bd);
                m_ball.CreateFixture(fd);

                b2PolygonShape polygon_shape = new b2PolygonShape();
                polygon_shape.SetAsBox(10.0f, 0.2f, new b2Vec2(-10.0f, 0.0f), 0.0f);

                b2BodyDef polygon_bd = new b2BodyDef();
                polygon_bd.position.Set(20.0f, 10.0f);
                polygon_bd.type   = b2BodyType.b2_dynamicBody;
                polygon_bd.bullet = true;
                b2Body polygon_body = m_world.CreateBody(polygon_bd);
                polygon_body.CreateFixture(polygon_shape, 2.0f);

                b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                rjd.Initialize(ground, polygon_body, new b2Vec2(20.0f, 10.0f));
                rjd.lowerAngle  = -0.25f * b2Settings.b2_pi;
                rjd.upperAngle  = 0.0f * b2Settings.b2_pi;
                rjd.enableLimit = true;
                m_world.CreateJoint(rjd);
            }

            // Tests mass computation of a small object far from the origin
            {
                b2BodyDef bodyDef = new b2BodyDef();
                bodyDef.type = b2BodyType.b2_dynamicBody;
                b2Body body = m_world.CreateBody(bodyDef);

                b2PolygonShape polyShape = new b2PolygonShape();
                b2Vec2[]       verts     = new b2Vec2[3];
                verts[0].Set(17.63f, 36.31f);
                verts[1].Set(17.52f, 36.69f);
                verts[2].Set(17.19f, 36.36f);
                polyShape.Set(verts, 3);

                b2FixtureDef polyFixtureDef = new b2FixtureDef();
                polyFixtureDef.shape   = polyShape;
                polyFixtureDef.density = 1;

                body.CreateFixture(polyFixtureDef); //assertion hits inside here
            }
        }
Example #11
0
        public Bridge()
        {
            b2Body ground = null;

            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = shape;
                fd.density  = 20.0f;
                fd.friction = 0.2f;

                b2RevoluteJointDef jd = new b2RevoluteJointDef();

                b2Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type     = b2BodyType.b2_dynamicBody;
                    bd.position = new b2Vec2(-14.5f + 1.0f * i, 5.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(-15.0f + 1.0f * i, 5.0f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    if (i == (e_count >> 1))
                    {
                        m_middle = body;
                    }
                    prevBody = body;
                }

                b2Vec2 anchor1 = new b2Vec2(-15.0f + 1.0f * e_count, 5.0f);
                jd.Initialize(prevBody, ground, anchor1);
                m_world.CreateJoint(jd);
            }

            for (int i = 0; i < 2; ++i)
            {
                b2Vec2[] vertices = new b2Vec2[3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);

                b2PolygonShape shape = new b2PolygonShape();
                shape.Set(vertices, 3);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 1.0f;

                b2BodyDef bd = new b2BodyDef();
                bd.type     = b2BodyType.b2_dynamicBody;
                bd.position = new b2Vec2(-8.0f + 8.0f * i, 12.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }

            for (int i = 0; i < 3; ++i)
            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.5f;

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = shape;
                fd.density = 1.0f;

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-6.0f + 6.0f * i, 10.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
Example #12
0
        public CompoundShapes()
        {
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                b2Body body = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(50.0f, 0.0f), new b2Vec2(-50.0f, 0.0f));

                body.CreateFixture(shape, 0.0f);
            }

            {
                b2CircleShape circle1 = new b2CircleShape();
                circle1.Radius   = 0.5f;
                circle1.Position = new b2Vec2(-0.5f, 0.5f);

                b2CircleShape circle2 = new b2CircleShape();
                circle2.Radius   = 0.5f;
                circle2.Position = new b2Vec2(0.5f, 0.5f);

                for (int i = 0; i < 10; ++i)
                {
                    float     x  = Rand.RandomFloat(-0.1f, 0.1f);
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(x + 5.0f, 1.05f + 2.5f * i);
                    bd.angle = Rand.RandomFloat(-b2Settings.b2_pi, b2Settings.b2_pi);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(circle1, 2.0f);
                    body.CreateFixture(circle2, 0.0f);
                }
            }

            {
                b2PolygonShape polygon1 = new b2PolygonShape();
                polygon1.SetAsBox(0.25f, 0.5f);

                b2PolygonShape polygon2 = new b2PolygonShape();
                polygon2.SetAsBox(0.25f, 0.5f, new b2Vec2(0.0f, -0.5f), 0.5f * b2Settings.b2_pi);

                for (int i = 0; i < 10; ++i)
                {
                    float     x  = Rand.RandomFloat(-0.1f, 0.1f);
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(x - 5.0f, 1.05f + 2.5f * i);
                    bd.angle = Rand.RandomFloat(-b2Settings.b2_pi, b2Settings.b2_pi);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(polygon1, 2.0f);
                    body.CreateFixture(polygon2, 2.0f);
                }
            }

            {
                b2Transform xf1 = new b2Transform();
                xf1.q.Set(0.3524f * b2Settings.b2_pi);
                xf1.p = xf1.q.GetXAxis();

                b2Vec2[] vertices = new b2Vec2[3];

                b2PolygonShape triangle1 = new b2PolygonShape();
                vertices[0] = b2Math.b2Mul(xf1, new b2Vec2(-1.0f, 0.0f));
                vertices[1] = b2Math.b2Mul(xf1, new b2Vec2(1.0f, 0.0f));
                vertices[2] = b2Math.b2Mul(xf1, new b2Vec2(0.0f, 0.5f));
                triangle1.Set(vertices, 3);

                b2Transform xf2 = new b2Transform();
                xf2.q.Set(-0.3524f * b2Settings.b2_pi);
                xf2.p = -xf2.q.GetXAxis();

                b2PolygonShape triangle2 = new b2PolygonShape();
                vertices[0] = b2Math.b2Mul(xf2, new b2Vec2(-1.0f, 0.0f));
                vertices[1] = b2Math.b2Mul(xf2, new b2Vec2(1.0f, 0.0f));
                vertices[2] = b2Math.b2Mul(xf2, new b2Vec2(0.0f, 0.5f));
                triangle2.Set(vertices, 3);

                for (int i = 0; i < 10; ++i)
                {
                    float     x  = Rand.RandomFloat(-0.1f, 0.1f);
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(x, 2.05f + 2.5f * i);
                    bd.angle = 0.0f;
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(triangle1, 2.0f);
                    body.CreateFixture(triangle2, 2.0f);
                }
            }

            {
                b2PolygonShape bottom = new b2PolygonShape();
                bottom.SetAsBox(1.5f, 0.15f);

                b2PolygonShape left = new b2PolygonShape();
                left.SetAsBox(0.15f, 2.7f, new b2Vec2(-1.45f, 2.35f), 0.2f);

                b2PolygonShape right = new b2PolygonShape();
                right.SetAsBox(0.15f, 2.7f, new b2Vec2(1.45f, 2.35f), -0.2f);

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 2.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(bottom, 4.0f);
                body.CreateFixture(left, 4.0f);
                body.CreateFixture(right, 4.0f);
            }
        }
Example #13
0
        public ApplyForce()
        {
            m_world.Gravity = new b2Vec2(0.0f, 0.0f);

            float k_restitution = 0.4f;

            b2Body ground;
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position = new b2Vec2(0.0f, 20.0f);
                ground      = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();

                b2FixtureDef sd = new b2FixtureDef();
                sd.shape       = shape;
                sd.density     = 0.0f;
                sd.restitution = k_restitution;

                // Left vertical
                shape.Set(new b2Vec2(-20.0f, -20.0f), new b2Vec2(-20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Right vertical
                shape.Set(new b2Vec2(20.0f, -20.0f), new b2Vec2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Top horizontal
                shape.Set(new b2Vec2(-20.0f, 20.0f), new b2Vec2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Bottom horizontal
                shape.Set(new b2Vec2(-20.0f, -20.0f), new b2Vec2(20.0f, -20.0f));
                ground.CreateFixture(sd);
            }

            {
                b2Transform xf1;
                xf1.q = new b2Rot(0.3524f * b2Settings.b2_pi);
                xf1.p = xf1.q.GetXAxis();

                b2Vec2[] vertices = new b2Vec2[3];
                vertices[0] = b2Math.b2Mul(xf1, new b2Vec2(-1.0f, 0.0f));
                vertices[1] = b2Math.b2Mul(xf1, new b2Vec2(1.0f, 0.0f));
                vertices[2] = b2Math.b2Mul(xf1, new b2Vec2(0.0f, 0.5f));

                b2PolygonShape poly1 = new b2PolygonShape();
                poly1.Set(vertices, 3);

                b2FixtureDef sd1 = new b2FixtureDef();
                sd1.shape   = poly1;
                sd1.density = 4.0f;

                b2Transform xf2 = new b2Transform();
                xf2.q.Set(-0.3524f * b2Settings.b2_pi);
                xf2.p = -xf2.q.GetXAxis();

                vertices[0] = b2Math.b2Mul(xf2, new b2Vec2(-1.0f, 0.0f));
                vertices[1] = b2Math.b2Mul(xf2, new b2Vec2(1.0f, 0.0f));
                vertices[2] = b2Math.b2Mul(xf2, new b2Vec2(0.0f, 0.5f));

                b2PolygonShape poly2 = new b2PolygonShape();
                poly2.Set(vertices, 3);

                b2FixtureDef sd2 = new b2FixtureDef();
                sd2.shape   = poly2;
                sd2.density = 2.0f;

                b2BodyDef bd = new b2BodyDef();
                bd.type           = b2BodyType.b2_dynamicBody;
                bd.angularDamping = 5.0f;
                bd.linearDamping  = 0.1f;

                bd.position.Set(0.0f, 2.0f);
                bd.angle      = b2Settings.b2_pi;
                bd.allowSleep = false;
                m_body        = m_world.CreateBody(bd);
                m_body.CreateFixture(sd1);
                m_body.CreateFixture(sd2);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.5f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = shape;
                fd.density  = 1.0f;
                fd.friction = 0.3f;

                for (int i = 0; i < 10; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;

                    bd.position = new b2Vec2(0.0f, 5.0f + 1.54f * i);
                    b2Body body = m_world.CreateBody(bd);

                    body.CreateFixture(fd);

                    float gravity = 10.0f;
                    float I       = body.Inertia;
                    float mass    = body.Mass;

                    // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
                    float radius = b2Math.b2Sqrt(2.0f * I / mass);

                    b2FrictionJointDef jd = new b2FrictionJointDef();
                    jd.localAnchorA     = b2Vec2.Zero;
                    jd.localAnchorB     = b2Vec2.Zero;
                    jd.BodyA            = ground;
                    jd.BodyB            = body;
                    jd.CollideConnected = true;
                    jd.maxForce         = mass * gravity;
                    jd.maxTorque        = mass * radius * gravity;

                    m_world.CreateJoint(jd);
                }
            }
        }
Example #14
0
        public void CreateLeg(float s, b2Vec2 wheelAnchor)
        {
            b2Vec2 p1 = new b2Vec2(5.4f * s, -6.1f);
            b2Vec2 p2 = new b2Vec2(7.2f * s, -1.2f);
            b2Vec2 p3 = new b2Vec2(4.3f * s, -1.9f);
            b2Vec2 p4 = new b2Vec2(3.1f * s, 0.8f);
            b2Vec2 p5 = new b2Vec2(6.0f * s, 1.5f);
            b2Vec2 p6 = new b2Vec2(2.5f * s, 3.7f);

            b2FixtureDef fd1 = new b2FixtureDef();
            b2FixtureDef fd2 = new b2FixtureDef();

            fd1.filter.groupIndex = -1;
            fd2.filter.groupIndex = -1;
            fd1.density           = 1.0f;
            fd2.density           = 1.0f;

            b2PolygonShape poly1 = new b2PolygonShape();
            b2PolygonShape poly2 = new b2PolygonShape();

            if (s > 0.0f)
            {
                b2Vec2[] vertices = new b2Vec2[3];

                vertices[0] = p1;
                vertices[1] = p2;
                vertices[2] = p3;
                poly1.Set(vertices, 3);

                vertices[0] = b2Vec2.Zero;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                poly2.Set(vertices, 3);
            }
            else
            {
                b2Vec2[] vertices = new b2Vec2[3];

                vertices[0] = p1;
                vertices[1] = p3;
                vertices[2] = p2;
                poly1.Set(vertices, 3);

                vertices[0] = b2Vec2.Zero;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                poly2.Set(vertices, 3);
            }

            fd1.shape = poly1;
            fd2.shape = poly2;

            b2BodyDef bd1 = new b2BodyDef();
            b2BodyDef bd2 = new b2BodyDef();

            bd1.type     = b2BodyType.b2_dynamicBody;
            bd2.type     = b2BodyType.b2_dynamicBody;
            bd1.position = m_offset;
            bd2.position = p4 + m_offset;

            bd1.angularDamping = 10.0f;
            bd2.angularDamping = 10.0f;

            b2Body body1 = m_world.CreateBody(bd1);
            b2Body body2 = m_world.CreateBody(bd2);

            body1.CreateFixture(fd1);
            body2.CreateFixture(fd2);

            b2DistanceJointDef djd = new b2DistanceJointDef();

            // Using a soft distance constraint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            djd.dampingRatio = 0.5f;
            djd.frequencyHz  = 10.0f;

            djd.Initialize(body1, body2, p2 + m_offset, p5 + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset);
            m_world.CreateJoint(djd);

            b2RevoluteJointDef rjd = new b2RevoluteJointDef();

            rjd.Initialize(body2, m_chassis, p4 + m_offset);
            m_world.CreateJoint(rjd);
        }
        public CollisionProcessing()
        {
            // Ground body
            {
                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-50.0f, 0.0f), new b2Vec2(50.0f, 0.0f));

                b2FixtureDef sd = new b2FixtureDef();
                sd.shape = shape;

                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            float xLo = -5.0f, xHi = 5.0f;
            float yLo = 2.0f, yHi = 35.0f;

            // Small triangle
            b2Vec2[] vertices = new b2Vec2[3];
            vertices[0].Set(-1.0f, 0.0f);
            vertices[1].Set(1.0f, 0.0f);
            vertices[2].Set(0.0f, 2.0f);

            b2PolygonShape polygon = new b2PolygonShape();

            polygon.Set(vertices, 3);

            b2FixtureDef triangleShapeDef = new b2FixtureDef();

            triangleShapeDef.shape   = polygon;
            triangleShapeDef.density = 1.0f;

            b2BodyDef triangleBodyDef = new b2BodyDef();

            triangleBodyDef.type = b2BodyType.b2_dynamicBody;
            triangleBodyDef.position.Set(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            b2Body body1 = m_world.CreateBody(triangleBodyDef);

            body1.CreateFixture(triangleShapeDef);

            // Large triangle (recycle definitions)
            vertices[0] *= 2.0f;
            vertices[1] *= 2.0f;
            vertices[2] *= 2.0f;
            polygon.Set(vertices, 3);

            triangleBodyDef.position.Set(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            b2Body body2 = m_world.CreateBody(triangleBodyDef);

            body2.CreateFixture(triangleShapeDef);

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);

            b2FixtureDef boxShapeDef = new b2FixtureDef();

            boxShapeDef.shape   = polygon;
            boxShapeDef.density = 1.0f;

            b2BodyDef boxBodyDef = new b2BodyDef();

            boxBodyDef.type = b2BodyType.b2_dynamicBody;
            boxBodyDef.position.Set(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            b2Body body3 = m_world.CreateBody(boxBodyDef);

            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxBodyDef.position.Set(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            b2Body body4 = m_world.CreateBody(boxBodyDef);

            body4.CreateFixture(boxShapeDef);

            // Small circle
            b2CircleShape circle = new b2CircleShape();

            circle.Radius = 1.0f;

            b2FixtureDef circleShapeDef = new b2FixtureDef();

            circleShapeDef.shape   = circle;
            circleShapeDef.density = 1.0f;

            b2BodyDef circleBodyDef = new b2BodyDef();

            circleBodyDef.type = b2BodyType.b2_dynamicBody;
            circleBodyDef.position.Set(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            b2Body body5 = m_world.CreateBody(circleBodyDef);

            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle.Radius = circle.Radius * 2.0f;
            circleBodyDef.position.Set(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi));

            b2Body body6 = m_world.CreateBody(circleBodyDef);

            body6.CreateFixture(circleShapeDef);
        }
Example #16
0
        public Car()
        {
            m_hz    = 4.0f;
            m_zeta  = 0.7f;
            m_speed = 50.0f;

            b2Body ground = null;
            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = shape;
                fd.density  = 0.0f;
                fd.friction = 0.6f;

                shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f));
                ground.CreateFixture(fd);

                float[] hs = { 0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f };

                float x = 20.0f, y1 = 0.0f, dx = 5.0f;

                for (int i = 0; i < 10; ++i)
                {
                    float y2 = hs[i];
                    shape.Set(new b2Vec2(x, y1), new b2Vec2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                for (int i = 0; i < 10; ++i)
                {
                    float y2 = hs[i];
                    shape.Set(new b2Vec2(x, y1), new b2Vec2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 80.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 10.0f, 5.0f));
                ground.CreateFixture(fd);

                x += 20.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x, 20.0f));
                ground.CreateFixture(fd);
            }

            // Teeter
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(140.0f, 1.0f);
                bd.type = b2BodyType.b2_dynamicBody;
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(10.0f, 0.25f);
                body.CreateFixture(box, 1.0f);

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.Initialize(ground, body, body.Position);
                jd.lowerAngle  = -8.0f * b2Settings.b2_pi / 180.0f;
                jd.upperAngle  = 8.0f * b2Settings.b2_pi / 180.0f;
                jd.enableLimit = true;
                m_world.CreateJoint(jd);

                body.ApplyAngularImpulse(100.0f);
            }

            // Bridge
            {
                int            N     = 20;
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(1.0f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = shape;
                fd.density  = 1.0f;
                fd.friction = 0.6f;

                b2RevoluteJointDef jd = new b2RevoluteJointDef();

                b2Body prevBody = ground;
                for (int i = 0; i < N; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(161.0f + 2.0f * i, -0.125f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(160.0f + 2.0f * i, -0.125f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }

                b2Vec2 anchor1 = new b2Vec2(160.0f + 2.0f * N, -0.125f);
                jd.Initialize(prevBody, ground, anchor1);
                m_world.CreateJoint(jd);
            }

            // Boxes
            {
                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(0.5f, 0.5f);

                b2Body    body = null;
                b2BodyDef bd   = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                bd.position.Set(230.0f, 0.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 1.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 2.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 3.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 4.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);
            }

            // Car
            {
                b2PolygonShape chassis  = new b2PolygonShape();
                b2Vec2[]       vertices = new b2Vec2[8];
                vertices[0].Set(-1.5f, -0.5f);
                vertices[1].Set(1.5f, -0.5f);
                vertices[2].Set(1.5f, 0.0f);
                vertices[3].Set(0.0f, 0.9f);
                vertices[4].Set(-1.15f, 0.9f);
                vertices[5].Set(-1.5f, 0.2f);
                chassis.Set(vertices, 6);

                b2CircleShape circle = new b2CircleShape();
                circle.Radius = 0.4f;

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 1.0f);
                m_car = m_world.CreateBody(bd);
                m_car.CreateFixture(chassis, 1.0f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = circle;
                fd.density  = 1.0f;
                fd.friction = 0.9f;

                bd.position.Set(-1.0f, 0.35f);
                m_wheel1 = m_world.CreateBody(bd);
                m_wheel1.CreateFixture(fd);

                bd.position.Set(1.0f, 0.4f);
                m_wheel2 = m_world.CreateBody(bd);
                m_wheel2.CreateFixture(fd);

                b2WheelJointDef jd   = new b2WheelJointDef();
                b2Vec2          axis = new b2Vec2(0.0f, 1.0f);

                jd.Initialize(m_car, m_wheel1, m_wheel1.Position, axis);
                jd.motorSpeed     = 0.0f;
                jd.maxMotorTorque = 20.0f;
                jd.enableMotor    = true;
                jd.frequencyHz    = m_hz;
                jd.dampingRatio   = m_zeta;
                m_spring1         = (b2WheelJoint)m_world.CreateJoint(jd);

                jd.Initialize(m_car, m_wheel2, m_wheel2.Position, axis);
                jd.motorSpeed     = 0.0f;
                jd.maxMotorTorque = 10.0f;
                jd.enableMotor    = false;
                jd.frequencyHz    = m_hz;
                jd.dampingRatio   = m_zeta;
                m_spring2         = (b2WheelJoint)m_world.CreateJoint(jd);
            }
        }