Ejemplo n.º 1
0
 public override long getTag(Fixture argFixture)
 {
     if (argFixture == m_platform)
     {
         return platformTag;
     }
     return base.getTag(argFixture);
 }
Ejemplo n.º 2
0
 public override long getTag(Fixture fixture)
 {
     if (fixture == m_platform)
         return PLATFORM_TAG;
     if (fixture == m_character)
         return CHARACTER_TAG;
     return base.getTag(fixture);
 }
Ejemplo n.º 3
0
 public Fixture()
 {
     m_userData = null;
     m_body = null;
     m_next = null;
     m_proxies = null;
     m_proxyCount = 0;
     m_shape = null;
     m_filter = new Filter();
 }
Ejemplo n.º 4
0
        /**
         * Return true if contact calculations should be performed between these two shapes.
         * @warning for performance reasons this is only called when the AABBs begin to overlap.
         * @param fixtureA
         * @param fixtureB
         * @return
         */
        public bool shouldCollide(Fixture fixtureA, Fixture fixtureB)
        {
            Filter filterA = fixtureA.getFilterData();
            Filter filterB = fixtureB.getFilterData();

            if (filterA.groupIndex == filterB.groupIndex && filterA.groupIndex != 0)
            {
                return filterA.groupIndex > 0;
            }

            bool collide = (filterA.maskBits & filterB.categoryBits) != 0 &&
                              (filterA.categoryBits & filterB.maskBits) != 0;
            return collide;
        }
Ejemplo n.º 5
0
        public override void initTest(bool argDeserialized)
        {
            {
                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);
            }

            BodyDef bd2 = new BodyDef();
            bd2.type = BodyType.DYNAMIC;
            bd2.position.set(0.0f, 10.0f);
            m_body = getWorld().createBody(bd2);

            PolygonShape shape2 = new PolygonShape();
            shape2.setAsBox(4.0f, 4.0f, new Vec2(0.0f, 0.0f), 0.0f);
            m_fixture1 = m_body.createFixture(shape2, 10.0f);

            m_fixture2 = null;
        }
Ejemplo n.º 6
0
 internal EdgeShapesCallback()
 {
     m_fixture = null;
 }
Ejemplo n.º 7
0
        public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction)
        {
            Body body = fixture.getBody();
            int index = 0;
            Object userData = body.getUserData();
            if (userData != null)
            {
                index = (int) userData;
                if (index == 0)
                {
                    // filter
                    return -1f;
                }
            }

            Debug.Assert(m_count < e_maxCount);

            m_points[m_count].set(point);
            m_normals[m_count].set(normal);
            ++m_count;

            if (m_count == e_maxCount)
            {
                return 0f;
            }

            return 1f;
        }
Ejemplo n.º 8
0
        private void Break()
        {
            // Create two bodies from one.
            Body body1 = m_piece1.getBody();
            Vec2 center = body1.getWorldCenter();

            body1.destroyFixture(m_piece2);
            m_piece2 = null;

            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;
            bd.position = body1.getPosition();
            bd.angle = body1.getAngle();

            Body body2 = getWorld().createBody(bd);
            m_piece2 = body2.createFixture(m_shape2, 1.0f);

            // Compute consistent velocities for new bodies based on
            // cached velocity.
            Vec2 center1 = body1.getWorldCenter();
            Vec2 center2 = body2.getWorldCenter();

            Vec2 velocity1 = m_velocity.add(Vec2.cross(m_angularVelocity, center1.sub(center)));
            Vec2 velocity2 = m_velocity.add(Vec2.cross(m_angularVelocity, center2.sub(center)));

            body1.setAngularVelocity(m_angularVelocity);
            body1.setLinearVelocity(velocity1);

            body2.setAngularVelocity(m_angularVelocity);
            body2.setLinearVelocity(velocity2);
        }
Ejemplo n.º 9
0
        public bool reportFixture(Fixture fixture)
        {
            if (m_count == e_maxCount)
            {
                return false;
            }

            Body body = fixture.getBody();
            Shape shape = fixture.getShape();

            bool overlap = p.getCollision().testOverlap(shape, 0, m_circle, 0, body.getTransform(),
                m_transform);

            if (overlap)
            {
                DrawFixture(fixture);
                ++m_count;
            }

            return true;
        }
Ejemplo n.º 10
0
 public TestQueryCallback()
 {
     point = new Vec2();
     fixture = null;
 }
Ejemplo n.º 11
0
        public void destroy()
        {
            // The proxies must be destroyed before calling this.
            Debug.Assert(m_proxyCount == 0);

            // Free the child shape.
            m_shape = null;
            m_proxies = null;
            m_next = null;

            // TODO pool shapes
            // TODO pool fixtures
        }
Ejemplo n.º 12
0
 public override void processFixture(Fixture fixture, long tag)
 {
     if (tag == PLATFORM_TAG)
     {
         m_platform = fixture;
     }
     else if (tag == CHARACTER_TAG)
     {
         m_character = fixture;
     }
     else
     {
         base.processFixture(fixture, tag);
     }
 }
Ejemplo n.º 13
0
 public void sayGoodbye(Fixture fixture)
 {
     fixtureDestroyed(fixture);
 }
Ejemplo n.º 14
0
 public virtual void processFixture(Fixture fixture, long tag)
 {
 }
Ejemplo n.º 15
0
 public virtual long getTag(Fixture fixture)
 {
     return default(long);
 }
Ejemplo n.º 16
0
 public virtual void fixtureDestroyed(Fixture fixture)
 {
 }
Ejemplo n.º 17
0
        public bool reportFixture(Fixture argFixture)
        {
            Body body = argFixture.getBody();
            if (body.getType() == BodyType.DYNAMIC)
            {
                bool inside = argFixture.testPoint(point);
                if (inside)
                {
                    fixture = argFixture;

                    return false;
                }
            }

            return true;
        }
Ejemplo n.º 18
0
        public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction)
        {
            m_fixture = fixture;
            m_point = point;
            m_normal = normal;

            return fraction;
        }
Ejemplo n.º 19
0
        // 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;
        }
Ejemplo n.º 20
0
        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);
                }
            }
        }
Ejemplo n.º 21
0
        public Contact popContact(Fixture fixtureA, int indexA, Fixture fixtureB, int indexB)
        {
            ShapeType type1 = fixtureA.getType();
            ShapeType type2 = fixtureB.getType();

            ContactRegister reg = contactStacks[(int) type1][(int) type2];
            if (reg != null)
            {
                if (reg.primary)
                {
                    Contact c = reg.creator.pop();
                    c.init(fixtureA, indexA, fixtureB, indexB);
                    return c;
                }
                else
                {
                    Contact c = reg.creator.pop();
                    c.init(fixtureB, indexB, fixtureA, indexA);
                    return c;
                }
            }
            else
            {
                return null;
            }
        }
Ejemplo n.º 22
0
        private void drawShape(Fixture fixture, Transform xf, Color4f color, bool wireframe)
        {
            switch (fixture.getType())
            {
                case ShapeType.CIRCLE:
                {
                    CircleShape circle = (CircleShape) fixture.getShape();

                    // Vec2 center = Mul(xf, circle.m_p);
                    Transform.mulToOutUnsafe(xf, circle.m_p, ref center);
                    float radius = circle.m_radius;
                    xf.q.getXAxis(axis);

                    if (fixture.getUserData() != null && fixture.getUserData().Equals(LIQUID_INT))
                    {
                        Body b = fixture.getBody();
                        liquidOffset.set(b.m_linearVelocity);
                        float linVelLength = b.m_linearVelocity.length();
                        if (averageLinearVel == -1)
                        {
                            averageLinearVel = linVelLength;
                        }
                        else
                        {
                            averageLinearVel = .98f*averageLinearVel + .02f*linVelLength;
                        }
                        liquidOffset.mulLocal(liquidLength/averageLinearVel/2);
                        circCenterMoved.set(center);
                        circCenterMoved.addLocal(liquidOffset);
                        center.subLocal(liquidOffset);
                        m_debugDraw.drawSegment(center, circCenterMoved, liquidColor);
                        return;
                    }
                    if (wireframe)
                    {
                        m_debugDraw.drawCircle(center, radius, axis, color);
                    }
                    else
                    {
                        m_debugDraw.drawSolidCircle(center, radius, axis, color);
                    }
                }
                    break;

                case ShapeType.POLYGON:
                {
                    PolygonShape poly = (PolygonShape) fixture.getShape();
                    int vertexCount = poly.m_count;
                    Debug.Assert(vertexCount <= Settings.maxPolygonVertices);
                    Vec2[] vertices = tlvertices.get(Settings.maxPolygonVertices);

                    for (int i = 0; i < vertexCount; ++i)
                    {
                        // vertices[i] = Mul(xf, poly.m_vertices[i]);
                        Transform.mulToOutUnsafe(xf, poly.m_vertices[i], ref vertices[i]);
                    }
                    if (wireframe)
                    {
                        m_debugDraw.drawPolygon(vertices, vertexCount, color);
                    }
                    else
                    {
                        m_debugDraw.drawSolidPolygon(vertices, vertexCount, color);
                    }
                }
                    break;
                case ShapeType.EDGE:
                {
                    EdgeShape edge = (EdgeShape) fixture.getShape();
                    Transform.mulToOutUnsafe(xf, edge.m_vertex1, ref v1);
                    Transform.mulToOutUnsafe(xf, edge.m_vertex2, ref v2);
                    m_debugDraw.drawSegment(v1, v2, color);
                }
                    break;
                case ShapeType.CHAIN:
                {
                    ChainShape chain = (ChainShape) fixture.getShape();
                    int count = chain.m_count;
                    Vec2[] vertices = chain.m_vertices;

                    Transform.mulToOutUnsafe(xf, vertices[0], ref v1);
                    for (int i = 1; i < count; ++i)
                    {
                        Transform.mulToOutUnsafe(xf, vertices[i], ref v2);
                        m_debugDraw.drawSegment(v1, v2, color);
                        m_debugDraw.drawCircle(v1, 0.05f, color);
                        v1.set(v2);
                    }
                }
                    break;
                default:
                    break;
            }
        }
Ejemplo n.º 23
0
 public override void processFixture(Fixture argFixture, long argTag)
 {
     if (argTag == platformTag)
     {
         m_platform = argFixture;
         return;
     }
     base.processFixture(argFixture, argTag);
 }
Ejemplo n.º 24
0
        public override void initTest(bool deserialized)
        {
            //m_state = State.e_unknown;
            if (deserialized)
            {
                return;
            }
            // Ground
            {
                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);
            }

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

                PolygonShape shape = new PolygonShape();
                shape.setAsBox(3.0f, 0.5f);
                m_platform = body.createFixture(shape, 0.0f);

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

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

                m_radius = 0.5f;
                CircleShape shape = new CircleShape();
                shape.m_radius = m_radius;
                m_character = body.createFixture(shape, 20.0f);

                body.setLinearVelocity(new Vec2(0.0f, -50.0f));

                //m_state = State.e_unknown;
            }
        }
Ejemplo n.º 25
0
        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);
            }
        }
Ejemplo n.º 26
0
        public Body(BodyDef bd, World world)
        {
            Debug.Assert(bd.position.isValid());
            Debug.Assert(bd.linearVelocity.isValid());
            Debug.Assert(bd.gravityScale >= 0.0f);
            Debug.Assert(bd.angularDamping >= 0.0f);
            Debug.Assert(bd.linearDamping >= 0.0f);

            m_flags = 0;

            if (bd.bullet)
            {
                m_flags |= BodyFlags.Bullet;
            }
            if (bd.fixedRotation)
            {
                m_flags |= BodyFlags.FixedRotation;
            }
            if (bd.allowSleep)
            {
                m_flags |= BodyFlags.AutoSleep;
            }
            if (bd.awake)
            {
                m_flags |= BodyFlags.Awake;
            }
            if (bd.active)
            {
                m_flags |= BodyFlags.Active;
            }

            m_world = world;

            m_xf.p.set(bd.position);
            m_xf.q.set(bd.angle);

            m_sweep.localCenter.setZero();
            m_sweep.c0.set(m_xf.p);
            m_sweep.c.set(m_xf.p);
            m_sweep.a0 = bd.angle;
            m_sweep.a = bd.angle;
            m_sweep.alpha0 = 0.0f;

            m_jointList = null;
            m_contactList = null;
            m_prev = null;
            m_next = null;

            m_linearVelocity.set(bd.linearVelocity);
            m_angularVelocity = bd.angularVelocity;

            m_linearDamping = bd.linearDamping;
            m_angularDamping = bd.angularDamping;
            m_gravityScale = bd.gravityScale;

            m_force.setZero();
            m_torque = 0.0f;

            m_sleepTime = 0.0f;

            m_type = bd.type;

            if (m_type == BodyType.DYNAMIC)
            {
                m_mass = 1f;
                m_invMass = 1f;
            }
            else
            {
                m_mass = 0f;
                m_invMass = 0f;
            }

            m_I = 0.0f;
            m_invI = 0.0f;

            m_userData = bd.userData;

            m_fixtureList = null;
            m_fixtureCount = 0;
        }
Ejemplo n.º 27
0
        private void DrawFixture(Fixture fixture)
        {
            Color4f color = new Color4f(0.95f, 0.95f, 0.6f);
            Transform xf = fixture.getBody().getTransform();

            switch (fixture.getType())
            {
                case ShapeType.CIRCLE:
                {
                    CircleShape circle = (CircleShape) fixture.getShape();

                    Vec2 center = Transform.mul(xf, circle.m_p);
                    float radius = circle.m_radius;

                    debugDraw.drawCircle(center, radius, color);
                }
                    break;

                case ShapeType.POLYGON:
                {
                    PolygonShape poly = (PolygonShape) fixture.getShape();
                    int vertexCount = poly.m_count;
                    Debug.Assert(vertexCount <= Settings.maxPolygonVertices);
                    Vec2[] vertices = new Vec2[Settings.maxPolygonVertices];

                    for (int i = 0; i < vertexCount; ++i)
                    {
                        vertices[i] = Transform.mul(xf, poly.m_vertices[i]);
                    }

                    debugDraw.drawPolygon(vertices, vertexCount, color);
                }
                    break;
                default:
                    break;
            }
        }
Ejemplo n.º 28
0
        /**
           * 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;
        }
Ejemplo n.º 29
0
        public override void initTest(bool argDeserialized)
        {
            // Ground body
            {
                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);
            }

            // Breakable dynamic body
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DYNAMIC;
                bd.position.set(0.0f, 40.0f);
                bd.angle = 0.25f*MathUtils.PI;
                m_body1 = getWorld().createBody(bd);

                m_shape1 = new PolygonShape();
                m_shape1.setAsBox(0.5f, 0.5f, new Vec2(-0.5f, 0.0f), 0.0f);
                m_piece1 = m_body1.createFixture(m_shape1, 1.0f);

                m_shape2 = new PolygonShape();
                m_shape2.setAsBox(0.5f, 0.5f, new Vec2(0.5f, 0.0f), 0.0f);
                m_piece2 = m_body1.createFixture(m_shape2, 1.0f);
            }

            m_break = false;
            m_broke = false;
        }
Ejemplo n.º 30
0
        /**
           * Destroy a fixture. This removes the fixture from the broad-phase and destroys all contacts
           * associated with this fixture. This will automatically adjust the mass of the body if the body
           * is dynamic and the fixture has positive density. All fixtures attached to a body are implicitly
           * destroyed when the body is destroyed.
           *
           * @param fixture the fixture to be removed.
           * @warning This function is locked during callbacks.
           */
        public void destroyFixture(Fixture fixture)
        {
            Debug.Assert(m_world.isLocked() == false);
            if (m_world.isLocked() == true)
            {
                return;
            }

            Debug.Assert(fixture.m_body == this);

            // Remove the fixture from this body's singly linked list.
            Debug.Assert(m_fixtureCount > 0);
            Fixture node = m_fixtureList;
            Fixture last = null; // java change
            bool found = false;
            while (node != null)
            {
                if (node == fixture)
                {
                    node = fixture.m_next;
                    found = true;
                    break;
                }
                last = node;
                node = node.m_next;
            }

            // You tried to remove a shape that is not attached to this body.
            Debug.Assert(found);

            // java change, remove it from the list
            if (last == null)
            {
                m_fixtureList = fixture.m_next;
            }
            else
            {
                last.m_next = fixture.m_next;
            }

            // Destroy any contacts associated with the fixture.
            ContactEdge edge = m_contactList;
            while (edge != null)
            {
                Contact c = edge.contact;
                edge = edge.next;

                Fixture fixtureA = c.getFixtureA();
                Fixture fixtureB = c.getFixtureB();

                if (fixture == fixtureA || fixture == fixtureB)
                {
                    // This destroys the contact and removes it from
                    // this body's contact list.
                    m_world.m_contactManager.destroy(c);
                }
            }

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

            fixture.destroy();
            fixture.m_body = null;
            fixture.m_next = null;
            fixture = null;

            --m_fixtureCount;

            // Reset the mass data.
            resetMassData();
        }