Exemple #1
0
        internal FrictionJoint(IWorldPool argWorldPool, FrictionJointDef def)
            : base(argWorldPool, def)
        {
            m_localAnchorA = new Vec2(def.localAnchorA);
            m_localAnchorB = new Vec2(def.localAnchorB);

            m_linearImpulse = new Vec2();
            m_angularImpulse = 0.0f;

            m_maxForce = def.maxForce;
            m_maxTorque = def.maxTorque;
        }
Exemple #2
0
        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);
                }
            }
        }