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

The following examples show how to use com.badlogic.gdx.physics.box2d.Body#getUserData() . 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: GameLevelController.java    From tilt-game-android with MIT License 5 votes vote down vote up
private boolean checkContact(Contact contact) {
    // ignore not touching contacts
    if (!contact.isTouching()) return false;

    Fixture fixtureA = contact.getFixtureA();
    if (fixtureA == null) return false;

    Fixture fixtureB = contact.getFixtureB();
    if (fixtureB == null) return false;

    Body bodyA = fixtureA.getBody();
    Body bodyB = fixtureB.getBody();

    String nameA = (String) bodyA.getUserData();
    String nameB = (String) bodyB.getUserData();

    if (nameA == null || nameB == null) {               // nameless bodies, check if sound needs to be played
        checkCollisionSound(contact, bodyA, bodyB);
        return false;
    } else if (nameA.equals(ObjectName.EDGE_SENSOR_NAME) || nameB.equals(ObjectName.EDGE_SENSOR_NAME)) {    // check edges
        Log.d(TAG, "onUpdate: OFF THE EDGE");

        setBallOut();

        return true;
    } else if (nameA.equals(ObjectName.SINKHOLE_NAME) || nameB.equals(ObjectName.SINKHOLE_NAME)) {  // check sinkhole
        Log.d(TAG, "onUpdate: SINKHOLE");

        setLevelComplete();

        return true;
    }

    return false;
}
 
Example 2
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 3
Source File: CarImpactManager.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
private void ifCarThenCollide (Contact contact, Fixture f, Fixture other, ContactImpulse impulse) {
	Body body = f.getBody();
	Object userData = f.getUserData();
	if ((body != null) && (userData == CarType.PlayerCar || userData == CarType.ReplayCar)) {
		Car car = (Car)body.getUserData();
		float[] impulses = impulse.getNormalImpulses();
		tmpVec2.set(impulses[0], impulses[1]);

		// dbg
		Fixture fcar = null;
		if (contact.getFixtureA().getUserData() == CarType.PlayerCar) {
			fcar = contact.getFixtureA();
		} else if (contact.getFixtureB().getUserData() == CarType.PlayerCar) {
			fcar = contact.getFixtureB();
		}

		// assumes perfect side collision
		float front_ratio = 0.5f;

		// compute median front/rear ratio for collision points
		if (fcar != null) {
			front_ratio = 0;
			float ml = car.getCarModel().length;
			float half_ml = ml * 0.5f;
			Vector2[] pts = contact.getWorldManifold().getPoints();

			int num_points = contact.getWorldManifold().getNumberOfContactPoints();
			for (int i = 0; i < num_points; i++) {
				Vector2 lp = fcar.getBody().getLocalPoint(pts[i]);

				// examine front/rear ratio
				float r = MathUtils.clamp(lp.y + half_ml, 0, ml);
				r /= ml;
				front_ratio += r;
			}

			front_ratio /= (float)num_points;
			// Gdx.app.log("Cntct", "" + front_ratio);
		}
		// dbg

		car.onCollide(other, tmpVec2, front_ratio);
	}
}
 
Example 4
Source File: GameActor.java    From martianrun with Apache License 2.0 4 votes vote down vote up
public GameActor(Body body) {
    this.body = body;
    this.userData = (UserData) body.getUserData();
    screenRectangle = new Rectangle();
}
 
Example 5
Source File: BodyUtils.java    From martianrun with Apache License 2.0 4 votes vote down vote up
public static boolean bodyIsEnemy(Body body) {
    UserData userData = (UserData) body.getUserData();

    return userData != null && userData.getUserDataType() == UserDataType.ENEMY;
}
 
Example 6
Source File: BodyUtils.java    From martianrun with Apache License 2.0 4 votes vote down vote up
public static boolean bodyIsRunner(Body body) {
    UserData userData = (UserData) body.getUserData();

    return userData != null && userData.getUserDataType() == UserDataType.RUNNER;
}
 
Example 7
Source File: BodyUtils.java    From martianrun with Apache License 2.0 4 votes vote down vote up
public static boolean bodyIsGround(Body body) {
    UserData userData = (UserData) body.getUserData();

    return userData != null && userData.getUserDataType() == UserDataType.GROUND;
}
 
Example 8
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);
}