com.badlogic.gdx.physics.box2d.BodyDef Java Examples

The following examples show how to use com.badlogic.gdx.physics.box2d.BodyDef. 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: Scene2dRaycastObstacleAvoidanceTest.java    From gdx-ai with Apache License 2.0 6 votes vote down vote up
private void createRandomWalls (int n) {
	PolygonShape groundPoly = new PolygonShape();

	BodyDef groundBodyDef = new BodyDef();
	groundBodyDef.type = BodyType.StaticBody;

	FixtureDef fixtureDef = new FixtureDef();
	fixtureDef.shape = groundPoly;
	fixtureDef.filter.groupIndex = 0;

	walls = new Body[n];
	walls_hw = new int[n];
	walls_hh = new int[n];
	for (int i = 0; i < n; i++) {
		groundBodyDef.position.set(MathUtils.random(50, (int)container.stageWidth - 50),
			MathUtils.random(50, (int)container.stageHeight - 50));
		walls[i] = world.createBody(groundBodyDef);
		walls_hw[i] = (int)MathUtils.randomTriangular(20, 150);
		walls_hh[i] = (int)MathUtils.randomTriangular(30, 80);
		groundPoly.setAsBox(walls_hw[i], walls_hh[i]);
		walls[i].createFixture(fixtureDef);

	}
	groundPoly.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: 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 #4
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 #5
Source File: ActorBuilder.java    From Bomberman_libGdx with MIT License 6 votes vote down vote up
public void createIndestructible(float x, float y, TextureAtlas tileTextureAtlas) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(x, y);

    Body body = b2dWorld.createBody(bodyDef);
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(0.5f, 0.5f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.filter.categoryBits = GameManager.INDESTRUCTIIBLE_BIT;
    fixtureDef.filter.maskBits = GameManager.PLAYER_BIT | GameManager.ENEMY_BIT | GameManager.BOMB_BIT;
    body.createFixture(fixtureDef);

    polygonShape.dispose();

    Renderer renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("indestructible"), 0, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);
    renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2);

    new EntityBuilder(world)
            .with(
                    new Transform(x, y, 1f, 1f, 0),
                    renderer
            )
            .build();
}
 
Example #6
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 #7
Source File: Box2dRaycastObstacleAvoidanceTest.java    From gdx-ai with Apache License 2.0 6 votes vote down vote up
private void createRandomWalls (int n) {
	PolygonShape groundPoly = new PolygonShape();

	BodyDef groundBodyDef = new BodyDef();
	groundBodyDef.type = BodyType.StaticBody;

	FixtureDef fixtureDef = new FixtureDef();
	fixtureDef.shape = groundPoly;
	fixtureDef.filter.groupIndex = 0;

	walls = new Body[n];
	walls_hw = new int[n];
	walls_hh = new int[n];
	for (int i = 0; i < n; i++) {
		groundBodyDef.position.set(Box2dSteeringTest.pixelsToMeters(MathUtils.random(50, (int)container.stageWidth - 50)),
			Box2dSteeringTest.pixelsToMeters(MathUtils.random(50, (int)container.stageHeight - 50)));
		walls[i] = world.createBody(groundBodyDef);
		walls_hw[i] = (int)MathUtils.randomTriangular(20, 150);
		walls_hh[i] = (int)MathUtils.randomTriangular(30, 80);
		groundPoly.setAsBox(Box2dSteeringTest.pixelsToMeters(walls_hw[i]), Box2dSteeringTest.pixelsToMeters(walls_hh[i]));
		walls[i].createFixture(fixtureDef);

	}
	groundPoly.dispose();
}
 
Example #8
Source File: Block.java    From Entitas-Java with MIT License 6 votes vote down vote up
@Override
public GameEntity create(Engine engine, Context<GameEntity> context) {

    BasePhysicsManager physics = engine.getManager(BasePhysicsManager.class);
    BodyBuilder bodyBuilder = physics.getBodyBuilder();

    Body bodyBlock = bodyBuilder.fixture(new FixtureDefBuilder()
            .boxShape(1, 1)
            .friction(0.5f)
            .restitution(0.5f))
            .type(BodyDef.BodyType.StaticBody)
            .build();


    GameEntity entity =  context.createEntity()
            .addRigidBody(bodyBlock)
            .addElement("Platform", "Block");


    return entity;
}
 
Example #9
Source File: Block.java    From Entitas-Java with MIT License 6 votes vote down vote up
@Override
public GameEntity create(Engine engine, Context<GameEntity> context) {

    BasePhysicsManager physics = engine.getManager(BasePhysicsManager.class);
    BodyBuilder bodyBuilder = physics.getBodyBuilder();

    Body bodyBlock = bodyBuilder.fixture(new FixtureDefBuilder()
            .boxShape(1, 1)
            .friction(0.5f)
            .restitution(0.5f))
            .type(BodyDef.BodyType.StaticBody)
            .build();


    GameEntity entity =  context.createEntity()
            .addRigidBody(bodyBlock)
            .addElement("Platform", "Block");


    return entity;
}
 
Example #10
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 #11
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 #12
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 #13
Source File: Box.java    From Entitas-Java with MIT License 6 votes vote down vote up
@Override
public GameEntity create(Engine engine, Entitas entitas) {
    BodyBuilder bodyBuilder = engine.getManager(PhysicsManagerGDX.class).getBodyBuilder();

    float width = MathUtils.random(0.4f, 1);
    float height =  MathUtils.random(0.4f, 1f);


    GameEntity entity = entitas.game.createEntity()
            .setInteractive(true)
            .addRigidBody(bodyBuilder.fixture(bodyBuilder.fixtureDefBuilder
                    .boxShape(width, height)
                    .friction(0.5f)
                    .density(1f))
                    .type(BodyDef.BodyType.DynamicBody)
                    .build())
            .addTextureView(new TextureRegion(assetsManager.getTexture(box2)), new Bounds(width, height),
                    false, false, 1, 0, Color.WHITE);

    entitas.actuator.createEntity()
            .addRadialGravityActuator(9, 5, 2)
            .addLink(entity.getCreationIndex(), "RadialGravityActuator", true);

    return entity;

}
 
Example #14
Source File: AddViewSystem.java    From Entitas-Java with MIT License 6 votes vote down vote up
@Override
    public void execute(List<GameEntity> entities) {
        for (GameEntity e : entities) {
            Texture texture = assetsManager.getTexture(String.format("assets/textures/%1$s.png", e.getAsset().name));
            Body body = bodyBuilder.fixture(new FixtureDefBuilder()
                    .boxShape(0.5f, 0.5f))
                    .type(BodyDef.BodyType.KinematicBody)
                    .build();
            TextureRegion textureRegion = new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());
            e.addTextureView(e.getAsset().name, textureRegion, body);


//                gameObject.Link(e, _pool);
//
            if (e.hasPosition()) {
                Position pos = e.getPosition();
                body.setTransform(new Vector2(pos.x, pos.y + 1), 0);
            }
        }
    }
 
Example #15
Source File: FlipperElement.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
@Override public void createBodies(World world) {
    this.anchorBody = Box2DFactory.createCircle(world, this.cx, this.cy, 0.05f, true);
    // Joint angle is 0 when flipper is horizontal.
    // The flipper needs to be slightly extended past anchorBody to rotate correctly.
    float ext = (this.flipperLength > 0) ? -0.05f : +0.05f;
    // Width larger than 0.12 slows rotation?
    this.flipperBody = Box2DFactory.createWall(world, cx+ext, cy-0.12f, cx+flipperLength, cy+0.12f, 0f);
    flipperBody.setType(BodyDef.BodyType.DynamicBody);
    flipperBody.setBullet(true);
    flipperBody.getFixtureList().get(0).setDensity(5.0f);

    jointDef = new RevoluteJointDef();
    jointDef.initialize(anchorBody, flipperBody, new Vector2(this.cx, this.cy));
    jointDef.enableLimit = true;
    jointDef.enableMotor = true;
    // counterclockwise rotations are positive, so flip angles for flippers extending left
    jointDef.lowerAngle = (this.flipperLength>0) ? this.minangle : -this.maxangle;
    jointDef.upperAngle = (this.flipperLength>0) ? this.maxangle : -this.minangle;
    jointDef.maxMotorTorque = 1000f;

    this.joint = (RevoluteJoint)world.createJoint(jointDef);

    flipperBodySet = Collections.singletonList(flipperBody);
    this.setEffectiveMotorSpeed(-this.downspeed); // Force flipper to bottom when field is first created.
}
 
Example #16
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 #17
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 #18
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 #19
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 #20
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 #21
Source File: PhysicsFactory.java    From tilt-game-android with MIT License 5 votes vote down vote up
/**
 * @param pPhysicsWorld
 * @param pEntity
 * @param pTriangleVertices are to be defined relative to the center of the pEntity and have the {@link PhysicsConstants#PIXEL_TO_METER_RATIO_DEFAULT} applied.
 * 					The vertices will be triangulated and for each triangle a {@link Fixture} will be created.
 * @param pBodyType
 * @param pFixtureDef
 * @return
 */
public static Body createTrianglulatedBody(final PhysicsWorld pPhysicsWorld, final IEntity pEntity, final List<Vector2> pTriangleVertices, final BodyType pBodyType, final FixtureDef pFixtureDef, final float pPixelToMeterRatio) {
	final Vector2[] TMP_TRIANGLE = new Vector2[3];

	final BodyDef boxBodyDef = new BodyDef();
	boxBodyDef.type = pBodyType;

	final float[] sceneCenterCoordinates = pEntity.getSceneCenterCoordinates();
	boxBodyDef.position.x = sceneCenterCoordinates[Constants.VERTEX_INDEX_X] / pPixelToMeterRatio;
	boxBodyDef.position.y = sceneCenterCoordinates[Constants.VERTEX_INDEX_Y] / pPixelToMeterRatio;

	final Body boxBody = pPhysicsWorld.createBody(boxBodyDef);

	final int vertexCount = pTriangleVertices.size();
	for (int i = 0; i < vertexCount; /* */) {
		final PolygonShape boxPoly = new PolygonShape();

		TMP_TRIANGLE[2] = pTriangleVertices.get(i++);
		TMP_TRIANGLE[1] = pTriangleVertices.get(i++);
		TMP_TRIANGLE[0] = pTriangleVertices.get(i++);

		boxPoly.set(TMP_TRIANGLE);
		pFixtureDef.shape = boxPoly;

		boxBody.createFixture(pFixtureDef);

		boxPoly.dispose();
	}

	return boxBody;
}
 
Example #22
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 #23
Source File: GameSceneState.java    From Entitas-Java with MIT License 5 votes vote down vote up
@Override
public void initialize() {
    atlas = assetsManager.getTextureAtlas(SPRITE_ATLAS);
    Array<TextureAtlas.AtlasRegion> heroWalking = atlas.findRegions("Andando");
    Array<TextureAtlas.AtlasRegion> heroJump = atlas.findRegions("Saltando");
    Array<TextureAtlas.AtlasRegion> heroFall = atlas.findRegions("Cayendo");
    Array<TextureAtlas.AtlasRegion> heroIdle = atlas.findRegions("Parado");


    Animation walking = new Animation(RUNNING_FRAME_DURATION, heroWalking, Animation.PlayMode.LOOP);
    Animation jump = new Animation(RUNNING_FRAME_DURATION * 7, heroJump, Animation.PlayMode.NORMAL);
    Animation fall = new Animation(RUNNING_FRAME_DURATION * 5, heroFall, Animation.PlayMode.NORMAL);
    Animation idle = new Animation(RUNNING_FRAME_DURATION * 4, heroIdle, Animation.PlayMode.LOOP);


    Map animationHero = Collections.createMap(String.class, Animation.class);
    animationHero.put("WALKING", walking);
    animationHero.put("JUMPING", jump);
    animationHero.put("FALL", fall);
    animationHero.put("HURT", fall);
    animationHero.put("IDLE", idle);
    animationHero.put("HIT", fall);


    Body body = bodyBuilder.fixture(new FixtureDefBuilder()
            .boxShape(0.5f, 0.5f))
            .type(BodyDef.BodyType.KinematicBody)
            .build();


    systems.add(new InputsControllerSystem(entitas.input, guiFactory))
            .add(new AnimationSystem(entitas))
            .add(new TextureRendererSystem(entitas, engine.batch));


    entitas.game.getPlayerEntity()
            .addTextureView(null,new Bounds(0.8f,1.2f),false,false,1,1, Color.WHITE)
            .addAnimations(animationHero, walking, 0.02f)
            .addRigidBody(body);
}
 
Example #24
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 #25
Source File: PractiseLevel.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
public void createLevel(PhysicsWorld world, FixtureDef fixtureDef) {
	createBox(world, fixtureDef, _originalWidth / 2, _originalHeight, _originalWidth, 5);
	createBox(world, fixtureDef, _originalWidth / 2, 0, _originalWidth, 5);
	createBox(world, fixtureDef, 0, _originalHeight / 2, 5, _originalHeight);
	createBox(world, fixtureDef, _originalWidth, _originalHeight / 2, 5, _originalHeight);

	_centerSensor = PhysicsFactory.createCircleBody(world, _scale * _originalWidth / 2, _scale * _originalHeight / 2, .1f * _width,
			BodyDef.BodyType.StaticBody, WorldController.SENSOR_FIX_DEF);
	_centerSensor.setUserData(ObjectName.CENTER_SENSOR_NAME);
}
 
Example #26
Source File: WorldController.java    From tilt-game-android with MIT License 5 votes vote down vote up
private void createPhysicalBall(Vector2 location) {
    if (location == null) return;

    _ball = PhysicsFactory.createCircleBody(_physicsWorld, location.x, location.y, BALL_SIZE_FACTOR * _width, 0, BodyDef.BodyType.DynamicBody,
            BALL_FIX_DEF);
    _ball.setFixedRotation(true);
    _ball.setUserData(ObjectName.BALL_NAME);
}
 
Example #27
Source File: Box2DSynchronizerPre.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
protected void process(int entityId) {
  Body body = mBox2DBody.get(entityId).body;
  Vector2 velocity = mVelocity.get(entityId).velocity;
  body.setLinearVelocity(velocity);

  // FIXME: This is a temp fix to prevent shoving NPCs -- need to explore Contact Filters
  if (velocity.isZero()) {
    body.setType(BodyDef.BodyType.StaticBody);
  } else {
    body.setType(BodyDef.BodyType.DynamicBody);
  }
}
 
Example #28
Source File: Mariano.java    From Entitas-Java with MIT License 5 votes vote down vote up
@Override
public GameEntity create(Engine engine, Context<GameEntity> context) {

    BasePhysicsManager physics = engine.getManager(BasePhysicsManager.class);
    BodyBuilder bodyBuilder =  physics.getBodyBuilder();

    TextureAtlas textureAtlas = assetsManager.getTextureAtlas(atlas);

    Array<TextureAtlas.AtlasRegion> heroWalking = textureAtlas.findRegions("Andando");
    Array<TextureAtlas.AtlasRegion> heroJump = textureAtlas.findRegions("Saltando");
    Array<TextureAtlas.AtlasRegion> heroFall = textureAtlas.findRegions("Cayendo");
    Array<TextureAtlas.AtlasRegion> heroIdle = textureAtlas.findRegions("Parado");

    Map<String, Animation<TextureRegion>> animationStates = Collections.createMap(String.class, Animation.class);
    animationStates.put("walking", new Animation(0.02f, heroWalking, Animation.PlayMode.LOOP));
    animationStates.put("jump", new Animation(0.02f * 7, heroJump, Animation.PlayMode.NORMAL));
    animationStates.put("fall", new Animation(0.02f * 5, heroFall, Animation.PlayMode.NORMAL));
    animationStates.put("idle", new Animation(0.02f * 4, heroIdle, Animation.PlayMode.LOOP));

    Body bodyPlayer = bodyBuilder.fixture(bodyBuilder.fixtureDefBuilder()
            .boxShape(0.35f, 1)
            .density(1))
            .type(BodyDef.BodyType.DynamicBody)
            .fixedRotation()
            .position(0, 5)
            .mass(1)
            .build();



    GameEntity entity = ((GameContext) context).getPlayerEntity()
            .addRigidBody(bodyPlayer)
            .addAnimations(animationStates, animationStates.get("idle"), 0)
            .addCharacter("Mariano", StateCharacter.IDLE, false)
            .addMovable(7,8)
            .addTextureView(null, new Bounds(0.9f,1.15f),false, false,1,1, Color.WHITE);


    return entity;
}
 
Example #29
Source File: GameSceneState.java    From Entitas-Java with MIT License 5 votes vote down vote up
@Override
public void initialize() {
    atlas = assetsManager.getTextureAtlas(SPRITE_ATLAS);
    Array<TextureAtlas.AtlasRegion> heroWalking = atlas.findRegions("Andando");
    Array<TextureAtlas.AtlasRegion> heroJump = atlas.findRegions("Saltando");
    Array<TextureAtlas.AtlasRegion> heroFall = atlas.findRegions("Cayendo");
    Array<TextureAtlas.AtlasRegion> heroIdle = atlas.findRegions("Parado");


    Animation walking = new Animation(RUNNING_FRAME_DURATION, heroWalking, Animation.PlayMode.LOOP);
    Animation jump = new Animation(RUNNING_FRAME_DURATION * 7, heroJump, Animation.PlayMode.NORMAL);
    Animation fall = new Animation(RUNNING_FRAME_DURATION * 5, heroFall, Animation.PlayMode.NORMAL);
    Animation idle = new Animation(RUNNING_FRAME_DURATION * 4, heroIdle, Animation.PlayMode.LOOP);


    Map animationHero = Collections.createMap(String.class, Animation.class);
    animationHero.put("WALKING", walking);
    animationHero.put("JUMPING", jump);
    animationHero.put("FALL", fall);
    animationHero.put("HURT", fall);
    animationHero.put("IDLE", idle);
    animationHero.put("HIT", fall);


    Body body = bodyBuilder.fixture(new FixtureDefBuilder()
            .boxShape(0.5f, 0.5f))
            .type(BodyDef.BodyType.KinematicBody)
            .build();


    systems.add(new InputsControllerSystem(entitas.input, guiFactory))
            .add(new AnimationSystem(entitas))
            .add(new TextureRendererSystem(entitas, engine.batch));


    entitas.game.getPlayerEntity()
            .addTextureView(null,new Bounds(0.8f,1.2f),false,false,1,1, Color.WHITE)
            .addAnimations(animationHero, walking, 0.02f)
            .addRigidBody(body);
}
 
Example #30
Source File: Mariano.java    From Entitas-Java with MIT License 5 votes vote down vote up
@Override
public GameEntity create(Engine engine, Context<GameEntity> context) {

    BasePhysicsManager physics = engine.getManager(BasePhysicsManager.class);
    BodyBuilder bodyBuilder =  physics.getBodyBuilder();

    TextureAtlas textureAtlas = assetsManager.getTextureAtlas(atlas);

    Array<TextureAtlas.AtlasRegion> heroWalking = textureAtlas.findRegions("Andando");
    Array<TextureAtlas.AtlasRegion> heroJump = textureAtlas.findRegions("Saltando");
    Array<TextureAtlas.AtlasRegion> heroFall = textureAtlas.findRegions("Cayendo");
    Array<TextureAtlas.AtlasRegion> heroIdle = textureAtlas.findRegions("Parado");

    Map<String, Animation<TextureRegion>> animationStates = Collections.createMap(String.class, Animation.class);
    animationStates.put("walking", new Animation(0.02f, heroWalking, Animation.PlayMode.LOOP));
    animationStates.put("jump", new Animation(0.02f * 7, heroJump, Animation.PlayMode.NORMAL));
    animationStates.put("fall", new Animation(0.02f * 5, heroFall, Animation.PlayMode.NORMAL));
    animationStates.put("idle", new Animation(0.02f * 4, heroIdle, Animation.PlayMode.LOOP));

    Body bodyPlayer = bodyBuilder.fixture(bodyBuilder.fixtureDefBuilder()
            .boxShape(0.35f, 1)
            .density(1))
            .type(BodyDef.BodyType.DynamicBody)
            .fixedRotation()
            .position(0, 5)
            .mass(1)
            .build();



    GameEntity entity = ((GameContext) context).getPlayerEntity()
            .addRigidBody(bodyPlayer)
            .addAnimations(animationStates, animationStates.get("idle"), 0)
            .addCharacter("Mariano", StateCharacter.IDLE, false)
            .addMovable(7,8)
            .addTextureView(null, new Bounds(0.9f,1.15f),false, false,1,1, Color.WHITE);


    return entity;
}