Java Code Examples for com.badlogic.gdx.physics.box2d.Body#setUserData()

The following examples show how to use com.badlogic.gdx.physics.box2d.Body#setUserData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorldUtils.java    From martianrun with Apache License 2.0 6 votes vote down vote up
public static Body createEnemy(World world) {
    EnemyType enemyType = RandomUtils.getRandomEnemyType();
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY()));
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
    Body body = world.createBody(bodyDef);
    body.createFixture(shape, enemyType.getDensity());
    body.resetMassData();
    EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(),
            enemyType.getAnimationAssetId());
    body.setUserData(userData);
    shape.dispose();
    return body;
}
 
Example 2
Source File: DestroySystem.java    From Entitas-Java with MIT License 5 votes vote down vote up
@Override
protected void execute(List<GameEntity> entities) {
    for (GameEntity e : entities) {
        if (e.hasRigidBody()) {
            Body body = e.getRigidBody().body;
            e.getRigidBody().body = null;
            body.setUserData(null);
            removeBodies.add(body);

        }
        context.destroyEntity(e);

    }
}
 
Example 3
Source File: RigidBodySystem.java    From Entitas-Java with MIT License 5 votes vote down vote up
@Override
protected void execute(List<GameEntity> entities) {
    for (GameEntity e : entities) {
        Body body = e.getRigidBody().body;
        body.setUserData(e.getCreationIndex());
    }

}
 
Example 4
Source File: WorldUtils.java    From martianrun with Apache License 2.0 5 votes vote down vote up
public static Body createGround(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y));
    Body body = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Constants.GROUND_WIDTH / 2, Constants.GROUND_HEIGHT / 2);
    body.createFixture(shape, Constants.GROUND_DENSITY);
    body.setUserData(new GroundUserData(Constants.GROUND_WIDTH, Constants.GROUND_HEIGHT));
    shape.dispose();
    return body;
}
 
Example 5
Source File: WorldUtils.java    From martianrun with Apache License 2.0 5 votes vote down vote up
public static Body createRunner(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y));
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2);
    Body body = world.createBody(bodyDef);
    body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);
    body.createFixture(shape, Constants.RUNNER_DENSITY);
    body.resetMassData();
    body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));
    shape.dispose();
    return body;
}
 
Example 6
Source File: CollectibleRenderer.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
public void load(final World world, final BodyEditorLoader loader, final AssetManager assets, final MapLayer layer,
		final Object userData) {
	if (layer == null) {
		return;
	}

	for (MapObject mo : layer.getObjects()) {
		BodyDef bodyDefinition = new BodyDef();
		bodyDefinition.type = BodyType.KinematicBody;
		float x = ((Float) mo.getProperties().get("x")).floatValue() * unitScale;
		float y = ((Float) mo.getProperties().get("y")).floatValue() * unitScale;
		bodyDefinition.position.set(x, y);

		BodyFactory bodyFactory = null;
		Entity entity = null;

		if (CARROT_TYPE.equals(mo.getProperties().get(TYPE_PROPERTY, CARROT_TYPE, String.class))) {
			bodyFactory = new CarrotBodyFactory(loader);
			entity = EntityFactory.createCollectible(world, assets);
		} else {
			throw new IllegalArgumentException("Unknown collectible type {" + mo.getProperties().get(TYPE_PROPERTY, String.class) + "}");
		}

		Body body = bodyFactory.create(world, bodyDefinition);

		entity.setBody(body);
		body.setUserData(entity);
		collectibles.add(entity);
	}
}
 
Example 7
Source File: NinjaRabbitBodyProcessor.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void update(final Entity character) {
	if (character.getBody() == null) {
		character.setDirection(Direction.RIGHT);
		Body body = bodyFactory.create(world, Direction.RIGHT);
		character.setBody(body);
		body.setUserData(character);
		lastKnownDirection = Direction.RIGHT;
	} else {
		if (!lastKnownDirection.equals(character.getDirection())) {
			changeDirection(character);
		}
	}
}
 
Example 8
Source File: NinjaRabbitBodyProcessor.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets a new body onto the {@link NinjaRabbit} instance, after destroying the current one. Sets
 * the current direction to the given {@link Direction}.
 *
 * @param character
 *            The {@link NinjaRabbit} being updated.
 * @param direction
 *            The direction the {@link NinjaRabbit} is facing.
 */
private void changeDirection(final Entity character) {
	Vector2 position = character.getBody().getPosition();
	Vector2 velocity = character.getBody().getLinearVelocity();
	float angle = character.getBody().getAngle();

	world.destroyBody(character.getBody());
	Body newBody = bodyFactory.create(world, character.getDirection());
	newBody.setTransform(position, angle);
	newBody.setLinearVelocity(velocity);
	character.setBody(newBody);
	newBody.setUserData(character);

	lastKnownDirection = character.getDirection();
}
 
Example 9
Source File: Box2dSteeringEntity.java    From gdx-ai with Apache License 2.0 5 votes vote down vote up
public Box2dSteeringEntity (TextureRegion region, Body body, boolean independentFacing, float boundingRadius) {
	this.region = region;
	this.body = body;
	this.independentFacing = independentFacing;
	this.boundingRadius = boundingRadius;
	this.tagged = false;

	body.setUserData(this);
}
 
Example 10
Source File: WorldController.java    From tilt-game-android with MIT License 4 votes vote down vote up
private void createEdgeSensor(float x, float y, float w, float h) {
    Body body = PhysicsFactory.createBoxBody(_physicsWorld, x, y, w, h, BodyDef.BodyType.StaticBody, SENSOR_FIX_DEF);
    body.setUserData(ObjectName.EDGE_SENSOR_NAME);
}
 
Example 11
Source File: ActorBuilder.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
public void createPowerUp(float x, float y) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(MathUtils.floor(x) + 0.5f, MathUtils.floor(y) + 0.5f);

    Body body = b2dWorld.createBody(bodyDef);

    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(0.4f, 0.4f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.filter.categoryBits = GameManager.POWERUP_BIT;
    fixtureDef.filter.maskBits = GameManager.PLAYER_BIT;
    fixtureDef.isSensor = true;
    body.createFixture(fixtureDef);

    PowerUp powerUp = new PowerUp();
    int i;
    switch (powerUp.type) {
        case ONE_UP:
            i = 5;
            break;
        case REMOTE:
            i = 4;
            break;
        case KICK:
            i = 3;
            break;
        case SPEED:
            i = 2;
            break;
        case POWER:
            i = 1;
            break;
        case AMMO:
        default:
            i = 0;
            break;

    }

    TextureAtlas textureAtlas = assetManager.get("img/actors.pack", TextureAtlas.class);
    Renderer renderer = new Renderer(new TextureRegion(textureAtlas.findRegion("Items"), i * 16, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);
    renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2);

    Entity e = new EntityBuilder(world)
            .with(
                    powerUp,
                    new RigidBody(body),
                    new Transform(body.getPosition().x, body.getPosition().y, 1, 1, 0),
                    new State("normal"),
                    renderer
            )
            .build();

    body.setUserData(e);
    polygonShape.dispose();
}
 
Example 12
Source File: ActorBuilder.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
public void createPortal() {
    float x = GameManager.getInstance().getPortalPosition().x;
    float y = GameManager.getInstance().getPortalPosition().y;

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.position.set(x + 0.5f, y + 0.5f);

    Body body = b2dWorld.createBody(bodyDef);

    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(0.2f, 0.2f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.filter.categoryBits = GameManager.PORTAL_BIT;
    fixtureDef.filter.maskBits = GameManager.PLAYER_BIT;
    fixtureDef.isSensor = true;
    body.createFixture(fixtureDef);

    polygonShape.dispose();

    TextureRegion textureRegion = assetManager.get("img/actors.pack", TextureAtlas.class).findRegion("Items");
    Array<TextureRegion> keyFrames = new Array<>();
    for (int i = 6; i < 8; i++) {
        keyFrames.add(new TextureRegion(textureRegion, i * 16, 0, 16, 16));
    }
    Animation anim = new Animation(0.2f, keyFrames, Animation.PlayMode.LOOP);

    HashMap<String, Animation> anims = new HashMap<>();
    anims.put("normal", anim);

    Transform transform = new Transform(body.getPosition().x, body.getPosition().y, 1, 1, 0);
    transform.z = 99; // make portal drawn below player
    Renderer renderer = new Renderer(textureRegion, 16 / GameManager.PPM, 16 / GameManager.PPM);
    renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2);

    Entity e = new EntityBuilder(world)
            .with(
                    transform,
                    new State("normal"),
                    new Anim(anims),
                    renderer
            )
            .build();

    body.setUserData(e);
}