예제 #1
0
        public ShapeEditing()
        {
            {
                b2BodyDef bd1    = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd1);

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

            b2BodyDef bd = new b2BodyDef();

            bd.type = b2BodyType.b2_dynamicBody;
            bd.position.Set(0.0f, 10.0f);
            m_body = m_world.CreateBody(bd);

            b2PolygonShape shape = new b2PolygonShape();

            shape.SetAsBox(4.0f, 4.0f, new b2Vec2(0.0f, 0.0f), 0.0f);
            m_fixture1 = m_body.CreateFixture(shape, 10.0f);

            m_fixture2 = null;

            m_sensor = false;
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override b2Shape CreatePhysicsShape()
        {
            var shape = new b2PolygonShape();

            shape.SetAsBox(PhysicWidth / 2, PhysicHeight / 2);
            return(shape);
        }
예제 #3
0
파일: Form1.cs 프로젝트: prepare/box2c
        void MakeBox(b2Vec2 pos, float w, float h)
        {
            // Define the dynamic body. We set its position and call the body factory.
            b2BodyDef bodyDef = new b2BodyDef();

            bodyDef.type     = b2BodyType.b2_dynamicBody;
            bodyDef.position = pos;
            var body = world.CreateBody(bodyDef);

            // Define another box shape for our dynamic body.
            b2PolygonShape dynamicBox = new b2PolygonShape();

            dynamicBox.SetAsBox(w, h);

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

            fixtureDef.shape = dynamicBox;

            // Set the box density to be non-zero, so it will be dynamic.
            fixtureDef.density = 1.0f;

            // Override the default friction.
            fixtureDef.friction = 0.3f;

            // Add the shape to the body.
            body.CreateFixture(fixtureDef);
        }
예제 #4
0
        void SetPhysicsToSquare(Square square)
        {
            // Define the dynamic body.
            //Set up a 1m squared box in the physics world
            b2BodyDef def = new b2BodyDef();

            def.position = new b2Vec2(square.Position.X / PTM_RATIO, square.Position.Y / PTM_RATIO);
            def.type     = b2BodyType.b2_dynamicBody;
            b2Body body = _world.CreateBody(def);
            // Define another box shape for our dynamic body.
            var dynamicBox = new b2PolygonShape();

            dynamicBox.SetAsBox(0.5f, 0.5f); //These are mid points for our 1m box

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

            fd.shape    = dynamicBox;
            fd.density  = 1f;
            fd.friction = 0.3f;
            b2Fixture fixture = body.CreateFixture(fd);

            square.PhysicsBody = body;
            //_world.SetContactListener(new Myb2Listener());

            // _world.Dump();
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override b2Shape CreatePhysicsShape()
        {
            var shape = new b2PolygonShape();

            shape.SetAsBox(PhysicWidth / 2f, PhysicHeight / 2f);
            GroundSprite.DrawRect(new CCRect(0, 0, Width, Height), CCColor4B.Black);
            return(shape);
        }
예제 #6
0
        public ContinuousTest()
        {
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                b2Body body = m_world.CreateBody(bd);

                b2EdgeShape edge = new b2EdgeShape();

                edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f));
                body.CreateFixture(edge, 0.0f);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f);
                body.CreateFixture(shape, 0.0f);
            }

#if true
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 20.0f);
                //bd.angle = 0.1f;

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(2.0f, 0.1f);

                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(shape, 1.0f);

                m_angularVelocity = Rand.RandomFloat(-50.0f, 50.0f);
                //m_angularVelocity = 46.661274f;
                m_body.LinearVelocity  = new b2Vec2(0.0f, -100.0f);
                m_body.AngularVelocity = m_angularVelocity;
            }
#else
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 2.0f);
                b2Body body = m_world.CreateBody(bd);

                b2CircleShape shape = new b2CircleShape();
                shape.Position = b2Vec2.Zero;
                shape.Radius   = 0.5f;
                body.CreateFixture(shape, 1.0f);

                bd.bullet = true;
                bd.position.Set(0.0f, 10.0f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);
                body.LinearVelocity = new b2Vec2(0.0f, -100.0f);
            }
#endif
        }
예제 #7
0
        public BulletTest()
        {
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                b2Body body = m_world.CreateBody(bd);

                b2EdgeShape edge = new b2EdgeShape();

                edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f));
                body.CreateFixture(edge, 0.0f);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f);
                body.CreateFixture(shape, 0.0f);
            }

            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 4.0f);

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(2.0f, 0.1f);

                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(box, 1.0f);

                box.SetAsBox(0.25f, 0.25f);

                //m_x = RandomFloat(-1.0f, 1.0f);
                m_x = 0.20352793f;
                bd.position.Set(m_x, 10.0f);
                bd.bullet = true;

                m_bullet = m_world.CreateBody(bd);
                m_bullet.CreateFixture(box, 100.0f);

                m_bullet.LinearVelocity = new b2Vec2(0.0f, -50.0f);
            }
        }
예제 #8
0
        public Tumbler()
        {
            b2Body ground;

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

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

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 10.0f, new b2Vec2(10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(0.5f, 10.0f, new b2Vec2(-10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, 10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, -10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.BodyA = ground;
                jd.BodyB = body;
                jd.localAnchorA.Set(0.0f, 10.0f);
                jd.localAnchorB.Set(0.0f, 0.0f);
                jd.referenceAngle = 0.0f;
                jd.motorSpeed     = 0.05f * b2Settings.b2_pi;
                jd.maxMotorTorque = 1e8f;
                jd.enableMotor    = true;
                m_joint           = (b2RevoluteJoint)m_world.CreateJoint(jd);
            }

            m_count = 0;
        }
예제 #9
0
        public Pulleys()
        {
            float y = 16.0f;
            float L = 12.0f;
            float a = 1.0f;
            float b = 2.0f;

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

                b2EdgeShape edge = new b2EdgeShape();
                edge.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                //ground->CreateFixture(&shape, 0.0f);

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

                circle.Position = new b2Vec2(-10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);

                circle.Position = new b2Vec2(10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(a, b);

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

                //bd.fixedRotation = true;
                bd.position.Set(-10.0f, y);
                b2Body body1 = m_world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.position.Set(10.0f, y);
                b2Body body2 = m_world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                b2PulleyJointDef pulleyDef     = new b2PulleyJointDef();
                b2Vec2           anchor1       = new b2Vec2(-10.0f, y + b);
                b2Vec2           anchor2       = new b2Vec2(10.0f, y + b);
                b2Vec2           groundAnchor1 = new b2Vec2(-10.0f, y + b + L);
                b2Vec2           groundAnchor2 = new b2Vec2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f);

                m_joint1 = (b2PulleyJoint)m_world.CreateJoint(pulleyDef);
            }
        }
예제 #10
0
        //public const int e_columnCount = 1;
        //public const int e_rowCount = 1;

        public VerticalStack()
        {
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    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);

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

            float[] xs = { 0.0f, -10.0f, -5.0f, 5.0f, 10.0f };

            for (int j = 0; j < e_columnCount; ++j)
            {
                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 < e_rowCount; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;

                    int n = j * e_rowCount + i;
                    Debug.Assert(n < e_rowCount * e_columnCount);
                    m_indices[n] = n;
                    bd.userData  = m_indices[n];

                    float x = 0.0f;
                    //float32 x = RandomFloat(-0.02f, 0.02f);
                    //float32 x = i % 2 == 0 ? -0.025f : 0.025f;
                    bd.position.Set(xs[j] + x, 0.752f + 1.54f * i);
                    b2Body body = m_world.CreateBody(bd);

                    m_bodies[n] = body;

                    body.CreateFixture(fd);
                }
            }

            m_bullet = null;
        }
예제 #11
0
        private void checkBounds()
        {
            for (int i = 0; i < liquid.Length; ++i)
            {
                if (liquid[i].WorldCenter.y < -10.0f)
                {
                    m_world.DestroyBody(liquid[i]);
                    float massPerParticle = totalMass / nParticles;

                    var pd = new b2CircleShape();
                    var fd = new b2FixtureDef();
                    fd.shape             = pd;
                    fd.density           = 1.0f;
                    fd.filter.groupIndex = -10;
                    pd.Radius            = .05f;
                    fd.restitution       = 0.4f;
                    fd.friction          = 0.0f;
                    float cx = 0.0f + Rand.RandomFloat(-0.6f, 0.6f);
                    float cy = 15.0f + Rand.RandomFloat(-2.3f, 2.0f);
                    var   bd = new b2BodyDef();
                    bd.position      = new b2Vec2(cx, cy);
                    bd.fixedRotation = true;
                    bd.type          = b2BodyType.b2_dynamicBody;
                    var b = m_world.CreateBody(bd);
                    b.CreateFixture(fd).UserData = LIQUID_INT;
                    var md = new b2MassData();
                    md.mass = massPerParticle;
                    md.I    = 1.0f;
                    b.SetMassData(md);
                    b.SetSleepingAllowed(false);
                    liquid[i] = b;
                }
            }

            if (bod.WorldCenter.y < -15.0f)
            {
                m_world.DestroyBody(bod);
                var polyDef = new b2PolygonShape();
                polyDef.SetAsBox(Rand.RandomFloat(0.3f, 0.7f), Rand.RandomFloat(0.3f, 0.7f));
                var bodyDef = new b2BodyDef();
                bodyDef.position = new b2Vec2(0.0f, 25.0f);
                bodyDef.type     = b2BodyType.b2_dynamicBody;
                bod = m_world.CreateBody(bodyDef);
                bod.CreateFixture(polyDef, 1f);
            }
        }
예제 #12
0
        public override void Step(Settings settings)
        {
            base.Step(settings);

            if (m_count < e_count)
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

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

                ++m_count;
            }
        }
예제 #13
0
    protected b2Body createBox(float w, float h, float x, float y)
    {
        b2BodyDef bodyDef = new b2BodyDef();

        bodyDef.type = b2Body.b2_dynamicBody;
        bodyDef.position.Set(x / ptm_ratio, y / ptm_ratio);
        b2Body body = _world.CreateBody(bodyDef);

        b2PolygonShape s = new b2PolygonShape();

        s.SetAsBox(w / ptm_ratio * 0.5f, h / ptm_ratio * 0.5f);
        b2FixtureDef fixtrureDef = new b2FixtureDef();

        fixtrureDef.shape = s;
        body.CreateFixture(fixtrureDef);

        return(body);
    }
        public void createRectangleWithDictionary(PlistDictionary dict, b2Body body, CCNode node, LHScene scene, CCSize size)
        {
            _shapeID   = dict ["shapeID"].AsInt;
            _shapeName = dict ["name"].AsString;

            b2PolygonShape shape = new b2PolygonShape();

            shape.SetAsBox(size.Width * 0.5f, size.Height * 0.5f);

            b2FixtureDef fixture = new b2FixtureDef();

            LHSetupb2FixtureWithInfo(fixture, dict);

            fixture.userData = this;
            fixture.shape    = shape;

            body.CreateFixture(fixture);
        }
예제 #15
0
        public Prismatic()
        {
            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(2.0f, 0.5f);

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-10.0f, 10.0f);
                bd.angle      = 0.5f * b2Settings.b2_pi;
                bd.allowSleep = false;
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                b2PrismaticJointDef pjd = new b2PrismaticJointDef();

                // Bouncy limit
                b2Vec2 axis = new b2Vec2(2.0f, 1.0f);
                axis.Normalize();
                pjd.Initialize(ground, body, new b2Vec2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f));

                pjd.motorSpeed       = 10.0f;
                pjd.maxMotorForce    = 10000.0f;
                pjd.enableMotor      = true;
                pjd.lowerTranslation = 0.0f;
                pjd.upperTranslation = 20.0f;
                pjd.enableLimit      = true;

                m_joint = (b2PrismaticJoint)m_world.CreateJoint(pjd);
            }
        }
예제 #16
0
        public OneSidedPlatform()
        {
            // Ground
            {
                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);
            }

            // Platform
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(3.0f, 0.5f);
                m_platform = body.CreateFixture(shape, 0.0f);

                m_bottom = 10.0f - 0.5f;
                m_top    = 10.0f + 0.5f;
            }

            // Actor
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 12.0f);
                b2Body body = m_world.CreateBody(bd);

                m_radius = 0.5f;
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = m_radius;
                m_character  = body.CreateFixture(shape, 20.0f);

                body.LinearVelocity = new b2Vec2(0.0f, -50.0f);

                m_state = State.e_unknown;
            }
        }
예제 #17
0
        public Chain()
        {
            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.6f, 0.125f);

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

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.CollideConnected = false;

                const float y        = 25.0f;
                b2Body      prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.5f + i, y);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
예제 #18
0
        public Pyramid()
        {
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    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);
            }

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

                b2Vec2 x = new b2Vec2(-7.0f, 0.75f);
                b2Vec2 y;
                b2Vec2 deltaX = new b2Vec2(0.5625f, 1.25f);
                b2Vec2 deltaY = new b2Vec2(1.125f, 0.0f);

                for (int i = 0; i < e_count; ++i)
                {
                    y = x;

                    for (int j = i; j < e_count; ++j)
                    {
                        b2BodyDef bd = new b2BodyDef();
                        bd.type     = b2BodyType.b2_dynamicBody;
                        bd.position = y;
                        b2Body body = m_world.CreateBody(bd);
                        body.CreateFixture(shape, 5.0f);

                        y += deltaY;
                    }

                    x += deltaX;
                }
            }
        }
예제 #19
0
        public BoxProp(
            b2World b2world,

            double[] size,
            double[] position

            )
        {
            /*
             * static rectangle shaped prop
             *
             *  pars:
             *  size - array [width, height]
             *  position - array [x, y], in world meters, of center
             */
            this.size = size;

            //initialize body
            var bdef = new b2BodyDef();

            bdef.position      = new b2Vec2(position[0], position[1]);
            bdef.angle         = 0;
            bdef.fixedRotation = true;
            this.body          = b2world.CreateBody(bdef);

            //initialize shape
            var fixdef = new b2FixtureDef();

            var shape = new b2PolygonShape();

            fixdef.shape = shape;

            shape.SetAsBox(this.size[0] / 2, this.size[1] / 2);

            fixdef.restitution = 0.4; //positively bouncy!



            this.body.CreateFixture(fixdef);
        }
예제 #20
0
        public void addNewSpriteAtPosition(CCPoint p)
        {
            //CCLog.Log("Add sprite #{2} : {0} x {1}", p.X, p.Y, _batch.ChildrenCount + 1);

            //We have a 64x64 sprite sheet with 4 different 32x32 images.  The following code is
            //just randomly picking one of the images
            int idx    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
            int idy    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
            var sprite = new CCPhysicsSprite(spriteTexture, new CCRect(32 * idx, 32 * idy, 32, 32));

            AddChild(sprite, 0, kTagForPhysicsSprite);

            sprite.Position = new CCPoint(p.X, p.Y);

            // Define the dynamic body.
            //Set up a 1m squared box in the physics world
            b2BodyDef def = new b2BodyDef();

            def.position = new b2Vec2(p.X / PTM_RATIO, p.Y / PTM_RATIO);
            def.type     = b2BodyType.b2_dynamicBody;
            b2Body body = _world.CreateBody(def);
            // Define another box shape for our dynamic body.
            var dynamicBox = new b2PolygonShape();

            dynamicBox.SetAsBox(.5f, .5f); //These are mid points for our 1m box

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

            fd.shape    = dynamicBox;
            fd.density  = 1f;
            fd.friction = 0.3f;
            b2Fixture fixture = body.CreateFixture(fd);

            sprite.PhysicsBody = body;
            //_world.SetContactListener(new Myb2Listener());

            // _world.Dump();
        }
예제 #21
0
        public Gears()
        {
            b2Body ground = null;
            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

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

            // Gears co
            {
                b2CircleShape circle1 = new b2CircleShape();
                circle1.Radius = 1.0f;

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(0.5f, 5.0f);

                b2CircleShape circle2 = new b2CircleShape();
                circle2.Radius = 2.0f;

                b2BodyDef bd1 = new b2BodyDef();
                bd1.type = b2BodyType.b2_staticBody;
                bd1.position.Set(10.0f, 9.0f);
                b2Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1, 0.0f);

                b2BodyDef bd2 = new b2BodyDef();
                bd2.type = b2BodyType.b2_dynamicBody;
                bd2.position.Set(10.0f, 8.0f);
                b2Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(box, 5.0f);

                b2BodyDef bd3 = new b2BodyDef();
                bd3.type = b2BodyType.b2_dynamicBody;
                bd3.position.Set(10.0f, 6.0f);
                b2Body body3 = m_world.CreateBody(bd3);
                body3.CreateFixture(circle2, 5.0f);

                b2RevoluteJointDef jd1 = new b2RevoluteJointDef();
                jd1.Initialize(body2, body1, bd1.position);
                b2Joint joint1 = m_world.CreateJoint(jd1);

                b2RevoluteJointDef jd2 = new b2RevoluteJointDef();
                jd2.Initialize(body2, body3, bd3.position);
                b2Joint joint2 = m_world.CreateJoint(jd2);

                b2GearJointDef jd4 = new b2GearJointDef();
                jd4.BodyA  = body1;
                jd4.BodyB  = body3;
                jd4.joint1 = joint1;
                jd4.joint2 = joint2;
                jd4.ratio  = circle2.Radius / circle1.Radius;
                m_world.CreateJoint(jd4);
            }

            {
                b2CircleShape circle1 = new b2CircleShape();
                circle1.Radius = 1.0f;

                b2CircleShape circle2 = new b2CircleShape();
                circle2.Radius = 2.0f;

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(0.5f, 5.0f);

                b2BodyDef bd1 = new b2BodyDef();
                bd1.type = b2BodyType.b2_dynamicBody;
                bd1.position.Set(-3.0f, 12.0f);
                b2Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                b2RevoluteJointDef jd1 = new b2RevoluteJointDef();
                jd1.BodyA          = ground;
                jd1.BodyB          = body1;
                jd1.localAnchorA   = ground.GetLocalPoint(bd1.position);
                jd1.localAnchorB   = body1.GetLocalPoint(bd1.position);
                jd1.referenceAngle = body1.Angle - ground.Angle;
                m_joint1           = (b2RevoluteJoint)m_world.CreateJoint(jd1);

                b2BodyDef bd2 = new b2BodyDef();
                bd2.type = b2BodyType.b2_dynamicBody;
                bd2.position.Set(0.0f, 12.0f);
                b2Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(circle2, 5.0f);

                b2RevoluteJointDef jd2 = new b2RevoluteJointDef();
                jd2.Initialize(ground, body2, bd2.position);
                m_joint2 = (b2RevoluteJoint)m_world.CreateJoint(jd2);

                b2BodyDef bd3 = new b2BodyDef();
                bd3.type = b2BodyType.b2_dynamicBody;
                bd3.position.Set(2.5f, 12.0f);
                b2Body body3 = m_world.CreateBody(bd3);
                body3.CreateFixture(box, 5.0f);

                b2PrismaticJointDef jd3 = new b2PrismaticJointDef();
                jd3.Initialize(ground, body3, bd3.position, new b2Vec2(0.0f, 1.0f));
                jd3.lowerTranslation = -5.0f;
                jd3.upperTranslation = 5.0f;
                jd3.enableLimit      = true;

                m_joint3 = (b2PrismaticJoint)m_world.CreateJoint(jd3);

                b2GearJointDef jd4 = new b2GearJointDef();
                jd4.BodyA  = body1;
                jd4.BodyB  = body2;
                jd4.joint1 = m_joint1;
                jd4.joint2 = m_joint2;
                jd4.ratio  = circle2.Radius / circle1.Radius;
                m_joint4   = (b2GearJoint)m_world.CreateJoint(jd4);

                b2GearJointDef jd5 = new b2GearJointDef();
                jd5.BodyA  = body2;
                jd5.BodyB  = body3;
                jd5.joint1 = m_joint2;
                jd5.joint2 = m_joint3;
                jd5.ratio  = -1.0f / circle2.Radius;
                m_joint5   = (b2GearJoint)m_world.CreateJoint(jd5);
            }
        }
예제 #22
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);
        }
예제 #23
0
        public Pinball()
        {
            // Ground body
            b2Body ground;

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

                b2Vec2[] vs = new b2Vec2[5];
                vs[0].Set(0.0f, -2.0f);
                vs[1].Set(8.0f, 6.0f);
                vs[2].Set(8.0f, 20.0f);
                vs[3].Set(-8.0f, 20.0f);
                vs[4].Set(-8.0f, 6.0f);

                b2ChainShape loop = new b2ChainShape();
                loop.CreateLoop(vs, 5);
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape   = loop;
                fd.density = 0.0f;
                ground.CreateFixture(fd);
            }

            // Flippers
            {
                b2Vec2 p1 = new b2Vec2(-2.0f, 0.0f);
                b2Vec2 p2 = new b2Vec2(2.0f, 0.0f);

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

                bd.position = p1;
                b2Body leftFlipper = m_world.CreateBody(bd);

                bd.position = p2;
                b2Body rightFlipper = m_world.CreateBody(bd);

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(1.75f, 0.1f);

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

                leftFlipper.CreateFixture(fd);
                rightFlipper.CreateFixture(fd);

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.BodyA = ground;
                jd.localAnchorB.SetZero();
                jd.enableMotor    = true;
                jd.maxMotorTorque = 1000.0f;
                jd.enableLimit    = true;

                jd.motorSpeed   = 0.0f;
                jd.localAnchorA = p1;
                jd.BodyB        = leftFlipper;
                jd.lowerAngle   = -30.0f * b2Settings.b2_pi / 180.0f;
                jd.upperAngle   = 5.0f * b2Settings.b2_pi / 180.0f;
                m_leftJoint     = (b2RevoluteJoint)m_world.CreateJoint(jd);

                jd.motorSpeed   = 0.0f;
                jd.localAnchorA = p2;
                jd.BodyB        = rightFlipper;
                jd.lowerAngle   = -5.0f * b2Settings.b2_pi / 180.0f;
                jd.upperAngle   = 30.0f * b2Settings.b2_pi / 180.0f;
                m_rightJoint    = (b2RevoluteJoint)m_world.CreateJoint(jd);
            }

            // Circle character
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(1.0f, 15.0f);
                bd.type   = b2BodyType.b2_dynamicBody;
                bd.bullet = true;

                m_ball = m_world.CreateBody(bd);

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

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

            m_button = false;
        }
예제 #24
0
        public LiquidTest()
        {
            hash = new List <int> [40, 40];
            for (int i = 0; i < 40; ++i)
            {
                for (int j = 0; j < 40; ++j)
                {
                    hash[i, j] = new List <int>();
                }
            }
            hashWidth  = 40;
            hashHeight = 40;

            //if (firstTime)
            //{
            //    setCamera(new Vec2(0, 2), 35f);
            //    firstTime = false;
            //}

            //m_getWorld().setGravity(new Vec2(0.0f,0.0f));

            b2Body ground = null;

            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                ground = m_world.CreateBody(bd);
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(5.0f, 0.5f);
                ground.CreateFixture(shape, 0);

                shape.SetAsBox(1.0f, 0.2f, new b2Vec2(0.0f, 4.0f), -0.2f);
                ground.CreateFixture(shape, 0);
                shape.SetAsBox(1.5f, 0.2f, new b2Vec2(-1.2f, 5.2f), -1.5f);
                ground.CreateFixture(shape, 0);
                shape.SetAsBox(0.5f, 50.0f, new b2Vec2(5.0f, 0.0f), 0.0f);
                ground.CreateFixture(shape, 0);

                shape.SetAsBox(0.5f, 3.0f, new b2Vec2(-8.0f, 0.0f), 0.0f);
                ground.CreateFixture(shape, 0);

                shape.SetAsBox(2.0f, 0.1f, new b2Vec2(-6.0f, -2.8f), 0.1f);
                ground.CreateFixture(shape, 0);

                b2CircleShape cd = new b2CircleShape();
                cd.Radius = 0.5f;
                cd.Position.Set(-0.5f, -4.0f);
                ground.CreateFixture(cd, 0);
            }

            liquid = new b2Body[nParticles];
            float massPerParticle = totalMass / nParticles;
            //		PointDef pd = new PointDef();
            //		pd.mass = massPerParticle;
            //		pd.restitution = 0.0f;
            //		pd.filter.groupIndex = -10;
            b2CircleShape pd = new b2CircleShape();
            b2FixtureDef  fd = new b2FixtureDef();

            fd.shape             = pd;
            fd.density           = 1f;
            fd.filter.groupIndex = -10;
            pd.Radius            = .05f;
            fd.restitution       = 0.4f;
            fd.friction          = 0.0f;
            float cx = 0.0f;
            float cy = 25.0f;

            for (int i = 0; i < nParticles; ++i)
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position = new b2Vec2(Rand.RandomFloat(cx - boxWidth * .5f, cx + boxWidth * .5f),
                                         Rand.RandomFloat(cy - boxHeight * .5f, cy + boxHeight * .5f));
                bd.fixedRotation = true;
                bd.type          = b2BodyType.b2_dynamicBody;
                b2Body b = m_world.CreateBody(bd);

                b.CreateFixture(fd).UserData = LIQUID_INT;

                b2MassData md = new b2MassData();
                md.mass = massPerParticle;
                md.I    = 1.0f;
                b.SetMassData(md);
                b.SetSleepingAllowed(false);
                liquid[i] = b;
            }

            b2PolygonShape polyDef = new b2PolygonShape();

            polyDef.SetAsBox(Rand.RandomFloat(0.3f, 0.7f), Rand.RandomFloat(0.3f, 0.7f));
            b2BodyDef bodyDef = new b2BodyDef();

            bodyDef.position = new b2Vec2(0.0f, 25.0f);
            bodyDef.type     = b2BodyType.b2_dynamicBody;
            bod = m_world.CreateBody(bodyDef);
            bod.CreateFixture(polyDef, 1f);
        }
예제 #25
0
파일: Form1.cs 프로젝트: RubisetCie/box2c
        void MakeBox(b2Vec2 pos, float w, float h)
        {
            // Define the dynamic body. We set its position and call the body factory.
            b2BodyDef bodyDef = new b2BodyDef();
            bodyDef.type = b2BodyType.b2_dynamicBody;
            bodyDef.position = pos;
            var body = world.CreateBody(bodyDef);

            // Define another box shape for our dynamic body.
            b2PolygonShape dynamicBox = new b2PolygonShape();
            dynamicBox.SetAsBox(w, h);

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

            // Set the box density to be non-zero, so it will be dynamic.
            fixtureDef.density = 1.0f;

            // Override the default friction.
            fixtureDef.friction = 0.3f;

            // Add the shape to the body.
            body.CreateFixture(fixtureDef);
        }
예제 #26
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);
            }
        }
예제 #27
0
        public CircleStress()
        {
            b2Body leftWall  = null;
            b2Body rightWall = null;

            {
                // Ground
                b2PolygonShape sd = new b2PolygonShape();
                sd.SetAsBox(50.0f, 10.0f);
                b2BodyDef bd = new b2BodyDef();
                bd.type     = b2BodyType.b2_staticBody;
                bd.position = new b2Vec2(0.0f, -10.0f);
                b2Body       b  = m_world.CreateBody(bd);
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = sd;
                fd.friction = 1.0f;
                b.CreateFixture(fd);

                // Walls
                sd.SetAsBox(3.0f, 50.0f);
                bd          = new b2BodyDef();
                bd.position = new b2Vec2(45.0f, 25.0f);
                rightWall   = m_world.CreateBody(bd);
                rightWall.CreateFixture(sd, 0);
                bd.position = new b2Vec2(-45.0f, 25.0f);
                leftWall    = m_world.CreateBody(bd);
                leftWall.CreateFixture(sd, 0);

                // Corners
                bd = new b2BodyDef();
                sd.SetAsBox(20.0f, 3.0f);
                bd.angle    = (float)(-Math.PI / 4.0);
                bd.position = new b2Vec2(-35f, 8.0f);
                b2Body myBod = m_world.CreateBody(bd);
                myBod.CreateFixture(sd, 0);
                bd.angle    = (float)(Math.PI / 4.0);
                bd.position = new b2Vec2(35f, 8.0f);
                myBod       = m_world.CreateBody(bd);
                myBod.CreateFixture(sd, 0);

                // top
                sd.SetAsBox(50.0f, 10.0f);
                bd.type     = b2BodyType.b2_staticBody;
                bd.angle    = 0;
                bd.position = new b2Vec2(0.0f, 75.0f);
                b           = m_world.CreateBody(bd);
                fd.shape    = sd;
                fd.friction = 1.0f;
                b.CreateFixture(fd);
            }

            {
                b2CircleShape cd;
                b2FixtureDef  fd = new b2FixtureDef();

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                int   numPieces = 5;
                float radius    = 6f;
                bd.position = new b2Vec2(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);
                for (int i = 0; i < numPieces; i++)
                {
                    cd             = new b2CircleShape();
                    cd.Radius      = 1.2f;
                    fd.shape       = cd;
                    fd.density     = 25;
                    fd.friction    = .1f;
                    fd.restitution = .9f;
                    float xPos = radius * (float)Math.Cos(2f * Math.PI * (i / (float)(numPieces)));
                    float yPos = radius * (float)Math.Sin(2f * Math.PI * (i / (float)(numPieces)));
                    cd.Position.Set(xPos, yPos);

                    body.CreateFixture(fd);
                }

                body.SetBullet(false);

                b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                rjd.Initialize(body, m_groundBody, body.Position);
                rjd.motorSpeed     = (float)Math.PI;
                rjd.maxMotorTorque = 1000000.0f;
                rjd.enableMotor    = true;
                joint = (b2RevoluteJoint)m_world.CreateJoint(rjd);
            }

            {
                int loadSize = 41;

                for (int j = 0; j < 15; j++)
                {
                    for (int i = 0; i < loadSize; i++)
                    {
                        b2CircleShape circ = new b2CircleShape();
                        b2BodyDef     bod  = new b2BodyDef();
                        bod.type    = b2BodyType.b2_dynamicBody;
                        circ.Radius = 1.0f + (i % 2 == 0 ? 1.0f : -1.0f) * .5f * Rand.RandomFloat(.5f, 1f);
                        b2FixtureDef fd2 = new b2FixtureDef();
                        fd2.shape       = circ;
                        fd2.density     = circ.Radius * 1.5f;
                        fd2.friction    = 0.5f;
                        fd2.restitution = 0.7f;
                        float xPos = -39f + 2 * i;
                        float yPos = 50f + j;
                        bod.position = new b2Vec2(xPos, yPos);
                        b2Body myBody = m_world.CreateBody(bod);
                        myBody.CreateFixture(fd2);
                    }
                }
            }

            m_world.Gravity = new b2Vec2(0, -50);
        }
예제 #28
0
        public PhysicalTank(StarlingGameSpriteWithTankTextures textures, StarlingGameSpriteWithPhysics Context)
        {
            this.CurrentInput = new KeySample();

            this.textures   = textures;
            this.Context    = Context;
            this.driverseat = new DriverSeat();
            this.visual     = new VisualTank(textures, Context);

            for (int i = 0; i < 7; i++)
            {
                this.KarmaInput0.Enqueue(
                    new KeySample()
                    );
            }


            #region b2world



            {
                var bodyDef = new b2BodyDef();

                bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                // stop moving if legs stop walking!
                bodyDef.linearDamping  = 1.0;
                bodyDef.angularDamping = 8;
                //bodyDef.angle = 1.57079633;
                //bodyDef.fixedRotation = true;

                body = Context.ground_b2world.CreateBody(bodyDef);
                //current = body;


                var fixDef = new Box2D.Dynamics.b2FixtureDef();
                fixDef.density     = 0.1;
                fixDef.friction    = 0.01;
                fixDef.restitution = 0;

                var fixdef_shape = new b2PolygonShape();

                fixDef.shape = fixdef_shape;

                // physics unit is looking to right
                fixdef_shape.SetAsBox(3, 2);



                var fix = body.CreateFixture(fixDef);

                var fix_data = new Action <double>(
                    jeep_forceA =>
                {
                    if (jeep_forceA < 1)
                    {
                        return;
                    }

                    Context.oncollision(this, jeep_forceA);
                }
                    );
                fix.SetUserData(fix_data);
            }


            {
                var bodyDef = new b2BodyDef();

                bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                // stop moving if legs stop walking!
                bodyDef.linearDamping  = 1.0;
                bodyDef.angularDamping = 8;
                //bodyDef.angle = 1.57079633;
                //bodyDef.fixedRotation = true;

                karmabody = Context.groundkarma_b2world.CreateBody(bodyDef);
                //current = body;


                var fixDef = new Box2D.Dynamics.b2FixtureDef();
                fixDef.density     = 0.1;
                fixDef.friction    = 0.01;
                fixDef.restitution = 0;

                var fixdef_shape = new b2PolygonShape();

                fixDef.shape = fixdef_shape;

                // physics unit is looking to right
                fixdef_shape.SetAsBox(3, 2);



                var fix = karmabody.CreateFixture(fixDef);
            }


            #endregion

            #region b2world



            {
                var bodyDef = new b2BodyDef();

                bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                // stop moving if legs stop walking!
                bodyDef.linearDamping  = 1.0;
                bodyDef.angularDamping = 8;
                //bodyDef.angle = 1.57079633;
                //bodyDef.fixedRotation = true;

                damagebody = Context.damage_b2world.CreateBody(bodyDef);
                //current = body;


                var fixDef = new Box2D.Dynamics.b2FixtureDef();
                fixDef.density     = 0.1;
                fixDef.friction    = 0.01;
                fixDef.restitution = 0;

                var fixdef_shape = new b2PolygonShape();

                fixDef.shape = fixdef_shape;

                // physics unit is looking to right
                fixdef_shape.SetAsBox(3, 2);



                var fix = damagebody.CreateFixture(fixDef);

                var fix_data = new Action <double>(
                    jeep_forceA =>
                {
                    if (jeep_forceA < 1)
                    {
                        return;
                    }

                    Context.oncollision(this, jeep_forceA);
                }
                    );
                fix.SetUserData(fix_data);
            }



            #endregion


            Context.internalunits.Add(this);
        }
예제 #29
0
        public PhysicalCannon(StarlingGameSpriteWithCannonTextures textures, StarlingGameSpriteWithPhysics Context)
        {
            this.CurrentInput = new KeySample();
            this.driverseat   = new DriverSeat();
            this.Context      = Context;

            visual = new VisualCannon(textures, Context);

            for (int i = 0; i < 7; i++)
            {
                this.KarmaInput0.Enqueue(
                    new KeySample()
                    );
            }

            {
                //initialize body
                var bdef = new b2BodyDef();
                bdef.angle         = 0;
                bdef.fixedRotation = true;
                this.body          = Context.ground_b2world.CreateBody(bdef);

                //initialize shape
                var fixdef = new b2FixtureDef();

                var shape = new b2PolygonShape();
                fixdef.shape = shape;

                shape.SetAsBox(2, 2);

                fixdef.restitution = 0.4; //positively bouncy!



                var fix = this.body.CreateFixture(fixdef);


                var fix_data = new Action <double>(
                    jeep_forceA =>
                {
                    if (jeep_forceA < 1)
                    {
                        return;
                    }

                    Context.oncollision(this, jeep_forceA);
                }
                    );
                fix.SetUserData(fix_data);
            }

            {
                //initialize body
                var bdef = new b2BodyDef();
                bdef.angle         = 0;
                bdef.fixedRotation = true;
                this.karmabody     = Context.groundkarma_b2world.CreateBody(bdef);

                //initialize shape
                var fixdef = new b2FixtureDef();

                var shape = new b2PolygonShape();
                fixdef.shape = shape;

                shape.SetAsBox(2, 2);

                fixdef.restitution = 0.4; //positively bouncy!



                this.karmabody.CreateFixture(fixdef);
            }

            Context.internalunits.Add(this);
        }
예제 #30
0
        public RopeJoint()
        {
            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));
                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;
                fd.filter.categoryBits = 0x0001;
                fd.filter.maskBits     = 0xFFFF & ~0x0002;

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.CollideConnected = false;

                const int   N = 10;
                const float y = 15.0f;
                m_ropeDef.localAnchorA.Set(0.0f, y);

                b2Body prevBody = ground;
                for (int i = 0; i < N; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.5f + 1.0f * i, y);
                    if (i == N - 1)
                    {
                        shape.SetAsBox(1.5f, 1.5f);
                        fd.density             = 100.0f;
                        fd.filter.categoryBits = 0x0002;
                        bd.position.Set(1.0f * i, y);
                        bd.angularDamping = 0.4f;
                    }

                    b2Body body = m_world.CreateBody(bd);

                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }

                m_ropeDef.localAnchorB.SetZero();

                float extraLength = 0.01f;
                m_ropeDef.maxLength = N - 1.0f + extraLength;
                m_ropeDef.BodyB     = prevBody;
            }

            {
                m_ropeDef.BodyA = ground;
                m_rope          = m_world.CreateJoint(m_ropeDef);
            }
        }
예제 #31
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);
            }
        }