Java Code Examples for com.badlogic.gdx.physics.box2d.World#createBody()

The following examples show how to use com.badlogic.gdx.physics.box2d.World#createBody() . 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: Box2DFactory.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
/** Creates a circle object with the given position and radius. Resitution defaults to 0.6. */
public static Body createCircle(World world, float x, float y, float radius, boolean isStatic) {
    CircleShape sd = new CircleShape();
    sd.setRadius(radius);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = sd;
    fdef.density = 1.0f;
    fdef.friction = 0.3f;
    fdef.restitution = 0.6f;

    BodyDef bd = new BodyDef();
    bd.allowSleep = true;
    bd.position.set(x, y);
    Body body = world.createBody(bd);
    body.createFixture(fdef);
    if (isStatic) {
        body.setType(BodyDef.BodyType.StaticBody);
    }
    else {
        body.setType(BodyDef.BodyType.DynamicBody);
    }
    return body;
}
 
Example 2
Source File: Box2DFactory.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax),
 * and rotating the box counterclockwise through the given angle, with specified restitution.
 */
public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax,
        float angle, float restitution) {
    float cx = (xmin + xmax) / 2;
    float cy = (ymin + ymax) / 2;
    float hx = Math.abs((xmax - xmin) / 2);
    float hy = Math.abs((ymax - ymin) / 2);
    PolygonShape wallshape = new PolygonShape();
    // Don't set the angle here; instead call setTransform on the body below. This allows future
    // calls to setTransform to adjust the rotation as expected.
    wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = wallshape;
    fdef.density = 1.0f;
    if (restitution>0) fdef.restitution = restitution;

    BodyDef bd = new BodyDef();
    bd.position.set(cx, cy);
    Body wall = world.createBody(bd);
    wall.createFixture(fdef);
    wall.setType(BodyDef.BodyType.StaticBody);
    wall.setTransform(cx, cy, angle);
    return wall;
}
 
Example 3
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 4
Source File: Box2dLightTest.java    From box2dlights with Apache License 2.0 6 votes vote down vote up
private void createPhysicsWorld() {

		world = new World(new Vector2(0, 0), true);
		
		float halfWidth = viewportWidth / 2f;
		ChainShape chainShape = new ChainShape();
		chainShape.createLoop(new Vector2[] {
				new Vector2(-halfWidth, 0f),
				new Vector2(halfWidth, 0f),
				new Vector2(halfWidth, viewportHeight),
				new Vector2(-halfWidth, viewportHeight) });
		BodyDef chainBodyDef = new BodyDef();
		chainBodyDef.type = BodyType.StaticBody;
		groundBody = world.createBody(chainBodyDef);
		groundBody.createFixture(chainShape, 0);
		chainShape.dispose();
		createBoxes();
	}
 
Example 5
Source File: Box2dSteeringTest.java    From gdx-ai with Apache License 2.0 6 votes vote down vote up
public Box2dSteeringEntity createSteeringEntity (World world, TextureRegion region, boolean independentFacing, int posX, int posY) {

		CircleShape circleChape = new CircleShape();
		circleChape.setPosition(new Vector2());
		int radiusInPixels = (int)((region.getRegionWidth() + region.getRegionHeight()) / 4f);
		circleChape.setRadius(Box2dSteeringTest.pixelsToMeters(radiusInPixels));

		BodyDef characterBodyDef = new BodyDef();
		characterBodyDef.position.set(Box2dSteeringTest.pixelsToMeters(posX), Box2dSteeringTest.pixelsToMeters(posY));
		characterBodyDef.type = BodyType.DynamicBody;
		Body characterBody = world.createBody(characterBodyDef);

		FixtureDef charFixtureDef = new FixtureDef();
		charFixtureDef.density = 1;
		charFixtureDef.shape = circleChape;
		charFixtureDef.filter.groupIndex = 0;
		characterBody.createFixture(charFixtureDef);

		circleChape.dispose();

		return new Box2dSteeringEntity(region, characterBody, independentFacing, Box2dSteeringTest.pixelsToMeters(radiusInPixels));
	}
 
Example 6
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 7
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 8
Source File: Box2DFactory.java    From Codelabs with MIT License 5 votes vote down vote up
public static Body createBody(World world, BodyType bodyType,
		FixtureDef fixtureDef, Vector2 position) {
	BodyDef bodyDef = new BodyDef();
	bodyDef.type = bodyType;
	bodyDef.position.set(position);

	Body body = world.createBody(bodyDef);
	body.createFixture(fixtureDef);

	fixtureDef.shape.dispose();

	return body;
}
 
Example 9
Source File: NinjaRabbitBodyFactory.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Body create(final World world, final BodyDef definition, final Direction direction) {
	Body rabbitBody = world.createBody(definition);
	loader.attachFixture(rabbitBody, RABBIT_IDENTIFIER + "-" + direction.direction(), fdef, NINJA_RABBIT_SCALE);

	Fixture footSensor = rabbitBody.getFixtureList().get(FOOT_FIXTURE_INDEX);
	footSensor.setUserData(NinjaRabbitPhysicsProcessor.FOOT_IDENTIFIER);
	footSensor.setDensity(0.0f);
	footSensor.setSensor(true);

	rabbitBody.resetMassData();

	return rabbitBody;
}
 
Example 10
Source File: CarrotBodyFactory.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Body create(final World world, final BodyDef definition, final Direction direction) {
	Body rabbitBody = world.createBody(definition);
	loader.attachFixture(rabbitBody, CarrotPhysicsProcessor.CARROT_IDENTIFIER, fdef, CARROT_SCALE);
	rabbitBody.getFixtureList().first().setUserData(CarrotPhysicsProcessor.CARROT_IDENTIFIER);
	return rabbitBody;
}
 
Example 11
Source File: Box2DFactory.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
/** Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), and rotating the box
 * counterclockwise through the given angle, with specified restitution. */
public static Body createWall (World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) {
	float cx = (xmin + xmax) / 2;
	float cy = (ymin + ymax) / 2;
	float hx = (xmax - xmin) / 2;
	float hy = (ymax - ymin) / 2;
	if (hx < 0) {
		hx = -hx;
	}

	if (hy < 0) {
		hy = -hy;
	}

	PolygonShape wallshape = new PolygonShape();
	wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), angle);

	FixtureDef fdef = new FixtureDef();
	fdef.shape = wallshape;
	fdef.density = 1.0f;
	fdef.friction = 0.02f;

	fdef.filter.groupIndex = CollisionFilters.GroupTrackWalls;
	fdef.filter.categoryBits = CollisionFilters.CategoryTrackWalls;
	fdef.filter.maskBits = CollisionFilters.MaskWalls;

	if (Config.Debug.TraverseWalls) {
		fdef.filter.groupIndex = CollisionFilters.GroupNoCollisions;
	}

	if (restitution > 0) {
		fdef.restitution = restitution;
	}

	BodyDef bd = new BodyDef();
	bd.position.set(cx, cy);
	Body wall = world.createBody(bd);
	wall.createFixture(fdef);
	wall.setType(BodyDef.BodyType.StaticBody);
	return wall;
}