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

The following examples show how to use com.badlogic.gdx.physics.box2d.Body#getPosition() . 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: PhysicsConnector.java    From tilt-game-android with MIT License 6 votes vote down vote up
@Override
public void onUpdate(final float pSecondsElapsed) {
	final IEntity entity = this.mEntity;
	final Body body = this.mBody;

	if (this.mUpdatePosition) {
		final Vector2 position = body.getPosition();
		final float pixelToMeterRatio = this.mPixelToMeterRatio;
		entity.setPosition(position.x * pixelToMeterRatio, position.y * pixelToMeterRatio);
	}

	if (this.mUpdateRotation) {
		final float angle = body.getAngle();
		entity.setRotation(-MathUtils.radToDeg(angle));
	}
}
 
Example 2
Source File: Field2Delegate.java    From homescreenarcade with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a RotatingGroup by computing the distance and angle to center from the first
 * element ID in the ids array.
 */
public static RotatingGroup create(Field field, String[] ids, double cx, double cy, double speed) {
    FieldElement element = field.getFieldElementById(ids[0]);
    Body body = element.getBodies().get(0);
    Vector2 position = body.getPosition();
    double radius = Math.hypot(position.x - cx, position.y - cy);
    double angle = Math.atan2(position.y - cy, position.x - cx);
    return new RotatingGroup(ids, cx, cy, radius, angle, speed);
}
 
Example 3
Source File: BodyUtils.java    From martianrun with Apache License 2.0 5 votes vote down vote up
public static boolean bodyInBounds(Body body) {
    UserData userData = (UserData) body.getUserData();

    switch (userData.getUserDataType()) {
        case RUNNER:
        case ENEMY:
            return body.getPosition().x + userData.getWidth() / 2 > 0;
    }

    return true;
}
 
Example 4
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);
}
 
Example 5
Source File: SpritesSample.java    From Codelabs with MIT License 4 votes vote down vote up
@Override
public void render(float delta) {
	/* Clear screen with a black background */
	Gdx.gl.glClearColor(1, 1, 1, 1);
	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

	/* Render all graphics before do physics step */
	debugRenderer.render(world, camera.combined);

	/*
	 * Set projection matrix to camera.combined, the same way we did with
	 * the debug renderer.
	 */
	batch.setProjectionMatrix(camera.combined);
	batch.begin();

	/* Get word bodies */
	world.getBodies(worldBodies);

	/*
	 * For each body in the world we have to check if it has user data
	 * associated and if it is an Sprite. In that case, we draw it in the
	 * screen.
	 */
	for (Body body : worldBodies) {
		if (body.getUserData() instanceof Sprite) {
			Sprite sprite = (Sprite) body.getUserData();

			/*
			 * Set body position equals to box position. We also need to
			 * center it in the box (measures are relative to body center).
			 */
			Vector2 position = body.getPosition();
			sprite.setPosition(position.x - sprite.getWidth() / 2,
					position.y - sprite.getWidth() / 2);

			/* Set sprite rotation equals to body rotation */
			sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);

			/* Draw the sprite on screen */
			sprite.draw(batch);
		}
	}

	batch.end();

	/* Step the simulation with a fixed time step of 1/60 of a second */
	world.step(1 / 60f, 6, 2);
}
 
Example 6
Source File: Box2dLightTest.java    From box2dlights with Apache License 2.0 4 votes vote down vote up
@Override
public void render() {
	
	/** Rotate directional light like sun :) */
	if (lightsType == 3) {
		sunDirection += Gdx.graphics.getDeltaTime() * 4f;
		lights.get(0).setDirection(sunDirection);
	}

	camera.update();

	boolean stepped = fixedStep(Gdx.graphics.getDeltaTime());
	Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1);
	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

	batch.setProjectionMatrix(camera.combined);
	batch.disableBlending();
	batch.begin();
	{
		batch.draw(bg, -viewportWidth / 2f, 0, viewportWidth, viewportHeight);
		batch.enableBlending();
		for (int i = 0; i < BALLSNUM; i++) {
			Body ball = balls.get(i);
			Vector2 position = ball.getPosition();
			float angle = MathUtils.radiansToDegrees * ball.getAngle();
			batch.draw(
					textureRegion,
					position.x - RADIUS, position.y - RADIUS,
					RADIUS, RADIUS,
					RADIUS * 2, RADIUS * 2,
					1f, 1f,
					angle);
		}
	}
	batch.end();

	/** BOX2D LIGHT STUFF BEGIN */
	rayHandler.setCombinedMatrix(camera);

	if (stepped) rayHandler.update();
	rayHandler.render();
	/** BOX2D LIGHT STUFF END */

	long time = System.nanoTime();

	boolean atShadow = rayHandler.pointAtShadow(testPoint.x,
			testPoint.y);
	aika += System.nanoTime() - time;
     
	/** FONT */
	if (showText) {
		batch.setProjectionMatrix(normalProjection);
		batch.begin();
		
		font.draw(batch,
				"F1 - PointLight",
				0, Gdx.graphics.getHeight());
		font.draw(batch,
				"F2 - ConeLight",
				0, Gdx.graphics.getHeight() - 15);
		font.draw(batch,
				"F3 - ChainLight",
				0, Gdx.graphics.getHeight() - 30);
		font.draw(batch,
				"F4 - DirectionalLight",
				0, Gdx.graphics.getHeight() - 45);
		font.draw(batch,
				"F5 - random lights colors",
				0, Gdx.graphics.getHeight() - 75);
		font.draw(batch,
				"F6 - random lights distance",
				0, Gdx.graphics.getHeight() - 90);
		font.draw(batch,
				"F9 - default blending (1.3)",
				0, Gdx.graphics.getHeight() - 120);
		font.draw(batch,
				"F10 - over-burn blending (default in 1.2)",
				0, Gdx.graphics.getHeight() - 135);
		font.draw(batch,
				"F11 - some other blending",
				0, Gdx.graphics.getHeight() - 150);
		
		font.draw(batch,
				"F12 - toggle help text",
				0, Gdx.graphics.getHeight() - 180);

		font.draw(batch,
				Integer.toString(Gdx.graphics.getFramesPerSecond())
				+ "mouse at shadows: " + atShadow
				+ " time used for shadow calculation:"
				+ aika / ++times + "ns" , 0, 20);

		batch.end();
	}
}