コード例 #1
0
ファイル: Chain.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }

            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.createFixture(shape, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.6f, 0.125f);

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

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.collideConnected = false;

                float y = 25.0f;
                Body prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(0.5f + i, y);
                    Body body = getWorld().createBody(bd);
                    body.createFixture(fd);

                    Vec2 anchor = new Vec2(i, y);
                    jd.initialize(prevBody, body, anchor);
                    getWorld().createJoint(jd);

                    prevBody = body;
                }
            }
        }
コード例 #2
0
ファイル: ConfinedTest.cs プロジェクト: Nomad1/sharpbox2d
        public void createCircle()
        {
            float radius = 2.0f;
            CircleShape shape = new CircleShape();
            shape.m_p.setZero();
            shape.m_radius = radius;

            FixtureDef fd = new FixtureDef();
            fd.shape = shape;
            fd.density = 1.0f;
            fd.friction = 0.0f;

            Vec2 p = new Vec2((float) _random.NextDouble(), 3.0f + (float) _random.NextDouble());
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;
            bd.position = p;
            //bd.allowSleep = false;
            Body body = getWorld().createBody(bd);

            body.createFixture(fd);
        }
コード例 #3
0
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.createFixture(shape, 0.0f);
            }

            {
                CircleShape shape = new CircleShape();
                shape.m_radius = 1.0f;

                FixtureDef fd = new FixtureDef();
                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)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(-10.0f + 3.0f*i, 20.0f);

                    Body body = getWorld().createBody(bd);

                    fd.restitution = restitution[i];
                    body.createFixture(fd);
                }
            }
        }
コード例 #4
0
ファイル: MotorTest.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            {
                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));
                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                getGroundBody().createFixture(fd);
            }

            // Define motorized body
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(0.0f, 8.0f);
                Body body = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(2.0f, 0.5f);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.friction = 0.6f;
                fd.density = 2.0f;
                body.createFixture(fd);

                MotorJointDef mjd = new MotorJointDef();
                mjd.initialize(getGroundBody(), body);
                mjd.maxForce = 1000.0f;
                mjd.maxTorque = 1000.0f;
                m_joint = (MotorJoint) m_world.createJoint(mjd);
            }

            m_go = false;
            m_time = 0.0f;
        }
コード例 #5
0
ファイル: ConveyorBelt.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            // Ground
            {

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));
                getGroundBody().createFixture(shape, 0.0f);
            }

            // Platform
            {
                BodyDef bd = new BodyDef();
                bd.position.set(-5.0f, 5.0f);
                Body body = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(10.0f, 0.5f);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.friction = 0.8f;
                m_platform = body.createFixture(fd);
            }

            // Boxes
            for (int i = 0; i < 5; ++i)
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(-10.0f + 2.0f*i, 7.0f);
                Body body = m_world.createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.5f);
                body.createFixture(shape, 20.0f);
            }
        }
コード例 #6
0
ファイル: SensorTest.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            for (int i = 0; i < m_touching.Length; i++)
            {
                m_touching[i] = new BoolWrapper();
            }

            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                {
                    EdgeShape shape = new EdgeShape();
                    shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                    ground.createFixture(shape, 0.0f);
                }

                {
                    CircleShape shape = new CircleShape();
                    shape.m_radius = 5.0f;
                    shape.m_p.set(0.0f, 10.0f);

                    FixtureDef fd = new FixtureDef();
                    fd.shape = shape;
                    fd._isSensor = true;
                    m_sensor = ground.createFixture(fd);
                }
            }

            {
                CircleShape shape = new CircleShape();
                shape.m_radius = 1.0f;

                for (int i = 0; i < _e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(-10.0f + 3.0f*i, 20.0f);
                    bd.userData = m_touching[i];

                    m_touching[i].tf = false;
                    m_bodies[i] = getWorld().createBody(bd);

                    m_bodies[i].createFixture(shape, 1.0f);
                }
            }
        }
コード例 #7
0
ファイル: DominoTest.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool argDeserialized)
        {
            if (argDeserialized)
            {
                return;
            }

            {
                // Floor
                FixtureDef fd = new FixtureDef();
                PolygonShape sd = new PolygonShape();
                sd.setAsBox(50.0f, 10.0f);
                fd.shape = sd;

                BodyDef bd = new BodyDef();
                bd.position = new Vec2(0.0f, -10.0f);
                getWorld().createBody(bd).createFixture(fd);

            }

            {
                // Platforms
                for (int i = 0; i < 4; i++)
                {
                    FixtureDef fd = new FixtureDef();
                    PolygonShape sd = new PolygonShape();
                    sd.setAsBox(15.0f, 0.125f);
                    fd.shape = sd;

                    BodyDef bd = new BodyDef();
                    bd.position = new Vec2(0.0f, 5f + 5f*i);
                    getWorld().createBody(bd).createFixture(fd);
                }
            }

            {
                FixtureDef fd = new FixtureDef();
                PolygonShape sd = new PolygonShape();
                sd.setAsBox(0.125f, 2f);
                fd.shape = sd;
                fd.density = 25.0f;

                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                float friction = .5f;
                int numPerRow = 25;

                for (int i = 0; i < 4; ++i)
                {
                    for (int j = 0; j < numPerRow; j++)
                    {
                        fd.friction = friction;
                        bd.position = new Vec2(-14.75f + j*(29.5f/(numPerRow - 1)), 7.3f + 5f*i);
                        if (i == 2 && j == 0)
                        {
                            bd.angle = -0.1f;
                            bd.position.x += .1f;
                        }
                        else if (i == 3 && j == numPerRow - 1)
                        {
                            bd.angle = .1f;
                            bd.position.x -= .1f;
                        }
                        else
                            bd.angle = 0f;
                        Body myBody = getWorld().createBody(bd);
                        myBody.createFixture(fd);
                    }
                }
            }
        }
コード例 #8
0
ファイル: DominoTower.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool argDeserialized)
        {
            if (argDeserialized)
            {
                return;
            }

            {
                // Floor
                PolygonShape sd = new PolygonShape();
                sd.setAsBox(50.0f, 10.0f);

                BodyDef bd = new BodyDef();
                bd.position = new Vec2(0.0f, -10.0f);
                getWorld().createBody(bd).createFixture(sd, 0f);
            }

            {
                ddensity = 10f;
                // Make bullet
                PolygonShape sd = new PolygonShape();
                sd.setAsBox(.7f, .7f);
                FixtureDef fd = new FixtureDef();
                fd.density = 35f;
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                fd.shape = sd;
                fd.friction = 0f;
                fd.restitution = 0.85f;
                bd.bullet = true;
                // bd.addShape(sd);
                bd.position = new Vec2(30f, 50f);
                Body b = getWorld().createBody(bd);
                b.createFixture(fd);
                b.setLinearVelocity(new Vec2(-25f, -25f));
                b.setAngularVelocity(6.7f);

                fd.density = 25f;
                bd.position = new Vec2(-30, 25f);
                b = getWorld().createBody(bd);
                b.createFixture(fd);
                b.setLinearVelocity(new Vec2(35f, -10f));
                b.setAngularVelocity(-8.3f);
            }

            {
                float currX;
                // Make base
                for (int i = 0; i < baseCount; ++i)
                {
                    currX = i*1.5f*dheight - (1.5f*dheight*baseCount/2f);
                    makeDomino(currX, dheight/2.0f, false, m_world);
                    makeDomino(currX, dheight + dwidth/2.0f, true, m_world);
                }
                currX = baseCount*1.5f*dheight - (1.5f*dheight*baseCount/2f);
                // Make 'I's
                for (int j = 1; j < baseCount; ++j)
                {
                    if (j > 3)
                        ddensity *= .8f;
                    float currY = dheight*.5f + (dheight + 2f*dwidth)*.99f*j; // y at center of 'I'
                    // structure

                    for (int i = 0; i < baseCount - j; ++i)
                    {
                        currX = i*1.5f*dheight - (1.5f*dheight*(baseCount - j)/2f); // +
                        // parent.random(-.05f,
                        // .05f);
                        ddensity *= 2.5f;
                        if (i == 0)
                        {
                            makeDomino(currX - (1.25f*dheight) + .5f*dwidth, currY - dwidth, false, m_world);
                        }
                        if (i == baseCount - j - 1)
                        {
                            // if (j != 1) //djm: why is this here? it makes it off balance
                            makeDomino(currX + (1.25f*dheight) - .5f*dwidth, currY - dwidth, false, m_world);
                        }
                        ddensity /= 2.5f;
                        makeDomino(currX, currY, false, m_world);
                        makeDomino(currX, currY + .5f*(dwidth + dheight), true, m_world);
                        makeDomino(currX, currY - .5f*(dwidth + dheight), true, m_world);
                    }
                }
            }
        }
コード例 #9
0
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            // Ground body
            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));
                ground.createFixture(shape, 0.0f);
            }

            // Collinear edges
            // This shows the problematic case where a box shape can hit
            // an internal vertex.
            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.m_radius = 0.0f;
                shape.set(new Vec2(-8.0f, 1.0f), new Vec2(-6.0f, 1.0f));
                ground.createFixture(shape, 0.0f);
                shape.set(new Vec2(-6.0f, 1.0f), new Vec2(-4.0f, 1.0f));
                ground.createFixture(shape, 0.0f);
                shape.set(new Vec2(-4.0f, 1.0f), new Vec2(-2.0f, 1.0f));
                ground.createFixture(shape, 0.0f);
            }

            // Chain shape
            {
                BodyDef bd = new BodyDef();
                bd.angle = 0.25f*MathUtils.PI;
                Body ground = getWorld().createBody(bd);

                Vec2[] vs = new Vec2[4];
                vs[0] = new Vec2(5.0f, 7.0f);
                vs[1] = new Vec2(6.0f, 8.0f);
                vs[2] = new Vec2(7.0f, 8.0f);
                vs[3] = new Vec2(8.0f, 7.0f);
                ChainShape shape = new ChainShape();
                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.
            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(1.0f, 1.0f, new Vec2(4.0f, 3.0f), 0.0f);
                ground.createFixture(shape, 0.0f);
                shape.setAsBox(1.0f, 1.0f, new Vec2(6.0f, 3.0f), 0.0f);
                ground.createFixture(shape, 0.0f);
                shape.setAsBox(1.0f, 1.0f, new Vec2(8.0f, 3.0f), 0.0f);
                ground.createFixture(shape, 0.0f);
            }

            // Square made from an edge loop. Collision should be smooth.
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.createBody(bd);

                Vec2[] vs = new Vec2[4];
                vs[0] = new Vec2(-1.0f, 3.0f);
                vs[1] = new Vec2(1.0f, 3.0f);
                vs[2] = new Vec2(1.0f, 5.0f);
                vs[3] = new Vec2(-1.0f, 5.0f);
                ChainShape shape = new ChainShape();
                shape.createLoop(vs, 4);
                ground.createFixture(shape, 0.0f);
            }

            // Edge loop. Collision should be smooth.
            {
                BodyDef bd = new BodyDef();
                bd.position.set(-10.0f, 4.0f);
                Body ground = getWorld().createBody(bd);

                Vec2[] vs = new Vec2[10];
                vs[0] = new Vec2(0.0f, 0.0f);
                vs[1] = new Vec2(6.0f, 0.0f);
                vs[2] = new Vec2(6.0f, 2.0f);
                vs[3] = new Vec2(4.0f, 1.0f);
                vs[4] = new Vec2(2.0f, 2.0f);
                vs[5] = new Vec2(0.0f, 2.0f);
                vs[6] = new Vec2(-2.0f, 2.0f);
                vs[7] = new Vec2(-4.0f, 3.0f);
                vs[8] = new Vec2(-6.0f, 2.0f);
                vs[9] = new Vec2(-6.0f, 0.0f);
                ChainShape shape = new ChainShape();
                shape.createLoop(vs, 10);
                ground.createFixture(shape, 0.0f);
            }

            // Square character 1
            {
                BodyDef bd = new BodyDef();
                bd.position.set(-3.0f, 8.0f);
                bd.type = BodyType.DYNAMIC;
                bd.fixedRotation = true;
                bd.allowSleep = false;

                Body body = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.5f);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                body.createFixture(fd);
            }

            // Square character 2
            {
                BodyDef bd = new BodyDef();
                bd.position.set(-5.0f, 5.0f);
                bd.type = BodyType.DYNAMIC;
                bd.fixedRotation = true;
                bd.allowSleep = false;

                Body body = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.25f, 0.25f);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                body.createFixture(fd);
            }

            // Hexagon character
            {
                BodyDef bd = new BodyDef();
                bd.position.set(-5.0f, 8.0f);
                bd.type = BodyType.DYNAMIC;
                bd.fixedRotation = true;
                bd.allowSleep = false;

                Body body = getWorld().createBody(bd);

                float angle = 0.0f;
                float delta = MathUtils.PI/3.0f;
                Vec2[] vertices = new Vec2[6];
                for (int i = 0; i < 6; ++i)
                {
                    vertices[i] = new Vec2(0.5f*MathUtils.cos(angle), 0.5f*MathUtils.sin(angle));
                    angle += delta;
                }

                PolygonShape shape = new PolygonShape();
                shape.set(vertices, 6);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                body.createFixture(fd);
            }

            // Circle character
            {
                BodyDef bd = new BodyDef();
                bd.position.set(3.0f, 5.0f);
                bd.type = BodyType.DYNAMIC;
                bd.fixedRotation = true;
                bd.allowSleep = false;

                Body body = getWorld().createBody(bd);

                CircleShape shape = new CircleShape();
                shape.m_radius = 0.5f;

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                body.createFixture(fd);
            }

            // Circle character
            {
                BodyDef bd = new BodyDef();
                bd.position.set(-7.0f, 6.0f);
                bd.type = BodyType.DYNAMIC;
                bd.allowSleep = false;

                m_character = getWorld().createBody(bd);

                CircleShape shape = new CircleShape();
                shape.m_radius = 0.25f;

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 1;
                m_character.createFixture(fd);
            }
        }
コード例 #10
0
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            // Ground body
            {
                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f));

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

                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);
                ground.createFixture(sd);
            }

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

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

            PolygonShape polygon = new PolygonShape();
            polygon.set(vertices, 3);

            FixtureDef triangleShapeDef = new FixtureDef();
            triangleShapeDef.shape = polygon;
            triangleShapeDef.density = 1.0f;

            BodyDef triangleBodyDef = new BodyDef();
            triangleBodyDef.type = BodyType.DYNAMIC;
            triangleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

            Body body1 = getWorld().createBody(triangleBodyDef);
            body1.createFixture(triangleShapeDef);

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

            triangleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

            Body body2 = getWorld().createBody(triangleBodyDef);
            body2.createFixture(triangleShapeDef);

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

            FixtureDef boxShapeDef = new FixtureDef();
            boxShapeDef.shape = polygon;
            boxShapeDef.density = 1.0f;

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.type = BodyType.DYNAMIC;
            boxBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

            Body body3 = getWorld().createBody(boxBodyDef);
            body3.createFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.setAsBox(2.0f, 1.0f);
            boxBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

            Body body4 = getWorld().createBody(boxBodyDef);
            body4.createFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();
            circle.m_radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.shape = circle;
            circleShapeDef.density = 1.0f;

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.type = BodyType.DYNAMIC;
            circleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

            Body body5 = getWorld().createBody(circleBodyDef);
            body5.createFixture(circleShapeDef);

            // Large circle
            circle.m_radius *= 2.0f;
            circleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

            Body body6 = getWorld().createBody(circleBodyDef);
            body6.createFixture(circleShapeDef);
        }
コード例 #11
0
ファイル: TestbedTest.cs プロジェクト: Nomad1/sharpbox2d
        private void launchBomb(Vec2 position, Vec2 velocity)
        {
            if (bomb != null)
            {
                m_world.destroyBody(bomb);
                bomb = null;
            }
            // todo optimize this
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;
            bd.position.set(position);
            bd.bullet = true;
            bomb = m_world.createBody(bd);
            bomb.setLinearVelocity(velocity);

            CircleShape circle = new CircleShape();
            circle.m_radius = 0.3f;

            FixtureDef fd = new FixtureDef();
            fd.shape = circle;
            fd.density = 20f;
            fd.restitution = 0;

            Vec2 minV = new Vec2(position);
            Vec2 maxV = new Vec2(position);

            minV.subLocal(new Vec2(.3f, .3f));
            maxV.addLocal(new Vec2(.3f, .3f));

            aabb.lowerBound.set(minV);
            aabb.upperBound.set(maxV);

            bomb.createFixture(fd);
        }
コード例 #12
0
ファイル: BodyTypes.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            m_speed = 3.0f;

            if (deserialized)
            {
                return;
            }

            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;

                ground.createFixture(fd);
            }

            // Define attachment
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(0.0f, 3.0f);
                m_attachment = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 2.0f);
                m_attachment.createFixture(shape, 2.0f);
            }

            // Define platform
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(-4.0f, 5.0f);
                m_platform = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 4.0f, new Vec2(4.0f, 0.0f), 0.5f*MathUtils.PI);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.friction = 0.6f;
                fd.density = 2.0f;
                m_platform.createFixture(fd);

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.initialize(m_attachment, m_platform, new Vec2(0.0f, 5.0f));
                rjd.maxMotorTorque = 50.0f;
                rjd.enableMotor = true;
                getWorld().createJoint(rjd);

                PrismaticJointDef pjd = new PrismaticJointDef();
                pjd.initialize(ground, m_platform, new Vec2(0.0f, 5.0f), new Vec2(1.0f, 0.0f));

                pjd.maxMotorForce = 1000.0f;
                pjd.enableMotor = true;
                pjd.lowerTranslation = -10.0f;
                pjd.upperTranslation = 10.0f;
                pjd.enableLimit = true;

                getWorld().createJoint(pjd);

            }

            // .create a payload
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(0.0f, 8.0f);
                Body body = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.75f, 0.75f);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.friction = 0.6f;
                fd.density = 2.0f;

                body.createFixture(fd);
            }
        }
コード例 #13
0
ファイル: RayCastTest.cs プロジェクト: Nomad1/sharpbox2d
        private void Create(int index)
        {
            if (m_bodies[m_bodyIndex] != null)
            {
                getWorld().destroyBody(m_bodies[m_bodyIndex]);
                m_bodies[m_bodyIndex] = null;
            }

            BodyDef bd = new BodyDef();

            float x = (float) _random.NextDouble()*20 - 10;
            float y = (float) _random.NextDouble()*20;
            bd.position.set(x, y);
            bd.angle = (float) _random.NextDouble()*MathUtils.TWOPI - MathUtils.PI;

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

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

            m_bodies[m_bodyIndex] = getWorld().createBody(bd);

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

                m_bodies[m_bodyIndex].createFixture(fd);
            }
            else
            {
                FixtureDef fd = new FixtureDef();
                fd.shape = m_edge;
                fd.friction = 0.3f;

                m_bodies[m_bodyIndex].createFixture(fd);
            }

            m_bodyIndex = (m_bodyIndex + 1)%e_maxBodies;
        }
コード例 #14
0
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.createFixture(shape, 0.0f);
            }

            {
                PolygonShape shape2 = new PolygonShape();
                shape2.setAsBox(13.0f, 0.25f);

                BodyDef bd2 = new BodyDef();
                bd2.position.set(-4.0f, 22.0f);
                bd2.angle = -0.25f;

                Body ground = getWorld().createBody(bd2);
                ground.createFixture(shape2, 0.0f);
            }

            {
                PolygonShape shape3 = new PolygonShape();
                shape3.setAsBox(0.25f, 1.0f);

                BodyDef bd3 = new BodyDef();
                bd3.position.set(10.5f, 19.0f);

                Body ground = getWorld().createBody(bd3);
                ground.createFixture(shape3, 0.0f);
            }

            {
                PolygonShape shape4 = new PolygonShape();
                shape4.setAsBox(13.0f, 0.25f);

                BodyDef bd4 = new BodyDef();
                bd4.position.set(4.0f, 14.0f);
                bd4.angle = 0.25f;

                Body ground = getWorld().createBody(bd4);
                ground.createFixture(shape4, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.25f, 1.0f);

                BodyDef bd = new BodyDef();
                bd.position.set(-10.5f, 11.0f);

                Body ground = getWorld().createBody(bd);
                ground.createFixture(shape, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(13.0f, 0.25f);

                BodyDef bd = new BodyDef();
                bd.position.set(-4.0f, 6.0f);
                bd.angle = -0.25f;

                Body ground = getWorld().createBody(bd);
                ground.createFixture(shape, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.5f);

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 25.0f;

                float[] friction = {0.75f, 0.5f, 0.35f, 0.1f, 0.0f};

                for (int i = 0; i < 5; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(-15.0f + 4.0f*i, 28.0f);
                    Body body = getWorld().createBody(bd);

                    fd.friction = friction[i];
                    body.createFixture(fd);
                }
            }
        }
コード例 #15
0
ファイル: RevoluteTest.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.createFixture(shape, 0.0f);
            }

            {
                CircleShape shape = new CircleShape();
                shape.m_radius = 0.5f;

                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;

                RevoluteJointDef rjd = new RevoluteJointDef();

                bd.position.set(-10f, 20.0f);
                Body body = getWorld().createBody(bd);
                body.createFixture(shape, 5.0f);

                float w = 100.0f;
                body.setAngularVelocity(w);
                body.setLinearVelocity(new Vec2(-8.0f*w, 0.0f));

                rjd.initialize(ground, body, new Vec2(-10.0f, 12.0f));
                rjd.motorSpeed = -1.0f*MathUtils.PI;
                rjd.maxMotorTorque = 10000.0f;
                rjd.enableMotor = false;
                rjd.lowerAngle = -0.25f*MathUtils.PI;
                rjd.upperAngle = 0.5f*MathUtils.PI;
                rjd.enableLimit = true;
                rjd.collideConnected = true;

                m_joint = (RevoluteJoint) getWorld().createJoint(rjd);
            }

            {
                CircleShape circle_shape = new CircleShape();
                circle_shape.m_radius = 3.0f;

                BodyDef circle_bd = new BodyDef();
                circle_bd.type = BodyType.DYNAMIC;
                circle_bd.position.set(5.0f, 30.0f);

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

                Body ball = m_world.createBody(circle_bd);
                ball.createFixture(fd);

                PolygonShape polygon_shape = new PolygonShape();
                polygon_shape.setAsBox(10.0f, 0.2f, new Vec2(-10.0f, 0.0f), 0.0f);

                BodyDef polygon_bd = new BodyDef();
                polygon_bd.position.set(20.0f, 10.0f);
                polygon_bd.type = BodyType.DYNAMIC;
                polygon_bd.bullet = true;
                Body polygon_body = m_world.createBody(polygon_bd);
                polygon_body.createFixture(polygon_shape, 2.0f);

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.initialize(ground, polygon_body, new Vec2(20.0f, 10.0f));
                rjd.lowerAngle = -0.25f*MathUtils.PI;
                rjd.upperAngle = 0.0f*MathUtils.PI;
                rjd.enableLimit = true;
                m_world.createJoint(rjd);
            }

            // Tests mass computation of a small object far from the origin
            {
                BodyDef bodyDef = new BodyDef();
                bodyDef.type = BodyType.DYNAMIC;
                Body body = m_world.createBody(bodyDef);

                PolygonShape polyShape = new PolygonShape();
                Vec2[] verts = new Vec2[3];
                verts[0] = new Vec2(17.63f, 36.31f);
                verts[1] = new Vec2(17.52f, 36.69f);
                verts[2] = new Vec2(17.19f, 36.36f);
                polyShape.set(verts, 3);

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

                body.createFixture(polyFixtureDef); // assertion hits inside here
            }
        }
コード例 #16
0
ファイル: Car.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            m_hz = 4.0f;
            m_zeta = 0.7f;
            m_speed = 50.0f;

            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.createBody(bd);

                EdgeShape shape = new EdgeShape();

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

                shape.set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));
                ground.createFixture(fd);

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

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

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

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

                shape.set(new Vec2(x, 0.0f), new Vec2(x + 40.0f, 0.0f));
                ground.createFixture(fd);

                x += 80.0f;
                shape.set(new Vec2(x, 0.0f), new Vec2(x + 40.0f, 0.0f));
                ground.createFixture(fd);

                x += 40.0f;
                shape.set(new Vec2(x, 0.0f), new Vec2(x + 10.0f, 5.0f));
                ground.createFixture(fd);

                x += 20.0f;
                shape.set(new Vec2(x, 0.0f), new Vec2(x + 40.0f, 0.0f));
                ground.createFixture(fd);

                x += 40.0f;
                shape.set(new Vec2(x, 0.0f), new Vec2(x, 20.0f));
                ground.createFixture(fd);
            }

            // Teeter
            {
                BodyDef bd = new BodyDef();
                bd.position.set(140.0f, 1.0f);
                bd.type = BodyType.DYNAMIC;
                Body body = m_world.createBody(bd);

                PolygonShape box = new PolygonShape();
                box.setAsBox(10.0f, 0.25f);
                body.createFixture(box, 1.0f);

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.initialize(ground, body, body.getPosition());
                jd.lowerAngle = -8.0f*MathUtils.PI/180.0f;
                jd.upperAngle = 8.0f*MathUtils.PI/180.0f;
                jd.enableLimit = true;
                m_world.createJoint(jd);

                body.applyAngularImpulse(100.0f);
            }

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

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

                RevoluteJointDef jd = new RevoluteJointDef();

                Body prevBody = ground;
                for (int i = 0; i < N; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(161.0f + 2.0f*i, -0.125f);
                    Body body = m_world.createBody(bd);
                    body.createFixture(fd);

                    Vec2 anchor = new Vec2(160.0f + 2.0f*i, -0.125f);
                    jd.initialize(prevBody, body, anchor);
                    m_world.createJoint(jd);

                    prevBody = body;
                }

                Vec2 anchor2 = new Vec2(160.0f + 2.0f*N, -0.125f);
                jd.initialize(prevBody, ground, anchor2);
                m_world.createJoint(jd);
            }

            // Boxes
            {
                PolygonShape box = new PolygonShape();
                box.setAsBox(0.5f, 0.5f);

                Body body = null;
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;

                bd.position.set(230.0f, 0.5f);
                body = m_world.createBody(bd);
                body.createFixture(box, 0.5f);

                bd.position.set(230.0f, 1.5f);
                body = m_world.createBody(bd);
                body.createFixture(box, 0.5f);

                bd.position.set(230.0f, 2.5f);
                body = m_world.createBody(bd);
                body.createFixture(box, 0.5f);

                bd.position.set(230.0f, 3.5f);
                body = m_world.createBody(bd);
                body.createFixture(box, 0.5f);

                bd.position.set(230.0f, 4.5f);
                body = m_world.createBody(bd);
                body.createFixture(box, 0.5f);
            }

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

                CircleShape circle = new CircleShape();
                circle.m_radius = 0.4f;

                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(0.0f, 1.0f);
                m_car = m_world.createBody(bd);
                m_car.createFixture(chassis, 1.0f);

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

                bd.position.set(-1.0f, 0.35f);
                m_wheel1 = m_world.createBody(bd);
                m_wheel1.createFixture(fd);

                bd.position.set(1.0f, 0.4f);
                m_wheel2 = m_world.createBody(bd);
                m_wheel2.createFixture(fd);

                WheelJointDef jd = new WheelJointDef();
                Vec2 axis = new Vec2(0.0f, 1.0f);

                jd.initialize(m_car, m_wheel1, m_wheel1.getPosition(), axis);
                jd.motorSpeed = 0.0f;
                jd.maxMotorTorque = 20.0f;
                jd.enableMotor = true;
                jd.frequencyHz = m_hz;
                jd.dampingRatio = m_zeta;
                m_spring1 = (WheelJoint) m_world.createJoint(jd);

                jd.initialize(m_car, m_wheel2, m_wheel2.getPosition(), axis);
                jd.motorSpeed = 0.0f;
                jd.maxMotorTorque = 10.0f;
                jd.enableMotor = false;
                jd.frequencyHz = m_hz;
                jd.dampingRatio = m_zeta;
                m_spring2 = (WheelJoint) m_world.createJoint(jd);
            }
        }
コード例 #17
0
ファイル: DominoTower.cs プロジェクト: Nomad1/sharpbox2d
 public void makeDomino(float x, float y, bool horizontal, World world)
 {
     PolygonShape sd = new PolygonShape();
     sd.setAsBox(.5f*dwidth, .5f*dheight);
     FixtureDef fd = new FixtureDef();
     fd.shape = sd;
     fd.density = ddensity;
     BodyDef bd = new BodyDef();
     bd.type = BodyType.DYNAMIC;
     fd.friction = dfriction;
     fd.restitution = 0.65f;
     bd.position = new Vec2(x, y);
     bd.angle = horizontal ? (float) (System.Math.PI/2.0) : 0f;
     Body myBody = getWorld().createBody(bd);
     myBody.createFixture(fd);
 }
コード例 #18
0
ファイル: CircleStress.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool argDeserialized)
        {
            if (argDeserialized)
            {
                return;
            }

            Body leftWall = null;
            Body rightWall = null;
            {
                // Ground
                PolygonShape sd = new PolygonShape();
                sd.setAsBox(50.0f, 10.0f);
                BodyDef bd = new BodyDef();
                bd.type = BodyType.STATIC;
                bd.position = new Vec2(0.0f, -10.0f);
                Body b = getWorld().createBody(bd);
                FixtureDef fd = new FixtureDef();
                fd.shape = sd;
                fd.friction = 1.0f;
                b.createFixture(fd);

                // Walls
                sd.setAsBox(3.0f, 50.0f);
                bd = new BodyDef();
                bd.position = new Vec2(45.0f, 25.0f);
                rightWall = getWorld().createBody(bd);
                rightWall.createFixture(sd, 0);
                bd.position = new Vec2(-45.0f, 25.0f);
                leftWall = getWorld().createBody(bd);
                leftWall.createFixture(sd, 0);

                // Corners
                bd = new BodyDef();
                sd.setAsBox(20.0f, 3.0f);
                bd.angle = (float) (-System.Math.PI/4.0);
                bd.position = new Vec2(-35f, 8.0f);
                Body myBod = getWorld().createBody(bd);
                myBod.createFixture(sd, 0);
                bd.angle = (float) (System.Math.PI/4.0);
                bd.position = new Vec2(35f, 8.0f);
                myBod = getWorld().createBody(bd);
                myBod.createFixture(sd, 0);

                // top
                sd.setAsBox(50.0f, 10.0f);
                bd.type = BodyType.STATIC;
                bd.angle = 0;
                bd.position = new Vec2(0.0f, 75.0f);
                b = getWorld().createBody(bd);
                fd.shape = sd;
                fd.friction = 1.0f;
                b.createFixture(fd);

            }

            CircleShape cd;
            FixtureDef fd2 = new FixtureDef();

            BodyDef bd2 = new BodyDef();
            bd2.type = BodyType.DYNAMIC;
            int numPieces = 5;
            float radius = 6f;
            bd2.position = new Vec2(0.0f, 10.0f);
            Body body = getWorld().createBody(bd2);
            for (int i = 0; i < numPieces; i++)
            {
                cd = new CircleShape();
                cd.m_radius = 1.2f;
                fd2.shape = cd;
                fd2.density = 25;
                fd2.friction = .1f;
                fd2.restitution = .9f;
                float xPos = radius*(float) System.Math.Cos(2f*System.Math.PI*(i/(float) (numPieces)));
                float yPos = radius*(float) System.Math.Sin(2f*System.Math.PI*(i/(float) (numPieces)));
                cd.m_p.set(xPos, yPos);

                body.createFixture(fd2);
            }

            body.setBullet(false);

            RevoluteJointDef rjd = new RevoluteJointDef();
            rjd.initialize(body, getGroundBody(), body.getPosition());
            rjd.motorSpeed = MathUtils.PI;
            rjd.maxMotorTorque = 1000000.0f;
            rjd.enableMotor = true;
            joint = (RevoluteJoint) getWorld().createJoint(rjd);

            {
                int loadSize = 41;

                for (int j = 0; j < 15; j++)
                {
                    for (int i = 0; i < loadSize; i++)
                    {
                        CircleShape circ = new CircleShape();
                        BodyDef bod = new BodyDef();
                        bod.type = BodyType.DYNAMIC;
                        circ.m_radius = 1.0f + (i%2 == 0 ? 1.0f : -1.0f)*.5f*MathUtils.randomFloat(.5f, 1f);
                        FixtureDef fd3 = new FixtureDef();
                        fd3.shape = circ;
                        fd3.density = circ.m_radius*1.5f;
                        fd3.friction = 0.5f;
                        fd3.restitution = 0.7f;
                        float xPos = -39f + 2*i;
                        float yPos = 50f + j;
                        bod.position = new Vec2(xPos, yPos);
                        Body myBody = getWorld().createBody(bod);
                        myBody.createFixture(fd3);

                    }
                }

            }

            getWorld().setGravity(new Vec2(0, -50));
        }
コード例 #19
0
ファイル: Fixture.cs プロジェクト: Nomad1/sharpbox2d
        // We need separation create/destroy functions from the constructor/destructor because
        // the destructor cannot access the allocator (no destructor arguments allowed by C++).
        public void create(Body body, FixtureDef def)
        {
            m_userData = def.userData;
            m_friction = def.friction;
            m_restitution = def.restitution;

            m_body = body;
            m_next = null;

            m_filter.set(def.filter);

            m_isSensor = def._isSensor;

            m_shape = def.shape.clone();

            // Reserve proxy space
            int childCount = m_shape.getChildCount();
            if (m_proxies == null)
            {
                m_proxies = new FixtureProxy[childCount];
                for (int i = 0; i < childCount; i++)
                {
                    m_proxies[i] = new FixtureProxy();
                    m_proxies[i].fixture = null;
                    m_proxies[i].proxyId = (int) BroadPhaseProxy.Null;
                }
            }

            if (m_proxies.Length < childCount)
            {
                FixtureProxy[] old = m_proxies;
                int newLen = MathUtils.max(old.Length*2, childCount);
                m_proxies = new FixtureProxy[newLen];
                Array.Copy(old, 0, m_proxies, 0, old.Length);
                for (int i = 0; i < newLen; i++)
                {
                    if (i >= old.Length)
                    {
                        m_proxies[i] = new FixtureProxy();
                    }
                    m_proxies[i].fixture = null;
                    m_proxies[i].proxyId = (int) BroadPhaseProxy.Null;
                }
            }
            m_proxyCount = 0;

            m_density = def.density;
        }
コード例 #20
0
ファイル: VerticalStack.cs プロジェクト: Nomad1/sharpbox2d
        public override void keyPressed(char argKeyChar, int argKeyCode)
        {
            switch (argKeyChar)
            {
                case ',':
                    if (m_bullet != null)
                    {
                        getWorld().destroyBody(m_bullet);
                        m_bullet = null;
                    }

                {
                    CircleShape shape = new CircleShape();
                    shape.m_radius = 0.25f;

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.bullet = true;
                    bd.position.set(-31.0f, 5.0f);

                    m_bullet = getWorld().createBody(bd);
                    m_bullet.createFixture(fd);

                    m_bullet.setLinearVelocity(new Vec2(400.0f, 0.0f));
                }
                    break;
            }
        }
コード例 #21
0
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            // Ground body
            {
                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

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

                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);
                ground.createFixture(sd);
            }

            // Small triangle
            Vec2[] vertices = new Vec2[3];
            vertices[0] = new Vec2(-1.0f, 0.0f);
            vertices[1] = new Vec2(1.0f, 0.0f);
            vertices[2] = new Vec2(0.0f, 2.0f);
            PolygonShape polygon = new PolygonShape();
            polygon.set(vertices, 3);

            FixtureDef triangleShapeDef = new FixtureDef();
            triangleShapeDef.shape = polygon;
            triangleShapeDef.density = 1.0f;

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

            BodyDef triangleBodyDef = new BodyDef();
            triangleBodyDef.type = BodyType.DYNAMIC;
            triangleBodyDef.position.set(-5.0f, 2.0f);

            Body body1 = getWorld().createBody(triangleBodyDef);
            body1.createFixture(triangleShapeDef);

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

            Body body2 = getWorld().createBody(triangleBodyDef);
            body2.createFixture(triangleShapeDef);

            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(-5.0f, 10.0f);
                Body body = getWorld().createBody(bd);

                PolygonShape p = new PolygonShape();
                p.setAsBox(0.5f, 1.0f);
                body.createFixture(p, 1.0f);

                PrismaticJointDef jd = new PrismaticJointDef();
                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;

                getWorld().createJoint(jd);
            }

            // Small box
            polygon.setAsBox(1.0f, 0.5f);
            FixtureDef boxShapeDef = new FixtureDef();
            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;

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.type = BodyType.DYNAMIC;
            boxBodyDef.position.set(0.0f, 2.0f);

            Body body3 = getWorld().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);

            Body body4 = getWorld().createBody(boxBodyDef);
            body4.createFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();
            circle.m_radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.shape = circle;
            circleShapeDef.density = 1.0f;

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

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.type = BodyType.DYNAMIC;
            circleBodyDef.position.set(5.0f, 2.0f);

            Body body5 = getWorld().createBody(circleBodyDef);
            body5.createFixture(circleShapeDef);

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

            Body body6 = getWorld().createBody(circleBodyDef);
            body6.createFixture(circleShapeDef);
        }
コード例 #22
0
ファイル: VerticalStack.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }

            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                // shape.setAsBox(40, 10, new Vec2(0,-10), 0);
                shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.createFixture(shape, 0.0f);

                shape.set(new Vec2(20.0f, 0.0f), new Vec2(20.0f, 20.0f));
                ground.createFixture(shape, 0.0f);
            }

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

            for (int j = 0; j < e_columnCount; ++j)
            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.5f);

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

                for (int i = 0; i < e_rowCount; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;

                    int n = j*e_rowCount + i;
                    Debug.Assert(n < e_rowCount*e_columnCount);

                    float x = 0.0f;
                    // float x = RandomFloat(-0.02f, 0.02f);
                    // float x = i % 2 == 0 ? -0.025f : 0.025f;
                    bd.position.set(xs[j] + x, 0.752f + 1.54f*i);
                    Body body = getWorld().createBody(bd);

                    body.createFixture(fd);
                }
            }

            m_bullet = null;
        }
コード例 #23
0
ファイル: ConfinedTest.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool argDeserialized)
        {
            if (argDeserialized)
            {
                return;
            }
            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();

                // Floor
                shape.set(new Vec2(-10.0f, 0.0f), new Vec2(10.0f, 0.0f));
                ground.createFixture(shape, 0.0f);

                // Left wall
                shape.set(new Vec2(-10.0f, 0.0f), new Vec2(-10.0f, 20.0f));
                ground.createFixture(shape, 0.0f);

                // Right wall
                shape.set(new Vec2(10.0f, 0.0f), new Vec2(10.0f, 20.0f));
                ground.createFixture(shape, 0.0f);

                // Roof
                shape.set(new Vec2(-10.0f, 20.0f), new Vec2(10.0f, 20.0f));
                ground.createFixture(shape, 0.0f);
            }

            float radius = 0.5f;
            CircleShape shape2 = new CircleShape();
            shape2.m_p.setZero();
            shape2.m_radius = radius;

            FixtureDef fd = new FixtureDef();
            fd.shape = shape2;
            fd.density = 1.0f;
            fd.friction = 0.1f;

            for (int j = 0; j < e_columnCount; ++j)
            {
                for (int i = 0; i < e_rowCount; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(-10.0f + (2.1f*j + 1.0f + 0.01f*i)*radius, (2.0f*i + 1.0f)*radius);
                    Body body = getWorld().createBody(bd);

                    body.createFixture(fd);
                }
            }

            getWorld().setGravity(new Vec2(0.0f, 0.0f));
        }
コード例 #24
0
ファイル: TheoJansen.cs プロジェクト: Nomad1/sharpbox2d
        private void createLeg(float s, Vec2 wheelAnchor)
        {
            Vec2 p1 = new Vec2(5.4f*s, -6.1f);
            Vec2 p2 = new Vec2(7.2f*s, -1.2f);
            Vec2 p3 = new Vec2(4.3f*s, -1.9f);
            Vec2 p4 = new Vec2(3.1f*s, 0.8f);
            Vec2 p5 = new Vec2(6.0f*s, 1.5f);
            Vec2 p6 = new Vec2(2.5f*s, 3.7f);

            FixtureDef fd1 = new FixtureDef();
            FixtureDef fd2 = new FixtureDef();
            fd1.filter.groupIndex = -1;
            fd2.filter.groupIndex = -1;
            fd1.density = 1.0f;
            fd2.density = 1.0f;

            PolygonShape poly1 = new PolygonShape();
            PolygonShape poly2 = new PolygonShape();

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

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

                vertices[0] = new Vec2();
                vertices[1] = p5.sub(p4);
                vertices[2] = p6.sub(p4);
                poly2.set(vertices, 3);
            }
            else
            {
                Vec2[] vertices = new Vec2[3];

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

                vertices[0] = new Vec2();
                vertices[1] = p6.sub(p4);
                vertices[2] = p5.sub(p4);
                poly2.set(vertices, 3);
            }

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

            BodyDef bd1 = new BodyDef(), bd2 = new BodyDef();
            bd1.type = BodyType.DYNAMIC;
            bd2.type = BodyType.DYNAMIC;
            bd1.position = m_offset;
            bd2.position = p4.add(m_offset);

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

            Body body1 = getWorld().createBody(bd1);
            Body body2 = getWorld().createBody(bd2);

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

            DistanceJointDef djd = new DistanceJointDef();

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

            djd.initialize(body1, body2, p2.add(m_offset), p5.add(m_offset));
            getWorld().createJoint(djd);

            djd.initialize(body1, body2, p3.add(m_offset), p4.add(m_offset));
            getWorld().createJoint(djd);

            djd.initialize(body1, m_wheel, p3.add(m_offset), wheelAnchor.add(m_offset));
            getWorld().createJoint(djd);

            djd.initialize(body2, m_wheel, p6.add(m_offset), wheelAnchor.add(m_offset));
            getWorld().createJoint(djd);

            RevoluteJointDef rjd = new RevoluteJointDef();

            rjd.initialize(body2, m_chassis, p4.add(m_offset));
            getWorld().createJoint(rjd);
        }
コード例 #25
0
ファイル: ApplyForce.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }

            getWorld().setGravity(new Vec2(0.0f, 0.0f));

            float k_restitution = 0.4f;

            Body ground;
            {
                BodyDef bd = new BodyDef();
                bd.position.set(0.0f, 20.0f);
                ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();

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

                // Left vertical
                shape.set(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f));
                ground.createFixture(sd);

                // Right vertical
                shape.set(new Vec2(20.0f, -20.0f), new Vec2(20.0f, 20.0f));
                ground.createFixture(sd);

                // Top horizontal
                shape.set(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f));
                ground.createFixture(sd);

                // Bottom horizontal
                shape.set(new Vec2(-20.0f, -20.0f), new Vec2(20.0f, -20.0f));
                ground.createFixture(sd);
            }

            {
                Transform xf1 = new Transform();
                xf1.q.set(0.3524f*MathUtils.PI);
                Rot.mulToOutUnsafe(xf1.q, new Vec2(1.0f, 0.0f), ref xf1.p);

                Vec2[] vertices = new Vec2[3];
                vertices[0] = Transform.mul(xf1, new Vec2(-1.0f, 0.0f));
                vertices[1] = Transform.mul(xf1, new Vec2(1.0f, 0.0f));
                vertices[2] = Transform.mul(xf1, new Vec2(0.0f, 0.5f));

                PolygonShape poly1 = new PolygonShape();
                poly1.set(vertices, 3);

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

                Transform xf2 = new Transform();
                xf2.q.set(-0.3524f*MathUtils.PI);
                Rot.mulToOut(xf2.q, new Vec2(-1.0f, 0.0f), ref xf2.p);

                vertices[0] = Transform.mul(xf2, new Vec2(-1.0f, 0.0f));
                vertices[1] = Transform.mul(xf2, new Vec2(1.0f, 0.0f));
                vertices[2] = Transform.mul(xf2, new Vec2(0.0f, 0.5f));

                PolygonShape poly2 = new PolygonShape();
                poly2.set(vertices, 3);

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.angularDamping = 2.0f;
                bd.linearDamping = 0.5f;

                bd.position.set(0.0f, 2.0f);
                bd.angle = MathUtils.PI;
                bd.allowSleep = false;
                m_body = getWorld().createBody(bd);
                m_body.createFixture(sd1);
                m_body.createFixture(sd2);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.5f);

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

                for (int i = 0; i < 10; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;

                    bd.position.set(0.0f, 5.0f + 1.54f*i);
                    Body body = getWorld().createBody(bd);

                    body.createFixture(fd);

                    float gravity = 10.0f;
                    float I = body.getInertia();
                    float mass = body.getMass();

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

                    FrictionJointDef jd = new FrictionJointDef();
                    jd.localAnchorA.setZero();
                    jd.localAnchorB.setZero();
                    jd.bodyA = ground;
                    jd.bodyB = body;
                    jd.collideConnected = true;
                    jd.maxForce = mass*gravity;
                    jd.maxTorque = mass*radius*gravity;

                    getWorld().createJoint(jd);
                }
            }
        }
コード例 #26
0
ファイル: TheoJansen.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool argDeserialized)
        {
            if (argDeserialized)
            {
                return;
            }

            m_offset.set(0.0f, 8.0f);
            m_motorSpeed = 2.0f;
            m_motorOn = true;
            Vec2 pivot = new Vec2(0.0f, 0.8f);

            // Ground
            {
                BodyDef bd = new BodyDef();
                Body ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f));
                ground.createFixture(shape, 0.0f);

                shape.set(new Vec2(-50.0f, 0.0f), new Vec2(-50.0f, 10.0f));
                ground.createFixture(shape, 0.0f);

                shape.set(new Vec2(50.0f, 0.0f), new Vec2(50.0f, 10.0f));
                ground.createFixture(shape, 0.0f);
            }

            // Balls
            for (int i = 0; i < 40; ++i)
            {
                CircleShape shape = new CircleShape();
                shape.m_radius = 0.25f;

                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(-40.0f + 2.0f*i, 0.5f);

                Body body = getWorld().createBody(bd);
                body.createFixture(shape, 1.0f);
            }

            // Chassis
            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(2.5f, 1.0f);

                FixtureDef sd = new FixtureDef();
                sd.density = 1.0f;
                sd.shape = shape;
                sd.filter.groupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(pivot);
                bd.position.addLocal(m_offset);
                m_chassis = getWorld().createBody(bd);
                m_chassis.createFixture(sd);
            }

            {
                CircleShape shape = new CircleShape();
                shape.m_radius = 1.6f;

                FixtureDef sd = new FixtureDef();
                sd.density = 1.0f;
                sd.shape = shape;
                sd.filter.groupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(pivot);
                bd.position.addLocal(m_offset);
                m_wheel = getWorld().createBody(bd);
                m_wheel.createFixture(sd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();

                jd.initialize(m_wheel, m_chassis, pivot.add(m_offset));
                jd.collideConnected = false;
                jd.motorSpeed = m_motorSpeed;
                jd.maxMotorTorque = 400.0f;
                jd.enableMotor = m_motorOn;
                m_motorJoint = (RevoluteJoint) getWorld().createJoint(jd);
            }

            Vec2 wheelAnchor;

            wheelAnchor = pivot.add(new Vec2(0.0f, -0.8f));

            createLeg(-1.0f, wheelAnchor);
            createLeg(1.0f, wheelAnchor);

            m_wheel.setTransform(m_wheel.getPosition(), 120.0f*MathUtils.PI/180.0f);
            createLeg(-1.0f, wheelAnchor);
            createLeg(1.0f, wheelAnchor);

            m_wheel.setTransform(m_wheel.getPosition(), -120.0f*MathUtils.PI/180.0f);
            createLeg(-1.0f, wheelAnchor);
            createLeg(1.0f, wheelAnchor);
        }
コード例 #27
0
ファイル: PolyShapes.cs プロジェクト: Nomad1/sharpbox2d
        private void Create(int index)
        {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;

            float x = MathUtils.randomFloat(-2.0f, 2.0f);
            bd.position.set(x, 10.0f);
            bd.angle = MathUtils.randomFloat(-MathUtils.PI, MathUtils.PI);

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

            m_bodies[m_bodyIndex] = getWorld().createBody(bd);

            if (index < 4)
            {
                FixtureDef fd = new FixtureDef();
                fd.shape = m_polygons[index];
                fd.density = 1.0f;
                fd.friction = 0.3f;
                m_bodies[m_bodyIndex].createFixture(fd);
            }
            else
            {
                FixtureDef fd = new FixtureDef();
                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;
        }
コード例 #28
0
ファイル: Body.cs プロジェクト: Nomad1/sharpbox2d
        /**
           * Creates a fixture and attach it to this body. Use this function if you need to set some fixture
           * parameters, like friction. Otherwise you can create the fixture directly from a shape. If the
           * density is non-zero, this function automatically updates the mass of the body. Contacts are not
           * created until the next time step.
           *
           * @param def the fixture definition.
           * @warning This function is locked during callbacks.
           */
        public Fixture createFixture(FixtureDef def)
        {
            Debug.Assert(m_world.isLocked() == false);

            if (m_world.isLocked() == true)
            {
                return null;
            }

            Fixture fixture = new Fixture();
            fixture.create(this, def);

            if ((m_flags & BodyFlags.Active) == BodyFlags.Active)
            {
                BroadPhase broadPhase = m_world.m_contactManager.m_broadPhase;
                fixture.createProxies(broadPhase, m_xf);
            }

            fixture.m_next = m_fixtureList;
            m_fixtureList = fixture;
            ++m_fixtureCount;

            fixture.m_body = this;

            // Adjust mass properties if needed.
            if (fixture.m_density > 0.0f)
            {
                resetMassData();
            }

            // Let the world know we have a new fixture. This will cause new contacts
            // to be created at the beginning of the next time step.
            m_world.m_flags |= World.NEW_FIXTURE;

            return fixture;
        }
コード例 #29
0
ファイル: Cantilever.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool argDeserialized)
        {
            if (argDeserialized)
            {
                return;
            }
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = getWorld().createBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.createFixture(shape, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.125f);

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

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(-14.5f + 1.0f*i, 5.0f);
                    Body body = getWorld().createBody(bd);
                    body.createFixture(fd);

                    Vec2 anchor = new Vec2(-15.0f + 1.0f*i, 5.0f);
                    jd.initialize(prevBody, body, anchor);
                    getWorld().createJoint(jd);

                    prevBody = body;
                }
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(1f, 0.125f);

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

                WeldJointDef jd = new WeldJointDef();
                jd.frequencyHz = 5f;
                jd.dampingRatio = .7f;

                Body prevBody = ground;
                for (int i = 0; i < 3; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(-14.0f + 2.0f*i, 15.0f);
                    Body body = getWorld().createBody(bd);
                    body.createFixture(fd);

                    Vec2 anchor = new Vec2(-15.0f + 2.0f*i, 15.0f);
                    jd.initialize(prevBody, body, anchor);
                    getWorld().createJoint(jd);

                    prevBody = body;
                }
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.125f);

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

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(-4.5f + 1.0f*i, 5.0f);
                    Body body = getWorld().createBody(bd);
                    body.createFixture(fd);

                    if (i > 0)
                    {
                        Vec2 anchor = new Vec2(-5.0f + 1.0f*i, 5.0f);
                        jd.initialize(prevBody, body, anchor);
                        getWorld().createJoint(jd);
                    }

                    prevBody = body;
                }
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(0.5f, 0.125f);

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

                WeldJointDef jd = new WeldJointDef();
                jd.frequencyHz = 8f;
                jd.dampingRatio = .7f;

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(5.5f + 1.0f*i, 10.0f);
                    Body body = getWorld().createBody(bd);
                    body.createFixture(fd);

                    if (i > 0)
                    {
                        Vec2 anchor = new Vec2(5.0f + 1.0f*i, 10.0f);
                        jd.initialize(prevBody, body, anchor);
                        getWorld().createJoint(jd);
                    }

                    prevBody = body;
                }
            }

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

                PolygonShape shape = new PolygonShape();
                shape.set(vertices, 3);

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(-8.0f + 8.0f*i, 12.0f);
                Body body = getWorld().createBody(bd);
                body.createFixture(fd);
            }

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

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(-6.0f + 6.0f*i, 10.0f);
                Body body = getWorld().createBody(bd);
                body.createFixture(fd);
            }
        }
コード例 #30
0
ファイル: PistonTest.cs プロジェクト: Nomad1/sharpbox2d
        public override void initTest(bool deserialized)
        {
            if (deserialized)
            {
                return;
            }
            World world = getWorld();
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = getWorld().createBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(5.0f, 100.0f);
                bd = new BodyDef();
                bd.type = BodyType.STATIC;
                FixtureDef sides = new FixtureDef();
                sides.shape = shape;
                sides.density = 0;
                sides.friction = 0;
                sides.restitution = .8f;
                sides.filter.categoryBits = 4;
                sides.filter.maskBits = 2;

                bd.position.set(-10.01f, 50.0f);
                Body bod = world.createBody(bd);
                bod.createFixture(sides);
                bd.position.set(10.01f, 50.0f);
                bod = world.createBody(bd);
                bod.createFixture(sides);
            }

            // turney
            {
                CircleShape cd;
                FixtureDef fd = new FixtureDef();
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                int numPieces = 5;
                float radius = 4f;
                bd.position = new Vec2(0.0f, 25.0f);
                Body body = getWorld().createBody(bd);
                for (int i = 0; i < numPieces; i++)
                {
                    cd = new CircleShape();
                    cd.m_radius = .5f;
                    fd.shape = cd;
                    fd.density = 25;
                    fd.friction = .1f;
                    fd.restitution = .9f;
                    float xPos = radius*(float) System.Math.Cos(2f*System.Math.PI*(i/(float) (numPieces)));
                    float yPos = radius*(float) System.Math.Sin(2f*System.Math.PI*(i/(float) (numPieces)));
                    cd.m_p.set(xPos, yPos);

                    body.createFixture(fd);
                }

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.initialize(body, getGroundBody(), body.getPosition());
                rjd.motorSpeed = MathUtils.PI;
                rjd.maxMotorTorque = 1000000.0f;
                rjd.enableMotor = true;
                getWorld().createJoint(rjd);
            }

            {
                Body prevBody = ground;

                // Define crank.
                {
                    PolygonShape shape = new PolygonShape();
                    shape.setAsBox(0.5f, 2.0f);

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(0.0f, 7.0f);
                    Body body = getWorld().createBody(bd);
                    body.createFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.initialize(prevBody, body, new Vec2(0.0f, 5.0f));
                    rjd.motorSpeed = 1.0f*MathUtils.PI;
                    rjd.maxMotorTorque = 20000;
                    rjd.enableMotor = true;
                    getWorld().createJoint(rjd);

                    prevBody = body;
                }

                // Define follower.
                {
                    PolygonShape shape = new PolygonShape();
                    shape.setAsBox(0.5f, 4.0f);

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(0.0f, 13.0f);
                    Body body = getWorld().createBody(bd);
                    body.createFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.initialize(prevBody, body, new Vec2(0.0f, 9.0f));
                    rjd.enableMotor = false;
                    getWorld().createJoint(rjd);

                    prevBody = body;
                }

                // Define piston
                {
                    PolygonShape shape = new PolygonShape();
                    shape.setAsBox(7f, 2f);

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    bd.position.set(0.0f, 17.0f);
                    Body body = getWorld().createBody(bd);
                    FixtureDef piston = new FixtureDef();
                    piston.shape = shape;
                    piston.density = 2;
                    piston.filter.categoryBits = 1;
                    piston.filter.maskBits = 2;
                    body.createFixture(piston);
                    body.setBullet(false);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.initialize(prevBody, body, new Vec2(0.0f, 17.0f));
                    getWorld().createJoint(rjd);

                    PrismaticJointDef pjd = new PrismaticJointDef();
                    pjd.initialize(ground, body, new Vec2(0.0f, 17.0f), new Vec2(0.0f, 1.0f));

                    pjd.maxMotorForce = 1000.0f;
                    pjd.enableMotor = true;

                    getWorld().createJoint(pjd);
                }

                // Create a payload
                {
                    PolygonShape sd = new PolygonShape();
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.DYNAMIC;
                    FixtureDef fixture = new FixtureDef();
                    Body body;
                    for (int i = 0; i < 100; ++i)
                    {
                        sd.setAsBox(0.4f, 0.3f);
                        bd.position.set(-1.0f, 23.0f + i);

                        bd.bullet = bullet;
                        body = world.createBody(bd);
                        fixture.shape = sd;
                        fixture.density = .1f;
                        fixture.filter.categoryBits = 2;
                        fixture.filter.maskBits = 1 | 4 | 2;
                        body.createFixture(fixture);
                    }

                    CircleShape cd = new CircleShape();
                    cd.m_radius = 0.36f;
                    for (int i = 0; i < 100; ++i)
                    {
                        bd.position.set(1.0f, 23.0f + i);
                        bd.bullet = bullet;
                        fixture.shape = cd;
                        fixture.density = 2f;
                        fixture.filter.categoryBits = 2;
                        fixture.filter.maskBits = 1 | 4 | 2;
                        body = world.createBody(bd);
                        body.createFixture(fixture);
                    }

                    float angle = 0.0f;
                    float delta = MathUtils.PI/3.0f;
                    Vec2[] vertices = new Vec2[6];
                    for (int i = 0; i < 6; ++i)
                    {
                        vertices[i] = new Vec2(0.3f*MathUtils.cos(angle), 0.3f*MathUtils.sin(angle));
                        angle += delta;
                    }

                    PolygonShape shape = new PolygonShape();
                    shape.set(vertices, 6);

                    for (int i = 0; i < 100; ++i)
                    {
                        bd.position.set(0f, 23.0f + i);
                        bd.type = BodyType.DYNAMIC;
                        bd.fixedRotation = true;
                        bd.bullet = bullet;
                        fixture.shape = shape;
                        fixture.density = 1f;
                        fixture.filter.categoryBits = 2;
                        fixture.filter.maskBits = 1 | 4 | 2;
                        body = world.createBody(bd);
                        body.createFixture(fixture);
                    }
                }
            }
        }