コード例 #1
0
ファイル: Form1.cs プロジェクト: prepare/box2c
        void MakeCircle(b2Vec2 pos, float radius)
        {
            // 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.
            b2CircleShape dynamicBox = new b2CircleShape();

            dynamicBox.m_radius = radius;

            // 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);
        }
コード例 #2
0
ファイル: b2Fixture.cs プロジェクト: Cr33zz/Box2DSharp
    // We need separation create/destroy functions from the constructor/destructor because
    // the destructor cannot access the allocator (no destructor arguments allowed by C++).
    internal void Create(b2Body body, b2FixtureDef def)
    {
        m_userData    = def.userData;
        m_friction    = def.friction;
        m_restitution = def.restitution;

        m_body = body;
        m_next = null;

        m_filter = def.filter;

        m_isSensor = def.isSensor;

        m_shape = def.shape.Clone();

        // Reserve proxy space
        int childCount = m_shape.GetChildCount();

        m_proxies = Arrays.InitializeWithDefaultInstances <b2FixtureProxy>(childCount);
        for (int i = 0; i < childCount; ++i)
        {
            m_proxies[i]         = new b2FixtureProxy();
            m_proxies[i].fixture = null;
            m_proxies[i].proxyId = (int)b2BroadPhase.AnonymousEnum.e_nullProxy;
        }
        m_proxyCount = 0;

        m_density = def.density;
    }
コード例 #3
0
        public VaryingRestitution()
        {
            {
                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);
            }

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

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

                float[] restitution = { 0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f };

                for (int i = 0; i < 7; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-10.0f + 3.0f * i, 20.0f);

                    b2Body body = m_world.CreateBody(bd);

                    fd.restitution = restitution[i];
                    body.CreateFixture(fd);
                }
            }
        }
コード例 #4
0
        void CreateNinja()
        {
            spriteImageName = String.Format("{0}_standing", baseImageName);

            onGround = false;

            // Define the dynamic body.
            var bodyDef = new b2BodyDef();

            bodyDef.type = b2BodyType.b2_staticBody;             //or you could use b2DynamicBody to start the ninja as dynamic

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

            var shape          = new b2CircleShape();
            var radiusInMeters = (40 / Constants.PTM_RATIO) * 0.5f;             //increase or decrease 40 for a different circle size definition

            shape.Radius = radiusInMeters;


            // 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);
        }
コード例 #5
0
ファイル: GameObject.cs プロジェクト: tuita520/heroesrpg
        /// <summary>
        ///
        /// </summary>
        /// <param name="world"></param>
        public virtual void CreatePhysicsBody(b2World world, int ptm)
        {
            PtmRatio = ptm;

            PhysicsBodyDef               = new b2BodyDef();
            PhysicsBodyDef.position      = InitialPosition;
            PhysicsBodyDef.type          = BodyType;
            PhysicsBodyDef.fixedRotation = FixedRotation;
            PhysicsBodyDef.gravityScale  = GravityScale;
            PhysicsBodyDef.linearDamping = LinearDamping;
            PhysicsBodyDef.bullet        = Bullet;

            PhysicsBody = world.CreateBody(PhysicsBodyDef);

            PhysicsBody.Mass = Mass;
            PhysicsBody.ResetMassData();

            var fixtureDef = new b2FixtureDef();

            fixtureDef.shape    = CreatePhysicsShape();
            fixtureDef.density  = Density;
            fixtureDef.friction = Friction;

            PhysicsBodyFixture = PhysicsBody.CreateFixture(fixtureDef);

            PositionX = GetMeterToPoint(InitialPosition.x);
            PositionY = GetMeterToPoint(InitialPosition.y);
        }
コード例 #6
0
ファイル: b2Body.cs プロジェクト: Deusald/SharpBox2D
        public b2Fixture CreateFixture(b2FixtureDef def)
        {
            global::System.IntPtr cPtr = Box2dPINVOKE.b2Body_CreateFixture__SWIG_0(swigCPtr, b2FixtureDef.getCPtr(def));
            b2Fixture             ret  = (cPtr == global::System.IntPtr.Zero) ? null : new b2Fixture(cPtr, false);

            return(ret);
        }
コード例 #7
0
        void InitPhysics()
        {
            CCSize size = Layer.VisibleBoundsWorldspace.Size;

            var gravity = new b2Vec2(0.0f, -10.0f);

            world = new b2World(gravity);

            world.SetAllowSleeping(true);
            world.SetContinuousPhysics(true);

            var def = new b2BodyDef();

            def.allowSleep = true;
            def.position   = b2Vec2.Zero;
            def.type       = b2BodyType.b2_staticBody;
            b2Body groundBody = world.CreateBody(def);

            groundBody.SetActive(true);

            b2EdgeShape groundBox = new b2EdgeShape();

            groundBox.Set(b2Vec2.Zero, new b2Vec2(size.Width / PTM_RATIO, 0));
            b2FixtureDef fd = new b2FixtureDef();

            fd.shape = groundBox;
            groundBody.CreateFixture(fd);
        }
コード例 #8
0
        void InitPhysics()
        {
            var gravity = new b2Vec2(0.0f, -10.0f);

            world = new b2World(gravity);

            world.SetAllowSleeping(true);
            world.SetContinuousPhysics(true);

            var def = new b2BodyDef();

            def.allowSleep = true;
            def.position   = b2Vec2.Zero;
            def.type       = b2BodyType.b2_staticBody;

            b2Body groundBody = world.CreateBody(def);

            groundBody.SetActive(true);

            b2EdgeShape groundBox = new b2EdgeShape();

            groundBox.Set(b2Vec2.Zero, new b2Vec2(900, 100));

            b2FixtureDef fd = new b2FixtureDef();

            fd.friction    = 0.3f;
            fd.restitution = 0.1f;
            fd.shape       = groundBox;

            groundBody.CreateFixture(fd);
        }
コード例 #9
0
        void AddBall()
        {
            int idx    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
            int idy    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
            var sprite = new CCPhysicsSprite(ballTexture, new CCRect(32 * idx, 32 * idy, 32, 32), PTM_RATIO);

            ballsBatch.AddChild(sprite);

            CCPoint p = GetRandomPosition(sprite.ContentSize);

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

            def.position       = new b2Vec2(p.X / PTM_RATIO, p.Y / PTM_RATIO);
            def.linearVelocity = new b2Vec2(0.0f, -1.0f);
            def.type           = b2BodyType.b2_dynamicBody;
            b2Body body = world.CreateBody(def);

            var circle = new b2CircleShape();

            circle.Radius = 0.5f;

            var fd = new b2FixtureDef();

            fd.shape       = circle;
            fd.density     = 1f;
            fd.restitution = 0.85f;
            fd.friction    = 0f;
            body.CreateFixture(fd);

            sprite.PhysicsBody = body;

            Console.WriteLine("sprite batch node count = {0}", ballsBatch.ChildrenCount);
        }
コード例 #10
0
        public override void Keyboard(char key)
        {
            switch (key)
            {
            case ',':
                if (m_bullet != null)
                {
                    m_world.DestroyBody(m_bullet);
                    m_bullet = null;
                }
                {
                    b2CircleShape shape = new b2CircleShape();
                    shape.Radius = 0.25f;

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

                    b2BodyDef bd = new b2BodyDef();
                    bd.type   = b2BodyType.b2_dynamicBody;
                    bd.bullet = true;
                    bd.position.Set(-31.0f, 5.0f);

                    m_bullet = m_world.CreateBody(bd);
                    m_bullet.CreateFixture(fd);

                    m_bullet.LinearVelocity = new b2Vec2(400.0f, 0.0f);
                }
                break;
            }
        }
コード例 #11
0
ファイル: GameLayer.cs プロジェクト: svetlanatanasov/jusTap
        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();
        }
コード例 #12
0
        private void createWithCollider2d(Collider2D coll)
        {
            b2FixtureDef      fixtureDef = new b2FixtureDef();
            PhysicsMaterial2D material   = coll.sharedMaterial;

            if (material != null)
            {
                fixtureDef.restitution = material.bounciness;
                fixtureDef.friction    = material.friction;
            }
            fixtureDef.isSensor = coll.isTrigger;

            if (coll is BoxCollider2D)
            {
                BoxCollider2D  boxColl = coll as BoxCollider2D;
                b2PolygonShape s       = b2PolygonShape.AsOrientedBox(boxColl.size.x * 0.5f,
                                                                      boxColl.size.y * 0.5f,
                                                                      new b2Vec2(boxColl.offset.x, boxColl.offset.y),
                                                                      0 /*transform.eulerAngles.z*Mathf.Deg2Rad*/);
                scaleShape(s);
                fixtureDef.shape   = s;
                _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) };
            }
            else if (coll is CircleCollider2D)
            {
                CircleCollider2D circleColl = coll as CircleCollider2D;
                b2CircleShape    s          = new b2CircleShape(circleColl.radius);
                s.SetLocalPosition(new b2Vec2(circleColl.offset.x, circleColl.offset.y));
                scaleShape(s);
                fixtureDef.shape   = s;
                _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) };
            }
            else if (coll is PolygonCollider2D)
            {
                int i, j;
                PolygonCollider2D polyColl = coll as PolygonCollider2D;

                List <b2Fixture> fixtureList = new List <b2Fixture>();
                int pathCount = polyColl.pathCount;
                for (i = 0; i < pathCount; i++)
                {
                    Vector2[] path     = polyColl.GetPath(i);
                    b2Vec2[]  vertices = new b2Vec2[path.Length];
                    for (j = 0; j < path.Length; j++)
                    {
                        vertices[j] = new b2Vec2(path[j].x, path[j].y);
                    }
                    b2Separator sep      = new b2Separator();
                    b2Fixture[] fixtures = sep.Separate(_body, fixtureDef, vertices, 100, polyColl.offset.x, polyColl.offset.y);             //必须放大100倍进行计算
                    for (j = 0; j < fixtures.Length; j++)
                    {
                        scaleShape(fixtures[j].GetShape());
                    }
                    fixtureList.AddRange(fixtures);
                }
                _fixtureDict[coll] = fixtureList.ToArray();
            }
        }
コード例 #13
0
ファイル: b2Body.cs プロジェクト: okebaybi/Box2DSharp
    /// Creates a fixture from a shape and attach it to this body.
    /// This is a convenience function. Use b2FixtureDef if you need to set parameters
    /// like friction, restitution, user data, or filtering.
    /// If the density is non-zero, this function automatically updates the mass of the body.
    /// @param shape the shape to be cloned.
    /// @param density the shape density (set to zero for static bodies).
    /// @warning This function is locked during callbacks.
    public b2Fixture CreateFixture(b2Shape shape, float density)
    {
        b2FixtureDef def = new b2FixtureDef();

        def.shape   = shape;
        def.density = density;

        return(CreateFixture(def));
    }
コード例 #14
0
        static void LHSetupb2FixtureWithInfo(b2FixtureDef fixture, PlistDictionary dict)
        {
            fixture.density     = dict ["density"].AsFloat;
            fixture.friction    = dict ["friction"].AsFloat;
            fixture.restitution = dict ["restitution"].AsFloat;
            fixture.isSensor    = dict["sensor"].AsBool;

            fixture.filter.maskBits     = (ushort)dict ["mask"].AsInt;
            fixture.filter.categoryBits = (ushort)dict ["category"].AsInt;
        }
コード例 #15
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);
        }
コード例 #16
0
        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);
                    }
                }
            }
        }
コード例 #17
0
        public Confined()
        {
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();

                // Floor
                shape.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);

                // Left wall
                shape.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(-10.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);

                // Right wall
                shape.Set(new b2Vec2(10.0f, 0.0f), new b2Vec2(10.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);

                // Roof
                shape.Set(new b2Vec2(-10.0f, 20.0f), new b2Vec2(10.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            float         radius = 0.5f;
            b2CircleShape shape1 = new b2CircleShape();

            shape1.Position = b2Vec2.Zero;
            shape1.Radius   = radius;

            b2FixtureDef fd = new b2FixtureDef();

            fd.shape    = shape1;
            fd.density  = 1.0f;
            fd.friction = 0.1f;

            for (int j = 0; j < e_columnCount; ++j)
            {
                for (int i = 0; i < e_rowCount; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius);
                    b2Body body = m_world.CreateBody(bd);

                    body.CreateFixture(fd);
                }
            }

            m_world.Gravity = new b2Vec2(0.0f, 0.0f);
        }
コード例 #18
0
        public SensorTest()
        {
            {
                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);
                }

#if false
                {
                    b2FixtureDef sd = new b2FixtureDef();
                    sd.SetAsBox(10.0f, 2.0f, new b2Vec2(0.0f, 20.0f), 0.0f);
                    sd.isSensor = true;
                    m_sensor    = ground.CreateFixture(sd);
                }
#else
                {
                    b2CircleShape shape = new b2CircleShape();
                    shape.Radius   = 5.0f;
                    shape.Position = new b2Vec2(0.0f, 10.0f);

                    b2FixtureDef fd = new b2FixtureDef();
                    fd.shape    = shape;
                    fd.isSensor = true;
                    m_sensor    = ground.CreateFixture(fd);
                }
#endif
            }

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

                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-10.0f + 3.0f * i, 20.0f);
                    bd.userData = i; //  m_touching[i];

                    m_touching[i] = false;
                    m_bodies[i]   = m_world.CreateBody(bd);

                    m_bodies[i].CreateFixture(shape, 1.0f);
                }
            }
        }
コード例 #19
0
        public PhysicalShapeEditor(Rect frameRect)
        {
            InitializeComponent();
            this.fixtureDef = new b2FixtureDef(IntPtr.Zero);
            this.propertyGrid.SelectedObject = this.fixtureDef;
            this.rect = frameRect;

            this.circleShape             = new b2CircleShape(IntPtr.Zero);
            this.polygonShape            = new b2PolygonShape(IntPtr.Zero);
            this.nBox_Rect_H_meter.Value = (decimal)(Math.Abs(rect.GetHeight()) / Scene.meterPixelRatio);
            this.nBox_Rect_W_meter.Value = (decimal)(Math.Abs(rect.GetWidth()) / Scene.meterPixelRatio);
            CreateMohitCircle();
            this.fixtureDef.شکل = this.circleShape;
        }
コード例 #20
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;
        }
コード例 #21
0
        /**
         * Separates a non-convex polygon into convex polygons and adds them as fixtures to the <code>body</code> parameter.<br/>
         * There are some rules you should follow (otherwise you might get unexpected results) :
         * <ul>
         * <li>This class is specifically for non-convex polygons. If you want to create a convex polygon, you don't need to use this class - Box2D's <code>b2PolygonShape</code> class allows you to create convex shapes with the <code>setAsArray()</code>/<code>setAsVector()</code> method.</li>
         * <li>The vertices must be in clockwise order.</li>
         * <li>No three neighbouring points should lie on the same line segment.</li>
         * <li>There must be no overlapping segments and no "holes".</li>
         * </ul> <p/>
         * @param body The b2Body, in which the new fixtures will be stored.
         * @param fixtureDef A b2FixtureDef, containing all the properties (friction, density, etc.) which the new fixtures will inherit.
         * @param verticesVec The vertices of the non-convex polygon, in clockwise order.
         * @param scale <code>[optional]</code> The scale which you use to draw shapes in Box2D. The bigger the scale, the better the precision. The default value is 30.
         * @see b2PolygonShape
         * @see b2PolygonShape.SetAsArray()
         * @see b2PolygonShape.SetAsVector()
         * @see b2Fixture
         * */

        public b2Fixture[] Separate(b2Body body, b2FixtureDef fixtureDef, b2Vec2[] verticesVec, float scale = 100, float offsetX = 0, float offsetY = 0)
        {
            int            i;
            int            n = verticesVec.Length;
            int            j;
            int            m;
            List <b2Vec2>  vec = new List <b2Vec2>();
            ArrayList      figsVec;
            b2PolygonShape polyShape;

            for (i = 0; i < n; i++)
            {
                vec.Add(new b2Vec2((verticesVec[i].x + offsetX) * scale, (verticesVec[i].y + offsetY) * scale));
            }
            if (!getIsConvexPolygon(vec, vec.Count))
            {
                int validate = Validate(vec);
                if (validate == 2 || validate == 3)
                {
                    vec.Reverse();                    //add by kingBook 2017.2.14
                }
            }
            //Debug.Log(string.Format("Validate:{0}",Validate(vec)));
            figsVec = calcShapes(vec);
            n       = figsVec.Count;



            b2Fixture[] fixtures = new b2Fixture[n];

            for (i = 0; i < n; i++)
            {
                vec = (List <b2Vec2>)figsVec[i];
                vec.Reverse();//add by kingBook 2017.2.14
                m           = vec.Count;
                verticesVec = new b2Vec2[m];
                for (j = 0; j < m; j++)
                {
                    verticesVec[j] = new b2Vec2(vec[j].x / scale, vec[j].y / scale);
                }

                polyShape = new b2PolygonShape();
                polyShape.SetAsArray(verticesVec, m);
                fixtureDef.shape = polyShape;
                fixtures[i]      = body.CreateFixture(fixtureDef);
            }
            return(fixtures);
        }
コード例 #22
0
ファイル: BaseMain.cs プロジェクト: kingBook/as3_framework
    protected b2Body createCircle(float radius, 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);

        b2CircleShape s           = new b2CircleShape(radius / ptm_ratio);
        b2FixtureDef  fixtrureDef = new b2FixtureDef();

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

        return(body);
    }
コード例 #23
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;
            }
        }
コード例 #24
0
ファイル: LiquidTest.cs プロジェクト: krashman/cocos2d-xna
        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);
            }
        }
コード例 #25
0
        private void AddBall()
        {
            //Ball
            float ballDiameter = _gameWidth * BALL_RADIUS_PERCENT;
            float ballCenterX  = _gameWidth * BALL_X_PERCENT;
            float ballCenterY  = ballDiameter / 2 + BALL_Y_MARGIN;

            var def = new b2BodyDef();

            def.position = new b2Vec2(ballCenterX / App.PTM_RATIO, ballCenterY / App.PTM_RATIO);
            def.type     = b2BodyType.b2_dynamicBody;
            b2Body physBody = _world.CreateBody(def);
            //Circle Physics Shape
            var shape = new b2CircleShape();

            shape.Radius = ballDiameter / 2 / App.PTM_RATIO;

            var fd = new b2FixtureDef();

            fd.shape       = shape;
            fd.density     = 1f;
            fd.restitution = BALL_RECOIL;
            fd.friction    = BALL_FRICTION;
            physBody.CreateFixture(fd);
            _contactListener.Ball = physBody;

            CCTexture2D ballTex = null;

            if (string.IsNullOrWhiteSpace(_ballFilename))
            {
                ballTex = new CCTexture2D("soccer");
            }
            else
            {
                FileStream fs = new FileStream(_ballFilename, FileMode.Open);
                ballTex = new CCTexture2D(fs);
            }
            _ballN = new CCPhysicsSprite(ballTex, physBody)
            {
                PositionX   = ballCenterX,
                PositionY   = ballCenterY,
                ContentSize = new CCSize(ballDiameter, ballDiameter)
            };
            AddChild(_ballN);
        }
コード例 #26
0
        public void createCircleWithDictionary(PlistDictionary dict, b2Body body, CCNode node, LHScene scene, CCSize size)
        {
            _shapeID   = dict ["shapeID"].AsInt;
            _shapeName = dict ["name"].AsString;

            b2CircleShape shape = new b2CircleShape();

            shape.Radius = size.Width * 0.5f;

            b2FixtureDef fixture = new b2FixtureDef();

            LHSetupb2FixtureWithInfo(fixture, dict);

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

            body.CreateFixture(fixture);
        }
コード例 #27
0
        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);
        }
コード例 #28
0
ファイル: BaseMain.cs プロジェクト: kingBook/as3_framework
    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);
    }
コード例 #29
0
ファイル: PolyShapes.cs プロジェクト: womandroid/cocos2d-xna
        private void Create(int index)
        {
            if (m_bodies[m_bodyIndex] != null)
            {
                m_world.DestroyBody(m_bodies[m_bodyIndex]);
                m_bodies[m_bodyIndex] = null;
            }

            b2BodyDef bd = new b2BodyDef();

            bd.type = b2BodyType.b2_dynamicBody;

            float x = Rand.RandomFloat(-2.0f, 2.0f);

            bd.position.Set(x, 10.0f);
            bd.angle = Rand.RandomFloat(-b2Settings.b2_pi, b2Settings.b2_pi);

            if (index == 4)
            {
                bd.angularDamping = 0.02f;
            }

            m_bodies[m_bodyIndex] = m_world.CreateBody(bd);

            if (index < 4)
            {
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = m_polygons[index];
                fd.density  = 1.0f;
                fd.friction = 0.3f;
                m_bodies[m_bodyIndex].CreateFixture(fd);
            }
            else
            {
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = m_circle;
                fd.density  = 1.0f;
                fd.friction = 0.3f;

                m_bodies[m_bodyIndex].CreateFixture(fd);
            }

            m_bodyIndex = (m_bodyIndex + 1) % k_maxBodies;
        }
コード例 #30
0
ファイル: RayCast.cs プロジェクト: codersamli/cocos2d-xna
        public void Create(int index)
        {
            if (m_bodies[m_bodyIndex] != null)
            {
                m_world.DestroyBody(m_bodies[m_bodyIndex]);
                m_bodies[m_bodyIndex] = null;
            }

            b2BodyDef bd = new b2BodyDef();

            float x = Rand.RandomFloat(-10.0f, 10.0f);
            float y = Rand.RandomFloat(0.0f, 20.0f);

            bd.position.Set(x, y);
            bd.angle = Rand.RandomFloat(-b2Settings.b2_pi, b2Settings.b2_pi);

            m_userData[m_bodyIndex] = index;
            bd.userData             = m_userData[m_bodyIndex];

            if (index == 4)
            {
                bd.angularDamping = 0.02f;
            }

            m_bodies[m_bodyIndex] = m_world.CreateBody(bd);

            if (index < 4)
            {
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = m_polygons[index];
                fd.friction = 0.3f;
                m_bodies[m_bodyIndex].CreateFixture(fd);
            }
            else
            {
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = m_circle;
                fd.friction = 0.3f;

                m_bodies[m_bodyIndex].CreateFixture(fd);
            }

            m_bodyIndex = (m_bodyIndex + 1) % e_maxBodies;
        }
コード例 #31
0
ファイル: Form1.cs プロジェクト: RubisetCie/box2c
        void MakeCircle(b2Vec2 pos, float radius)
        {
            // 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.
            b2CircleShape dynamicBox = new b2CircleShape();
            dynamicBox.m_radius = radius;

            // 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);
        }