Example usage for com.badlogic.gdx.physics.box2d Fixture getUserData

List of usage examples for com.badlogic.gdx.physics.box2d Fixture getUserData

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Fixture getUserData.

Prototype

public Object getUserData() 

Source Link

Usage

From source file:com.gdx.game.tools.WolrdContactListener.java

@Override
public void beginContact(Contact cntct) {
    Fixture fixA = cntct.getFixtureA();
    Fixture fixB = cntct.getFixtureB();//w w w. j a v a  2s . c  o  m

    if (fixA.getUserData() == "head" || fixB.getUserData() == "head") {
        Fixture head = fixA.getUserData() == "head" ? fixA : fixB;
        Fixture object = head == fixA ? fixB : fixA;

        if (object.getUserData() != null
                && InteractiveTileObject.class.isAssignableFrom(object.getUserData().getClass())) {
            ((InteractiveTileObject) object.getUserData()).onHeadHit();

        }
    }

}

From source file:com.hatstick.fireman.physics.Box2DPhysicsWorld.java

License:Apache License

@Override
public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
    UserData data = (UserData) fixture.getUserData();
    if (data.modelType == ModelType.WALL) {

    }//  w  ww.j  a  v a 2 s  .c o  m
    // TODO Auto-generated method stub
    return 0;
}

From source file:com.mygdx.managers.MainContactListener.java

@Override
public void beginContact(Contact contact) {

    Fixture fa = contact.getFixtureA();
    Fixture fb = contact.getFixtureB();/*from   w  w w  .  j  a v  a2  s .c om*/

    //Stability Edit (2/11/16)
    //generallizing fa,fb null checks
    if (fa == null || fa.getUserData() == null || fb == null || fb.getUserData() == null)
        return;

    try {

        //********************
        //   PLAYER ATT targets list update
        //*********************

        //String data;
        for (Entity e : entities) {

            if (e.getSensorData() != null && fa.getUserData().equals(e.getSensorData())) {
                String[] str = { "begin", e.getSensorData().toString(), fb.getUserData().toString() };
                e.alert(str);
            } else if (e.getUserData().equals(fa.getUserData())) {
                String[] str = { "begin", fa.getUserData().toString(), fb.getUserData().toString() };
                e.alert(str);
            }

            if (e.getSensorData() != null && fb.getUserData().equals(e.getSensorData())) {
                String[] str = { "begin", e.getSensorData().toString(), fa.getUserData().toString() };
                e.alert(str);
            } else if (e.getUserData().equals(fb.getUserData())) {
                String[] str = { "begin", fb.getUserData().toString(), fa.getUserData().toString() };
                e.alert(str);
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.mygdx.managers.MainContactListener.java

@Override
public void endContact(Contact contact) {

    Fixture fa = contact.getFixtureA();
    Fixture fb = contact.getFixtureB();// w  w  w .j a  va 2  s  .c  o  m

    //Stability Edit (2/11/16)
    //generallizing fa,fb null checks
    if (fa == null || fa.getUserData() == null || fb == null || fb.getUserData() == null)
        return;

    try {

        for (Entity e : entities) {
            if (e.getSensorData() != null && fa.getUserData().equals(e.getSensorData())) {
                String[] str = { "end", e.getSensorData().toString(), fb.getUserData().toString() };
                e.alert(str);
            } else if (e.getUserData().equals(fa.getUserData())) {
                String[] str = { "end", fa.getUserData().toString(), fb.getUserData().toString() };
                e.alert(str);
            }

            if (e.getSensorData() != null && fb.getUserData().equals(e.getSensorData())) {
                String[] str = { "end", e.getSensorData().toString(), fa.getUserData().toString() };
                e.alert(str);
            } else if (e.getUserData().equals(fb.getUserData())) {
                String[] str = { "end", fb.getUserData().toString(), fa.getUserData().toString() };
                e.alert(str);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.pastew.autogearbox.handlers.Box2DSprite.java

License:Apache License

/** draws all the {@link Box2DSprite Box2DSprites} on the {@link Body} or {@link Fixture} that hold them in their user data in the given {@link World} */
public static void draw(Batch batch, World world, boolean sortByZ) {
    @SuppressWarnings("unchecked")
    Array<Body> tmpBodies = Pools.obtain(Array.class);
    world.getBodies(tmpBodies);//  w w  w .  ja  v  a  2 s. com

    if (sortByZ) {
        @SuppressWarnings("unchecked")
        ObjectMap<Box2DSprite, Object> tmpZMap = Pools.obtain(ObjectMap.class);
        tmpZMap.clear();
        for (Body body : tmpBodies) {
            Box2DSprite tmpBox2DSprite;
            if ((tmpBox2DSprite = userDataAccessor.apply(body.getUserData())) != null)
                tmpZMap.put(tmpBox2DSprite, body);
            for (Fixture fixture : body.getFixtureList())
                if ((tmpBox2DSprite = userDataAccessor.apply(fixture.getUserData())) != null)
                    tmpZMap.put(tmpBox2DSprite, fixture);
        }

        @SuppressWarnings("unchecked")
        Array<Box2DSprite> tmpKeys = Pools.obtain(Array.class);
        Iterator<Box2DSprite> keys = tmpZMap.keys();
        while (keys.hasNext())
            tmpKeys.add(keys.next());
        tmpKeys.sort(zComparator);
        for (Box2DSprite key : tmpKeys) {
            Object value = tmpZMap.get(key);
            if (value instanceof Body)
                key.draw(batch, (Body) value);
            else
                key.draw(batch, (Fixture) value);
        }

        tmpKeys.clear();
        tmpZMap.clear();
        Pools.free(tmpKeys);
        Pools.free(tmpZMap);
    } else
        for (Body body : tmpBodies) {
            Box2DSprite tmpBox2DSprite;
            if ((tmpBox2DSprite = userDataAccessor.apply(body.getUserData())) != null)
                tmpBox2DSprite.draw(batch, body);
            for (Fixture fixture : body.getFixtureList())
                if ((tmpBox2DSprite = userDataAccessor.apply(fixture.getUserData())) != null)
                    tmpBox2DSprite.draw(batch, fixture);
        }

    tmpBodies.clear();
    Pools.free(tmpBodies);
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java

License:Apache License

/** creates a {@link Fixture} from a {@link MapObject}
 *  @param mapObject the {@link MapObject} to parse
 *  @param body the {@link Body} to create the {@link Fixture Fixtures} on
 *  @return the parsed {@link Fixture} */
public Fixture createFixture(MapObject mapObject, Body body) {
    if ((mapObject = listener.createFixture(mapObject)) == null)
        return null;

    String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties,
            layerProperties, mapObject.getProperties());
    transform(mat4, orientation);//  ww w. j  a v a 2 s .c o  m

    Shape shape = null;
    if (mapObject instanceof RectangleMapObject) {
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        vec3.set(rectangle.x, rectangle.y, 0);
        vec3.mul(mat4);
        float x = vec3.x, y = vec3.y, width, height;
        if (!orientation.equals(aliases.staggered)) {
            vec3.set(rectangle.width, rectangle.height, 0).mul(mat4);
            width = vec3.x;
            height = vec3.y;
        } else {
            width = rectangle.width * unitScale;
            height = rectangle.height * unitScale;
        }
        ((PolygonShape) (shape = new PolygonShape())).setAsBox(width / 2, height / 2,
                vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject || mapObject instanceof PolylineMapObject) {
        FloatArray vertices = Pools.obtain(FloatArray.class);
        vertices.clear();
        vertices.addAll(mapObject instanceof PolygonMapObject
                ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices()
                : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) {
            vec3.set(vertices.get(ix), vertices.get(iy), 0);
            vec3.mul(mat4);
            vertices.set(ix, vec3.x - body.getPosition().x);
            vertices.set(iy, vec3.y - body.getPosition().y);
        }
        if (mapObject instanceof PolygonMapObject)
            ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size);
        else if (vertices.size == 4)
            ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2),
                    vertices.get(3));
        else {
            vertices.shrink();
            ((ChainShape) (shape = new ChainShape())).createChain(vertices.items);
        }
        Pools.free(vertices);
    } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) {
        if (mapObject instanceof CircleMapObject) {
            Circle circle = ((CircleMapObject) mapObject).getCircle();
            vec3.set(circle.x, circle.y, circle.radius);
        } else {
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            if (ellipse.width != ellipse.height)
                throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                        + ClassReflection.getSimpleName(mapObject.getClass())
                        + "s that are not circles are not supported");
            vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2);
        }
        vec3.mul(mat4);
        vec3.sub(body.getPosition().x, body.getPosition().y, 0);
        CircleShape circleShape = (CircleShape) (shape = new CircleShape());
        circleShape.setPosition(vec2.set(vec3.x, vec3.y));
        circleShape.setRadius(vec3.z);
    } else if (mapObject instanceof TextureMapObject)
        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + ClassReflection.getSimpleName(mapObject.getClass()) + "s are not supported");
    else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    MapProperties properties = mapObject.getProperties();

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    assignProperties(fixtureDef, heritage);
    assignProperties(fixtureDef, mapProperties);
    assignProperties(fixtureDef, layerProperties);
    assignProperties(fixtureDef, properties);

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties,
            layerProperties, properties));

    shape.dispose();

    fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture);
    listener.created(fixture, mapObject);

    return fixture;
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** clones a {@link Fixture}
 *  @param fixture the {@link Fixture} to copy
 *  @param body the {@link Body} to create a copy of the given {@code fixture} on
 *  @param shape if the {@link Fixture#getShape() shape} of the given {@code fixture} should be deep {@link #clone(Shape) copied} as well
 *  @return the copied {@link Fixture} */
public static Fixture clone(Fixture fixture, Body body, boolean shape) {
    FixtureDef fixtureDef = createDef(fixture);
    if (shape)/*from   ww w . j  a va 2  s. c o  m*/
        fixtureDef.shape = clone(fixture.getShape());
    Fixture clone = body.createFixture(fixtureDef);
    clone.setUserData(clone.getUserData());
    return clone;
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.graphics.Box2DSprite.java

License:Apache License

/** draws all the {@link Box2DSprite Box2DSprites} on the {@link Body} or {@link Fixture} that hold them in their user data in the given {@link World} */
public static void draw(Batch batch, World world, boolean sortByZ) {
    @SuppressWarnings("unchecked")
    Array<Body> tmpBodies = Pools.obtain(Array.class);
    Box2DSprite tmpBox2DSprite;//from   ww w.j  av  a  2s  .  c o  m

    world.getBodies(tmpBodies);

    if (sortByZ) {
        @SuppressWarnings("unchecked")
        ObjectMap<Box2DSprite, Object> tmpZMap = Pools.obtain(ObjectMap.class);
        tmpZMap.clear();
        for (Body body : tmpBodies) {
            if ((tmpBox2DSprite = userDataAccessor.apply(body.getUserData())) != null)
                tmpZMap.put(tmpBox2DSprite, body);
            for (Fixture fixture : body.getFixtureList())
                if ((tmpBox2DSprite = userDataAccessor.apply(fixture.getUserData())) != null)
                    tmpZMap.put(tmpBox2DSprite, fixture);
        }

        @SuppressWarnings("unchecked")
        Array<Box2DSprite> tmpKeys = Pools.obtain(Array.class);
        Iterator<Box2DSprite> keys = tmpZMap.keys();
        while (keys.hasNext())
            tmpKeys.add(keys.next());
        tmpKeys.sort(zComparator);
        for (Box2DSprite key : tmpKeys) {
            Object value = tmpZMap.get(key);
            if (value instanceof Body)
                key.draw(batch, (Body) value);
            else
                key.draw(batch, (Fixture) value);
        }

        tmpKeys.clear();
        tmpZMap.clear();
        Pools.free(tmpKeys);
        Pools.free(tmpZMap);
    } else
        for (Body body : tmpBodies) {
            if ((tmpBox2DSprite = userDataAccessor.apply(body.getUserData())) != null)
                tmpBox2DSprite.draw(batch, body);
            for (Fixture fixture : body.getFixtureList())
                if ((tmpBox2DSprite = userDataAccessor.apply(fixture.getUserData())) != null)
                    tmpBox2DSprite.draw(batch, fixture);
        }

    tmpBodies.clear();
    Pools.free(tmpBodies);
}

From source file:com.strategames.engine.gameobject.types.Icecube.java

License:Open Source License

@Override
public void handleCollision(Contact contact, ContactImpulse impulse, GameObject gameObject) {
    float maxImpulse = 0.0f;

    float[] impulses = impulse.getNormalImpulses();

    maxImpulse = impulses[0] > maximumImpulse ? maximumImpulse : impulses[0];

    if (maxImpulse > 20) { // prevent counting rocks hitting when they are lying on top of eachother
        rocksHit++;//  ww w .j a va 2s.  com
        rocksHitTotalImpulse += maxImpulse;

        if ((maxImpulse > 50) && (this.amountOfParts > 1)) { // break object

            if (this.breakOfPart == null) {

                Body body = getBody();
                //Get colliding fixture for this object
                Fixture fixture = contact.getFixtureA();
                if (fixture.getBody() != body) {
                    fixture = contact.getFixtureB();
                }

                /**
                 * fixtures get reused and sometimes seem to be available in the wrong object
                 * (fixtures from an old object which has not yet been removed)
                 */
                if (body.getFixtureList().contains(fixture, true)) {
                    Integer userData = (Integer) fixture.getUserData();
                    if (userData != null) {
                        try {
                            this.breakOfPart = this.parts.get(userData.intValue());
                        } catch (IndexOutOfBoundsException e) {
                            Gdx.app.log("Icecube", "handleCollision: array out of bounds: this=" + this
                                    + "fixture=" + fixture);
                        }
                    }
                }
            }
        }
    }
}

From source file:de.fhkoeln.game.utils.Box2DMapObjectParser.java

License:Apache License

/** creates a {@link com.badlogic.gdx.physics.box2d.Fixture} from a {@link com.badlogic.gdx.maps.MapObject}
 *  @param mapObject the {@link com.badlogic.gdx.maps.MapObject} to parse
 *  @param body the {@link com.badlogic.gdx.physics.box2d.Body} to create the {@link com.badlogic.gdx.physics.box2d.Fixture Fixtures} on
 *  @return the parsed {@link com.badlogic.gdx.physics.box2d.Fixture} */
public Fixture createFixture(MapObject mapObject, Body body) {
    if ((mapObject = listener.createFixture(mapObject)) == null)
        return null;

    String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties,
            layerProperties, mapObject.getProperties());
    transform(mat4, orientation);/*  www .j  ava 2  s.  c  o m*/

    Shape shape = null;
    if (mapObject instanceof RectangleMapObject) {
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        vec3.set(rectangle.x, rectangle.y, 0);
        vec3.mul(mat4);
        float x = vec3.x, y = vec3.y, width, height;
        if (!orientation.equals(aliases.staggered)) {
            vec3.set(rectangle.width, rectangle.height, 0).mul(mat4);
            width = vec3.x;
            height = vec3.y;
        } else {
            width = rectangle.width * unitScale;
            height = rectangle.height * unitScale;
        }
        ((PolygonShape) (shape = new PolygonShape())).setAsBox(width / 2, height / 2,
                vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject || mapObject instanceof PolylineMapObject) {
        FloatArray vertices = Pools.obtain(FloatArray.class);
        vertices.clear();
        vertices.addAll(mapObject instanceof PolygonMapObject
                ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices()
                : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) {
            vec3.set(vertices.get(ix), vertices.get(iy), 0);
            vec3.mul(mat4);
            vertices.set(ix, vec3.x - body.getPosition().x);
            vertices.set(iy, vec3.y - body.getPosition().y);
        }
        if (mapObject instanceof PolygonMapObject)
            ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size);
        else if (vertices.size == 4)
            ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2),
                    vertices.get(3));
        else {
            vertices.shrink();
            ((ChainShape) (shape = new ChainShape())).createChain(vertices.items);
        }
        Pools.free(vertices);
    } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) {
        if (mapObject instanceof CircleMapObject) {
            Circle circle = ((CircleMapObject) mapObject).getCircle();
            vec3.set(circle.x, circle.y, circle.radius);
        } else {
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            if (ellipse.width != ellipse.height)
                throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                        + mapObject.getClass().getSimpleName() + "s that are not circles are not supported");
            vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2);
        }
        vec3.mul(mat4);
        vec3.sub(body.getPosition().x, body.getPosition().y, 0);
        CircleShape circleShape = (CircleShape) (shape = new CircleShape());
        circleShape.setPosition(vec2.set(vec3.x, vec3.y));
        circleShape.setRadius(vec3.z);
    } else if (mapObject instanceof TextureMapObject)
        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + mapObject.getClass().getSimpleName() + "s are not supported");
    else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    MapProperties properties = mapObject.getProperties();

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    assignProperties(fixtureDef, heritage);
    assignProperties(fixtureDef, mapProperties);
    assignProperties(fixtureDef, layerProperties);
    assignProperties(fixtureDef, properties);

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties,
            layerProperties, properties));

    shape.dispose();

    fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture);
    listener.created(fixture, mapObject);

    return fixture;
}