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

The following examples show how to use com.badlogic.gdx.physics.box2d.Box2DDebugRenderer. 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: DebugRendererSystem.java    From Entitas-Java with MIT License 6 votes vote down vote up
public DebugRendererSystem(Entitas entitas, World world, Batch batch) {
    this.physics = world;
    this.entitas = entitas;
    this.batch = batch;

    this.shapeRenderer = new ShapeRenderer();
    this.debugRenderer = new Box2DDebugRenderer(DRAW_BOX2D_BODIES, DRAW_BOX2D_JOINTS, DRAW_BOX2D_ABBs,
            DRAW_BOX2D_INACTIVE_BODIES, DRAW_BOX2D_VELOCITIES, DRAW_BOX2D_CONTACTS);
    debugRenderer.setDrawAABBs(DRAW_BOX2D_ABBs);
    debugRenderer.setDrawBodies(DRAW_BOX2D_BODIES);
    debugRenderer.setDrawContacts(DRAW_BOX2D_CONTACTS);
    debugRenderer.setDrawInactiveBodies(DRAW_BOX2D_INACTIVE_BODIES);
    debugRenderer.setDrawJoints(DRAW_BOX2D_JOINTS);
    debugRenderer.setDrawVelocities(DRAW_BOX2D_VELOCITIES);

}
 
Example #2
Source File: RendererSystem.java    From Entitas-Java with MIT License 6 votes vote down vote up
public RendererSystem(Entitas entitas, Camera cam, Batch batch, World world) {
    this.physics = world;
    this.cam = cam;
    this.batch = batch;
    this.entitas = entitas;
    _groupTextureView = entitas.game.getGroup(GameMatcher.TextureView());
    this.debugShapeRenderer = new ShapeRenderer();
    this.debugRenderer = new Box2DDebugRenderer(DRAW_BOX2D_BODIES, DRAW_BOX2D_JOINTS, DRAW_BOX2D_ABBs,
            DRAW_BOX2D_INACTIVE_BODIES, DRAW_BOX2D_VELOCITIES, DRAW_BOX2D_CONTACTS);
    debugRenderer.setDrawAABBs(DRAW_BOX2D_ABBs);
    debugRenderer.setDrawBodies(DRAW_BOX2D_BODIES);
    debugRenderer.setDrawContacts(DRAW_BOX2D_CONTACTS);
    debugRenderer.setDrawInactiveBodies(DRAW_BOX2D_INACTIVE_BODIES);
    debugRenderer.setDrawJoints(DRAW_BOX2D_JOINTS);
    debugRenderer.setDrawVelocities(DRAW_BOX2D_VELOCITIES);
    this.inputs = entitas.input.getGroup(InputMatcher.Input());
}
 
Example #3
Source File: DebugHelper.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
public DebugHelper (GameWorld gameWorld, PostProcessor postProcessor, LapManager lapManager, GameLogic logic, Input input) {
	this.gameWorld = gameWorld;
	this.postProcessor = postProcessor;
	this.lapManager = lapManager;
	this.logic = logic;
	this.input = input;

	this.enabled = Config.Debug.UseDebugHelper;
	this.box2dWorld = gameWorld.getBox2DWorld();

	GameEvents.gameRenderer.addListener(renderListener, GameRendererEvent.Type.BatchDebug, GameRendererEvent.Order.PLUS_4);
	GameEvents.gameRenderer.addListener(renderListener, GameRendererEvent.Type.Debug, GameRendererEvent.Order.PLUS_4);

	// player = null;
	b2drenderer = new Box2DDebugRenderer();

	// extrapolate version information
	uRacerInfo = "URacer " + URacer.versionInfo;

	// compute graphics stats size
	float updateHz = 60f;
	if (!URacer.Game.isDesktop()) {
		updateHz = 5f;
	}

	int sw = MathUtils.clamp(uRacerInfo.length() * Art.DebugFontWidth, 100, 500);
	stats = new DebugStatistics(sw, 100, updateHz);

	for (int i = 0; i < ReplayManager.MaxReplays + 1; i++) {
		ranks.add(new RankInfo());
	}
}
 
Example #4
Source File: Box2DDebugger.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
protected void initialize() {
  setEnabled(false);
  renderer = new Box2DDebugRenderer();
  camera = new OrthographicCamera();
  camera.setToOrtho(true);
  camera.near = -1024;
  camera.far  =  1024;
  camera.rotate(Vector3.X, -60);
  camera.rotate(Vector3.Z, -45);
}
 
Example #5
Source File: FluidSimulatorGeneric.java    From fluid-simulator-v2 with Apache License 2.0 5 votes vote down vote up
public FluidSimulatorGeneric(FluidSimulatorStarter fluidSimulatorStarter) {
		this.game = fluidSimulatorStarter;
		// LibGDX single batches cannot have a size more than 5460
		batch = new SpriteBatch(IS_DESKTOP ? 5460 : ANDROID_SIZE);
		font = new BitmapFont();
		camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
		camera.position.set(0, (WORLD_HEIGHT / 2) - 1, 0);
		immediateRenderer = new ImmediateModeRenderer20(SIZE*6, false, true, 0);
		irt = new Renderer20(SIZE*6, false, true, 1);
		irt2 = new ImmediateModeRenderer20(SIZE*11, false, true, 1);
		shapeRenderer = new ShapeRenderer(SIZE);
		renderer = new Box2DDebugRenderer(true, true, false, true, false, false);
		
		//3D
		camera3D = new PerspectiveCamera(67, WORLD_WIDTH, WORLD_HEIGHT);
		camera3D.position.set(0, 130f, 250f);
		camera3D.lookAt(0,150f,0);
		camera3D.near = 0.1f;
		camera3D.far = 500f;
		camera3D.update();
		ModelBuilder modelBuilder = new ModelBuilder();
//        model = modelBuilder.createSphere(5f, 5f, 5f, 4, 4, GL10.GL_TRIANGLES,
//                new Material(ColorAttribute.createDiffuse(Color.GREEN)),
//                Usage.Position | Usage.Normal);
        model = modelBuilder.createBox(5f, 5f, 5f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);
        instance = new ModelInstance(model);
        modelBatch = new ModelBatch();
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, 0, -0.8f, -0.2f));
        camController = new Camera3DController(camera3D);
        camController.setFluidSimulator(this);
		
		world = new World(new Vector2(0, -9.8f), false);
		world.setContactListener(this);
	}
 
Example #6
Source File: PlayScreen.java    From Pacman_libGdx with MIT License 4 votes vote down vote up
@Override
public void show() {
    camera = new OrthographicCamera();
    viewport = new FitViewport(WIDTH, HEIGHT, camera);
    camera.translate(WIDTH / 2, HEIGHT / 2);
    camera.update();

    batch = new SpriteBatch();

    playerSystem = new PlayerSystem();
    ghostSystem = new GhostSystem();
    movementSystem = new MovementSystem();
    pillSystem = new PillSystem();
    animationSystem = new AnimationSystem();
    renderSystem = new RenderSystem(batch);
    stateSystem = new StateSystem();

    engine = new Engine();
    engine.addSystem(playerSystem);
    engine.addSystem(ghostSystem);
    engine.addSystem(pillSystem);
    engine.addSystem(movementSystem);
    engine.addSystem(stateSystem);
    engine.addSystem(animationSystem);
    engine.addSystem(renderSystem);

    // box2d
    world = new World(Vector2.Zero, true);
    world.setContactListener(new WorldContactListener());
    box2DDebugRenderer = new Box2DDebugRenderer();
    showBox2DDebuggerRenderer = false;

    // box2d light
    rayHandler = new RayHandler(world);
    rayHandler.setAmbientLight(ambientLight);

    // load map
    tiledMap = new TmxMapLoader().load("map/map.tmx");
    tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap, 1 / 16f, batch);

    new WorldBuilder(tiledMap, engine, world, rayHandler).buildAll();

    stageViewport = new FitViewport(WIDTH * 20, HEIGHT * 20);
    stage = new Stage(stageViewport, batch);

    font = new BitmapFont(Gdx.files.internal("fonts/army_stencil.fnt"));
    Label.LabelStyle labelStyle = new Label.LabelStyle(font, Color.WHITE);

    Label scoreTextLabel = new Label("SCORE", labelStyle);
    scoreTextLabel.setPosition(WIDTH * 1, HEIGHT * 19);
    stage.addActor(scoreTextLabel);

    Label hightScoreTextLabel = new Label("High Score", labelStyle);
    hightScoreTextLabel.setPosition(WIDTH * 14, HEIGHT * 19);
    stage.addActor(hightScoreTextLabel);

    scoreLabel = new Label("0", labelStyle);
    scoreLabel.setPosition(WIDTH * 1.5f, HEIGHT * 18.2f);
    stage.addActor(scoreLabel);

    highScoreLabel = new Label("0", labelStyle);
    highScoreLabel.setPosition(WIDTH * 16.5f, HEIGHT * 18.2f);
    stage.addActor(highScoreLabel);

    gameOverLabel = new Label("              - Game Over -\n Press Enter to continue", labelStyle);
    gameOverLabel.setPosition(WIDTH * 4.3f, HEIGHT * 9f);
    gameOverLabel.setVisible(false);
    stage.addActor(gameOverLabel);

    TextureAtlas textureAtlas = GameManager.instance.assetManager.get("images/actors.pack", TextureAtlas.class);
    pacmanSprite = new Sprite(new TextureRegion(textureAtlas.findRegion("Pacman"), 16, 0, 16, 16));
    pacmanSprite.setBounds(8f, 21.5f, 16 / GameManager.PPM, 16 / GameManager.PPM);

    stringBuilder = new StringBuilder();

    changeScreen = false;
}
 
Example #7
Source File: ImpulsesSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/*
	 * Next line must remain commented because we do this in its parent (See
	 * Sample class). In case you are not using Sample class, uncomment this
	 * line to set input processor to handle events.
	 */
	// Gdx.input.setInputProcessor(this);

	/* Create the box */
	Shape shape = Box2DFactory.createBoxShape(1.5f, 1.5f,
			new Vector2(0, 0), 0);
	FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 0.3f, 0.5f,
			0.5f, false);
	box = Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef,
			new Vector2(0, 0));

	/* Create the walls */
	Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);
}
 
Example #8
Source File: JumpingSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/*
	 * Next line must remain commented because we do this in its parent (See
	 * Sample class). In case you are not using Sample class, uncomment this
	 * line to set input processor to handle events.
	 */
	// Gdx.input.setInputProcessor(this);

	/* Create the player */
	Shape shape = Box2DFactory.createBoxShape(0.35f, 1, new Vector2(0, 0),
			0);
	FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 1, 0, 0,
			false);
	player = Box2DFactory.createBody(world, BodyType.DynamicBody,
			fixtureDef, new Vector2(0, 0));

	/*
	 * Create foot sensor. This sensor will be used to detect when the
	 * player is standing on something.
	 */
	shape = Box2DFactory.createBoxShape(0.1f, 0.1f, new Vector2(0, -1), 0);
	fixtureDef = Box2DFactory.createFixture(shape, 0, 0, 0, true);

	/*
	 * Set user data to the player. In this case we are using an integer.
	 * This will help us to identify the fixture during collision handling.
	 */
	player.createFixture(fixtureDef).setUserData(new Integer(3));

	/*
	 * Fix the rotation of player's body. We don't want our player to fall
	 * every time he/she touch an object :)
	 */
	player.setFixedRotation(true);

	/* Create the walls */
	Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);

	world.setContactListener(this);
}
 
Example #9
Source File: SpawnBodiesSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/*
	 * Next line must remain commented because we do this in its parent (See
	 * Sample class). In case you are not using Sample class, uncomment this
	 * line to set input processor to handle events.
	 */
	// Gdx.input.setInputProcessor(this);

	/* Create the walls */
	Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);
}
 
Example #10
Source File: DragAndDropSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/*
	 * Next line must remain commented because we do this in its parent (See
	 * Sample class). In case you are not using Sample class, uncomment this
	 * line to set input processor to handle events.
	 */
	// Gdx.input.setInputProcessor(this);

	/*
	 * Instantiate the vector that will be used to store click/touch
	 * positions.
	 */
	touchPosition = new Vector3();

	/* Create the ball */
	Shape shape = Box2DFactory.createCircleShape(1);
	FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f,
			0.75f, false);
	Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef,
			new Vector2(0, 0));

	/* Create the walls */
	Body walls = Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);

	/* Define the mouse joint. We use walls as the first body of the joint */
	createMouseJointDefinition(walls);
}
 
Example #11
Source File: GravityAccelerometerSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage. As we use the accelerometer and the world
	 * gravity to change bodies positions, we can't let bodies to sleep.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/*
	 * Next line must remain commented because we do this in its parent (See
	 * Sample class). In case you are not using Sample class, uncomment this
	 * line to set input processor to handle events.
	 */
	// Gdx.input.setInputProcessor(this);

	/* Create the walls */
	Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);
}
 
Example #12
Source File: SpritesSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	batch = new SpriteBatch();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/*
	 * Next line must remain commented because we do this in its parent (See
	 * Sample class). In case you are not using Sample class, uncomment this
	 * line to set input processor to handle events.
	 */
	// Gdx.input.setInputProcessor(this);

	/* Create the box */
	Shape shape = Box2DFactory.createBoxShape(1.5f, 1.5f,
			new Vector2(0, 0), 0);
	FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 0.3f, 0.5f,
			0.5f, false);
	box = Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef,
			new Vector2(0, 0));

	/* Create the walls */
	Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);

	/* Set box texture */
	sprite = new Sprite(new Texture("data/images/crab.png"));

	/*
	 * Set the size of the sprite. We have to remember that we are not
	 * working in pixels, but with meters. The size of the sprite will be
	 * the same as the size of the box; 2 meter wide, 2 meters tall.
	 */
	sprite.setSize(3, 3);

	/*
	 * Sets the origin in relation to the sprite's position for scaling and
	 * rotation.
	 */
	sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);

	/* Set sprite as a user data of the body to draw it in each render step */
	box.setUserData(sprite);

	/* Instantiate the array of bodies that will be used during render step */
	worldBodies = new Array<Body>();
}
 
Example #13
Source File: CollisionsSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage. As we use the accelerometer and the world
	 * gravity to change bodies positions, we can't let bodies to sleep.
	 */
	world = new World(new Vector2(0, -9.81f), false);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/* Create the ball */
	Shape shape = Box2DFactory.createCircleShape(1);
	FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f,
			0.75f, false);
	Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef,
			new Vector2(5, 0));

	/* Create the box */
	shape = Box2DFactory.createBoxShape(0.5f, 0.5f, new Vector2(0, 0), 0);
	fixtureDef = Box2DFactory.createFixture(shape, 1, 0.5f, 0.5f, false);
	Box2DFactory.createBody(world, BodyType.StaticBody, fixtureDef,
			new Vector2(0, 0));

	/* Create the walls */
	walls = Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);

	world.setContactListener(this);
}
 
Example #14
Source File: BuoyancySample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the
	 * sample functionality. calls Sample.show() method. That method set the
	 * sample to receive all touch and key input events. Also prevents the
	 * app from be closed whenever the user press back button and instead
	 * returns to main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/*
	 * Next line must remain commented because we do this in its parent (See
	 * Sample class). In case you are not using Sample class, uncomment this
	 * line to set input processor to handle events.
	 */
	// Gdx.input.setInputProcessor(this);

	Box2DFactory.createWalls(world, camera.viewportWidth,
			camera.viewportHeight, 1);

	/* Create the water */
	Shape shape = Box2DFactory.createBoxShape(
			(camera.viewportWidth / 2 - 1),
			(camera.viewportHeight / 2 - 2) / 2, new Vector2(0, 0), 0);
	FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 1, 0.1f, 0,
			true);
	Body water = Box2DFactory.createBody(world, BodyType.StaticBody,
			fixtureDef, new Vector2(0,
					-(camera.viewportHeight / 2 - 1) / 2 - 0.5f));

	/*
	 * Create a buoyancy controller using the previous body as a fluid
	 * sensor
	 */
	buoyancyController = new BuoyancyController(world, water
			.getFixtureList().first());

	world.setContactListener(this);
}
 
Example #15
Source File: BouncingBallSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void show() {
	/*
	 * This line is found in every sample but is not necessary for the sample
	 * functionality. calls Sample.show() method. That method set the sample to
	 * receive all touch and key input events. Also prevents the app from be
	 * closed whenever the user press back button and instead returns to
	 * main menu.
	 */
	super.show();

	/*
	 * Create world with a common gravity vector (9.81 m/s2 downwards force)
	 * and tell world that we want objects to sleep. This last value
	 * conserves CPU usage.
	 */
	world = new World(new Vector2(0, -9.81f), true);

	/* Create renderer */
	debugRenderer = new Box2DDebugRenderer();

	/*
	 * Define camera viewport. Box2D uses meters internally so the camera
	 * must be defined also in meters. We set a desired width and adjust
	 * height to different resolutions.
	 */
	camera = new OrthographicCamera(20,
			20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
					.getWidth()));

	/* Create the ball */
	Shape shape = Box2DFactory.createCircleShape(0.5f);
	FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false);
	Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(6, 5));
	
	/* Create the ramp */
	Vector2[] rampVertices = new Vector2[] { new Vector2(-2.5f, -1), new Vector2(2.5f, 1) };
	shape = Box2DFactory.createChainShape(rampVertices);
	fixtureDef = Box2DFactory.createFixture(shape, 0, 0.3f, 0.3f, false);
	Box2DFactory.createBody(world, BodyType.StaticBody, fixtureDef, new Vector2(6.5f, 0));
	
	/* Create the walls */
	Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1);
}
 
Example #16
Source File: LevelPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
public LevelPhysicsProcessor(final World world, final Map map, final float unitScale) {
	b2dRenderer = new Box2DDebugRenderer();
	mapObjectListener = new Box2DMapObjectListener();
	Box2DMapObjectParser objectParser = new Box2DMapObjectParser(mapObjectListener, unitScale);
	objectParser.load(world, map);
}