Search in sources :

Example 6 with Fixture

use of com.badlogic.gdx.physics.box2d.Fixture in project commons-gdx by gemserk.

the class Box2dUtils method setFilter.

/**
	 * Sets the filter data for all the fixtures of the Body.
	 * 
	 * @param body
	 *            The Body to set the filters to.
	 * @param filter
	 *            The Filter data to set to the body fixtures.
	 */
public static void setFilter(Body body, Filter filter) {
    ArrayList<Fixture> fixtureList = body.getFixtureList();
    for (int i = 0; i < fixtureList.size(); i++) {
        Fixture fixture = fixtureList.get(i);
        fixture.setFilterData(filter);
    }
}
Also used : Fixture(com.badlogic.gdx.physics.box2d.Fixture)

Example 7 with Fixture

use of com.badlogic.gdx.physics.box2d.Fixture in project commons-gdx by gemserk.

the class Box2dUtils method translateFixtures.

/**
	 * Internally translates all the Body's fixtures the specified translation, without moving the Body's center. For now, it generates garbage so should be used only once.
	 * 
	 * @param body
	 *            The body to work on.
	 * @param tx
	 *            The translation in x coordinate.
	 * @param ty
	 *            The translation in y coordinate.
	 */
public static void translateFixtures(ArrayList<Fixture> fixtures, float tx, float ty) {
    // TODO: should be in a Box2dUtils
    for (int i = 0; i < fixtures.size(); i++) {
        Fixture fixture = fixtures.get(i);
        Shape shape = fixture.getShape();
        if (shape.getType() == Type.Polygon)
            ShapeUtils.translatePolygonShape((PolygonShape) shape, tx, ty);
        else if (shape.getType() == Type.Circle)
            ShapeUtils.translateCircleShape((CircleShape) shape, tx, ty);
    }
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Shape(com.badlogic.gdx.physics.box2d.Shape) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Fixture(com.badlogic.gdx.physics.box2d.Fixture)

Example 8 with Fixture

use of com.badlogic.gdx.physics.box2d.Fixture in project commons-gdx by gemserk.

the class Contacts method removeContact.

public void removeContact(com.badlogic.gdx.physics.box2d.Contact contact, boolean AB) {
    Fixture myFixture;
    Fixture otherFixture;
    if (AB) {
        myFixture = contact.getFixtureA();
        otherFixture = contact.getFixtureB();
    } else {
        myFixture = contact.getFixtureB();
        otherFixture = contact.getFixtureA();
    }
    removeContact(myFixture, otherFixture);
}
Also used : Fixture(com.badlogic.gdx.physics.box2d.Fixture)

Example 9 with Fixture

use of com.badlogic.gdx.physics.box2d.Fixture in project commons-gdx by gemserk.

the class PhysicsUtils method releaseContacts.

public static void releaseContacts(Contacts contacts) {
    // removes contact from the other entity
    for (int i = 0; i < contacts.getContactCount(); i++) {
        Contact contact = contacts.getContact(i);
        Fixture myFixture = contact.getMyFixture();
        Fixture otherFixture = contact.getOtherFixture();
        contactsToRemove.add(contact);
        Body otherBody = otherFixture.getBody();
        if (otherBody == null)
            continue;
        Entity otherEntity = (Entity) otherBody.getUserData();
        if (otherEntity == null)
            continue;
        // entitiesInContact.add(otherEntity);
        // removes the contact from the other entity.
        PhysicsComponent otherPhyiscsComponent = Components.getPhysicsComponent(otherEntity);
        otherPhyiscsComponent.getContact().removeContact(otherFixture, myFixture);
    }
    for (int i = 0; i < contactsToRemove.size; i++) {
        Contact contact = contactsToRemove.get(i);
        contacts.removeContact(contact.getMyFixture(), contact.getOtherFixture());
    }
    contactsToRemove.clear();
}
Also used : Entity(com.artemis.Entity) PhysicsComponent(com.gemserk.commons.artemis.components.PhysicsComponent) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact)

Example 10 with Fixture

use of com.badlogic.gdx.physics.box2d.Fixture in project RubeLoader by tescott.

the class RubeLoaderTest method createPolySpatialsFromRubeFixtures.

/**
    * Creates an array of PolySpatials based on fixture information from the scene. Note that
    * fixtures create aligned textures.
    * 
    * @param scene
    */
private void createPolySpatialsFromRubeFixtures(RubeScene scene) {
    Array<Body> bodies = scene.getBodies();
    EarClippingTriangulator ect = new EarClippingTriangulator();
    if ((bodies != null) && (bodies.size > 0)) {
        polySpatials = new Array<PolySpatial>();
        Vector2 bodyPos = new Vector2();
        // for each body in the scene...
        for (int i = 0; i < bodies.size; i++) {
            Body body = bodies.get(i);
            bodyPos.set(body.getPosition());
            Array<Fixture> fixtures = body.getFixtureList();
            if ((fixtures != null) && (fixtures.size > 0)) {
                // for each fixture on the body...
                for (int j = 0; j < fixtures.size; j++) {
                    Fixture fixture = fixtures.get(j);
                    String textureName = (String) scene.getCustom(fixture, "TextureMask", null);
                    if (textureName != null) {
                        String textureFileName = "data/" + textureName;
                        Texture texture = textureMap.get(textureFileName);
                        TextureRegion textureRegion = null;
                        if (texture == null) {
                            texture = new Texture(textureFileName);
                            texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
                            textureMap.put(textureFileName, texture);
                            textureRegion = new TextureRegion(texture);
                            textureRegionMap.put(texture, textureRegion);
                        } else {
                            textureRegion = textureRegionMap.get(texture);
                        }
                        // only handle polygons at this point -- no chain, edge, or circle fixtures.
                        if (fixture.getType() == Shape.Type.Polygon) {
                            PolygonShape shape = (PolygonShape) fixture.getShape();
                            int vertexCount = shape.getVertexCount();
                            float[] vertices = new float[vertexCount * 2];
                            // static bodies are texture aligned and do not get drawn based off of the related body.
                            if (body.getType() == BodyType.StaticBody) {
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    mTmp.rotate(body.getAngle() * MathUtils.radiansToDegrees);
                                    // convert local coordinates to world coordinates to that textures are
                                    mTmp.add(bodyPos);
                                    // aligned
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, Color.WHITE);
                                polySpatials.add(spatial);
                            } else {
                                // all other fixtures are aligned based on their associated body.
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, body, Color.WHITE);
                                polySpatials.add(spatial);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) EarClippingTriangulator(com.badlogic.gdx.math.EarClippingTriangulator) Texture(com.badlogic.gdx.graphics.Texture) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) PolygonRegion(com.badlogic.gdx.graphics.g2d.PolygonRegion)

Aggregations

Fixture (com.badlogic.gdx.physics.box2d.Fixture)18 Body (com.badlogic.gdx.physics.box2d.Body)6 Vector2 (com.badlogic.gdx.math.Vector2)5 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)4 Contact (com.gemserk.commons.gdx.box2d.Contacts.Contact)3 B2Controller (com.gushikustudios.box2d.controllers.B2Controller)3 Texture (com.badlogic.gdx.graphics.Texture)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)2 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)2 MassData (com.badlogic.gdx.physics.box2d.MassData)2 Expectations (org.jmock.Expectations)2 Test (org.junit.Test)2 Entity (com.artemis.Entity)1 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 PolygonRegion (com.badlogic.gdx.graphics.g2d.PolygonRegion)1 PolygonSpriteBatch (com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 EarClippingTriangulator (com.badlogic.gdx.math.EarClippingTriangulator)1 Vector3 (com.badlogic.gdx.math.Vector3)1