コード例 #1
0
ファイル: b2ChainShape.cs プロジェクト: homocury/cocos2d-xna
 public b2ChainShape(b2ChainShape clone)
     : base((b2Shape)clone)
 {
     CreateChain(clone.m_vertices, m_count);
     PrevVertex = clone.PrevVertex;
     NextVertex = clone.NextVertex;
     m_hasPrevVertex = clone.m_hasPrevVertex;
     m_hasNextVertex = clone.m_hasNextVertex;
 }
コード例 #2
0
 public b2ChainShape(b2ChainShape clone)
     : base((b2Shape)clone)
 {
     CreateChain(clone.m_vertices, m_count);
     PrevVertex      = clone.PrevVertex;
     NextVertex      = clone.NextVertex;
     m_hasPrevVertex = clone.m_hasPrevVertex;
     m_hasNextVertex = clone.m_hasNextVertex;
 }
コード例 #3
0
ファイル: b2ChainShape.cs プロジェクト: krashman/cocos2d-xna
 public b2ChainShape(b2ChainShape clone)
     : base((b2Shape)clone)
 {
     CreateChain(clone.Vertices, clone.Count);
     PrevVertex    = clone.PrevVertex;
     NextVertex    = clone.NextVertex;
     HasPrevVertex = clone.HasPrevVertex;
     HasNextVertex = clone.HasNextVertex;
 }
コード例 #4
0
ファイル: b2ChainShape.cs プロジェクト: h7ing/CocosSharp
 public b2ChainShape(b2ChainShape clone)
     : base((b2Shape)clone)
 {
     CreateChain(clone.Vertices, clone.Count);
     PrevVertex = clone.PrevVertex;
     NextVertex = clone.NextVertex;
     HasPrevVertex = clone.HasPrevVertex;
     HasNextVertex = clone.HasNextVertex;
 }
コード例 #5
0
        public override b2Shape Clone()
        {
            b2ChainShape clone = new b2ChainShape(this);

            return(clone);
        }
コード例 #6
0
ファイル: b2ChainShape.cs プロジェクト: homocury/cocos2d-xna
 public override b2Shape Clone()
 {
     b2ChainShape clone = new b2ChainShape(this);
     return clone;
 }
コード例 #7
0
ファイル: Pinball.cs プロジェクト: h7ing/CocosSharp
        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;
        }
コード例 #8
0
ファイル: Nb2dJson.cs プロジェクト: netonjm/RubeLoader
        // REVISADO =====================================================================
        b2Fixture j2b2Fixture(b2Body body, JObject fixtureValue)
        {
            if (null == fixtureValue)
                return null;

            b2FixtureDef fixtureDef = new b2FixtureDef();

            //Fixture fixtureDef = new Fixture();
            fixtureDef.restitution = jsonToFloat("restitution", fixtureValue);
            fixtureDef.friction = jsonToFloat("friction", fixtureValue);
            fixtureDef.density = jsonToFloat("density", fixtureValue);
            fixtureDef.isSensor = fixtureValue["sensor"] == null ? false : (bool)fixtureValue["sensor"];

            fixtureDef.filter.categoryBits = (fixtureValue["filter-categoryBits"] == null) ? (ushort)0x0001 : (ushort)fixtureValue["filter-categoryBits"];

            fixtureDef.filter.maskBits = fixtureValue["filter-maskBits"] == null ? (ushort)0xffff : (ushort)fixtureValue["filter-maskBits"];

            fixtureDef.filter.groupIndex = fixtureValue["filter-groupIndex"] == null ? (short)0 : (short)fixtureValue["filter-groupIndex"];

            b2Fixture fixture = null;

            if (null != fixtureValue["circle"])
            {
                JObject circleValue = (JObject)fixtureValue["circle"];

                b2CircleShape circleShape = new b2CircleShape();

                circleShape.Radius = jsonToFloat("radius", circleValue);
                circleShape.Position = jsonToVec("center", circleValue);

                fixtureDef.shape = circleShape;
                fixture = body.CreateFixture(fixtureDef);
            }
            else if (null != fixtureValue["edge"])
            {

                JObject edgeValue = (JObject)fixtureValue["edge"];
                b2EdgeShape edgeShape = new b2EdgeShape();

                edgeShape.Vertex1 = jsonToVec("vertex1", edgeValue);
                edgeShape.Vertex2 = jsonToVec("vertex2", edgeValue);

                edgeShape.HasVertex0 = edgeValue["hasVertex0"] == null ? false : (bool)edgeValue["hasVertex0"];
                edgeShape.HasVertex3 = edgeValue["hasVertex3"] == null ? false : (bool)edgeValue["hasVertex3"];

                if (edgeShape.HasVertex0)
                    edgeShape.Vertex0 = jsonToVec("vertex0", edgeValue);
                if (edgeShape.HasVertex3)
                    edgeShape.Vertex3 = jsonToVec("vertex3", edgeValue);
                fixtureDef.shape = edgeShape;

                fixture = body.CreateFixture(fixtureDef);
            }
            else if (null != fixtureValue["loop"])
            {// support old
                // format (r197)

                JObject chainValue = (JObject)fixtureValue["loop"];
                b2ChainShape chainShape = new b2ChainShape();

                int numVertices = ((JArray)chainValue["x"]).Count;

                b2Vec2[] vertices = new b2Vec2[numVertices];
                for (int i = 0; i < numVertices; i++)
                    vertices[i] = jsonToVec("vertices", chainValue, i);

                chainShape.CreateLoop(vertices, numVertices);

                fixtureDef.shape = chainShape;
                fixture = body.CreateFixture(fixtureDef);

            }
            else if (null != fixtureValue["chain"])
            {

                // FPE. See http://www.box2d.org/forum/viewtopic.php?f=4&t=7973&p=35363

                JObject chainValue = (JObject)fixtureValue["chain"];
                b2ChainShape chainShape = new b2ChainShape();
                int numVertices = ((JArray)chainValue["vertices"]["x"]).Count;
                var vertices = new b2Vec2[numVertices];

                for (int i = 0; i < numVertices; i++)
                    vertices[i] = jsonToVec("vertices", chainValue, i);

                chainShape.CreateChain(vertices, numVertices);
                chainShape.HasPrevVertex = chainValue["hasPrevVertex"] == null ? false : (bool)chainValue["hasPrevVertex"];
                chainShape.HasNextVertex = chainValue["hasNextVertex"] == null ? false : (bool)chainValue["hasNextVertex"];

                if (chainShape.HasPrevVertex)
                    chainShape.PrevVertex = (jsonToVec("prevVertex", chainValue));
                if (chainShape.HasNextVertex)
                    chainShape.NextVertex = (jsonToVec("nextVertex", chainValue));

                fixtureDef.shape = chainShape;
                fixture = body.CreateFixture(fixtureDef);

            }
            else if (null != fixtureValue["polygon"])
            {

                JObject polygonValue = (JObject)fixtureValue["polygon"];
                b2Vec2[] vertices = new b2Vec2[b2Settings.b2_maxPolygonVertices];

                int numVertices = ((JArray)polygonValue["vertices"]["x"]).Count;
                if (numVertices > b2Settings.b2_maxPolygonVertices)
                {
                    Console.WriteLine("Ignoring polygon fixture with too many vertices.");
                }
                else if (numVertices < 2)
                {
                    Console.WriteLine("Ignoring polygon fixture less than two vertices.");
                }
                else if (numVertices == 2)
                {
                    Console.WriteLine("Creating edge shape instead of polygon with two vertices.");

                    b2EdgeShape edgeShape = new b2EdgeShape();
                    edgeShape.Vertex1 = jsonToVec("vertices", polygonValue, 0);
                    edgeShape.Vertex2 = jsonToVec("vertices", polygonValue, 1);
                    fixtureDef.shape = edgeShape;
                    fixture = body.CreateFixture(fixtureDef);

                }
                else
                {

                    b2PolygonShape polygonShape = new b2PolygonShape();
                    for (int i = 0; i < numVertices; i++)
                        vertices[i] = jsonToVec("vertices", polygonValue, i);
                    polygonShape.Set(vertices, numVertices);
                    fixtureDef.shape = polygonShape;
                    fixture = body.CreateFixture(fixtureDef);
                }
            }

            String fixtureName = fixtureValue["name"] == null ? "" : fixtureValue["name"].ToString();
            if (fixtureName != "")
            {
                SetFixtureName(fixture, fixtureName);
            }

            return fixture;
        }
コード例 #9
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);
            }
        }
コード例 #10
0
        public virtual b2Shape Clone()
        {
            b2ChainShape clone = new b2ChainShape(this);

            return(clone);
        }
コード例 #11
0
 public virtual b2Shape Clone()
 {
     b2ChainShape clone = new b2ChainShape(this);
     return clone;
 }