Java Code Examples for com.badlogic.gdx.physics.box2d.BodyDef.BodyType#DynamicBody

The following examples show how to use com.badlogic.gdx.physics.box2d.BodyDef.BodyType#DynamicBody . 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: Box2dLightCustomShaderTest.java    From box2dlights with Apache License 2.0 6 votes vote down vote up
private void createBoxes() {
	CircleShape ballShape = new CircleShape();
	ballShape.setRadius(RADIUS);

	FixtureDef def = new FixtureDef();
	def.restitution = 0.9f;
	def.friction = 0.01f;
	def.shape = ballShape;
	def.density = 1f;
	BodyDef boxBodyDef = new BodyDef();
	boxBodyDef.type = BodyType.DynamicBody;

	for (int i = 0; i < BALLSNUM; i++) {
		// Create the BodyDef, set a random position above the
		// ground and create a new body
		boxBodyDef.position.x = 1 + (float) (Math.random() * (viewportWidth - 2));
		boxBodyDef.position.y = 1 + (float) (Math.random() * (viewportHeight - 2));
		Body boxBody = world.createBody(boxBodyDef);
		boxBody.createFixture(def);
		boxBody.setFixedRotation(true);
		balls.add(boxBody);
	}
	ballShape.dispose();
}
 
Example 2
Source File: Box2dLightTest.java    From box2dlights with Apache License 2.0 6 votes vote down vote up
private void createBoxes() {
	CircleShape ballShape = new CircleShape();
	ballShape.setRadius(RADIUS);

	FixtureDef def = new FixtureDef();
	def.restitution = 0.9f;
	def.friction = 0.01f;
	def.shape = ballShape;
	def.density = 1f;
	BodyDef boxBodyDef = new BodyDef();
	boxBodyDef.type = BodyType.DynamicBody;

	for (int i = 0; i < BALLSNUM; i++) {
		// Create the BodyDef, set a random position above the
		// ground and create a new body
		boxBodyDef.position.x = -20 + (float) (Math.random() * 40);
		boxBodyDef.position.y = 10 + (float) (Math.random() * 15);
		Body boxBody = world.createBody(boxBodyDef);
		boxBody.createFixture(def);
		balls.add(boxBody);
	}
	ballShape.dispose();
}
 
Example 3
Source File: NinjaRabbitBodyFactory.java    From ninja-rabbit with GNU General Public License v2.0 6 votes vote down vote up
public NinjaRabbitBodyFactory(final BodyEditorLoader loader) {
	if (loader == null) {
		throw new IllegalArgumentException("'loader' cannot be null");
	}
	this.loader = loader;

	bdef = new BodyDef();
	bdef.type = BodyType.DynamicBody;
	bdef.position.set(INITIAL_POSITION);
	bdef.fixedRotation = true;
	bdef.gravityScale = 2.0f;
	// bdef.bullet = true;

	fdef = new FixtureDef();
	fdef.density = 1.0f;
	fdef.restitution = 0.0f;
	fdef.friction = 0.8f;
}
 
Example 4
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 5
Source File: Body.java    From tilt-game-android with MIT License 5 votes vote down vote up
/**
 * Get the type of this body.
 */
public BodyType getType () {
	int type = jniGetType(addr);
	if (type == 0) return BodyType.StaticBody;
	if (type == 1) return BodyType.KinematicBody;
	if (type == 2) return BodyType.DynamicBody;
	return BodyType.StaticBody;
}
 
Example 6
Source File: BuoyancySample.java    From Codelabs with MIT License 5 votes vote down vote up
@Override
public void beginContact(Contact contact) {
	Fixture fixtureA = contact.getFixtureA();
	Fixture fixtureB = contact.getFixtureB();

	if (fixtureA.isSensor()
			&& fixtureB.getBody().getType() == BodyType.DynamicBody) {
		buoyancyController.addBody(fixtureB);
	} else if (fixtureB.isSensor()
			&& fixtureA.getBody().getType() == BodyType.DynamicBody) {
		buoyancyController.addBody(fixtureA);
	}
}
 
Example 7
Source File: BuoyancySample.java    From Codelabs with MIT License 5 votes vote down vote up
@Override
public void endContact(Contact contact) {
	Fixture fixtureA = contact.getFixtureA();
	Fixture fixtureB = contact.getFixtureB();

	if (fixtureA.isSensor()
			&& fixtureB.getBody().getType() == BodyType.DynamicBody) {
		buoyancyController.removeBody(fixtureB);
	} else if (fixtureB.isSensor()
			&& fixtureA.getBody().getType() == BodyType.DynamicBody) {
		if (fixtureA.getBody().getWorldCenter().y > -1) {
			buoyancyController.removeBody(fixtureA);
		}
	}
}
 
Example 8
Source File: CarrotBodyFactory.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
public CarrotBodyFactory(final BodyEditorLoader loader) {
	if (loader == null) {
		throw new IllegalArgumentException("'loader' cannot be null");
	}
	this.loader = loader;

	bdef = new BodyDef();
	bdef.type = BodyType.DynamicBody;
	bdef.position.set(0, 0);
	bdef.fixedRotation = true;

	fdef = new FixtureDef();
	fdef.isSensor = true;
}
 
Example 9
Source File: Car.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
private void applyCarPhysics (CarType carType, CarModel carModel) {
	if (body != null) {
		this.box2dWorld.destroyBody(body);
	}

	// body
	BodyDef bd = new BodyDef();
	bd.angle = 0;
	bd.type = BodyType.DynamicBody;
	// bd.bullet = true;

	body = box2dWorld.createBody(bd);
	body.setBullet(true);
	body.setUserData(this);

	// set physical properties and apply shape
	FixtureDef fd = new FixtureDef();
	fd.density = carModel.density;
	fd.friction = carModel.friction;
	fd.restitution = carModel.restitution;

	fd.filter.groupIndex = (short)((carType == CarType.PlayerCar) ? CollisionFilters.GroupPlayer : CollisionFilters.GroupReplay);
	fd.filter.categoryBits = (short)((carType == CarType.PlayerCar) ? CollisionFilters.CategoryPlayer
		: CollisionFilters.CategoryReplay);
	fd.filter.maskBits = (short)((carType == CarType.PlayerCar) ? CollisionFilters.MaskPlayer : CollisionFilters.MaskReplay);

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

	BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/cars/car-shapes"));

	// WARNING! Be sure to set a value and use it then, every time this changes replays will NOT be compatible!

	// electron is made for a model2 car, w=2.5, h=3.5, h/w=1.4, w:h=1:1.4
	float scaleX = carModel.width / 2.5f;
	float scaleY = carModel.length / 3.5f;

	// the scaling factor should be 2, but in night mode is cool to see light bleeding across the edges of
	// the car, fading away as soon as the physical body is reached
	loader.attachFixture(body, "uracer-car", fd, 1.85f, scaleX, scaleY);
	Array<Fixture> fs = body.getFixtureList();
	for (Fixture f : fs) {
		f.setUserData(carType);
	}

	MassData mdata = body.getMassData();
	mdata.center.set(0, 0);
	body.setMassData(mdata);
}