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

The following examples show how to use com.badlogic.gdx.physics.box2d.Contact. 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: JumpingSample.java    From Codelabs with MIT License 6 votes vote down vote up
@Override
public void endContact(Contact contact) {
	/*
	 * For each fixture contacting (A and B) check if it has user data
	 * associated. In that case and only if it is the number 3 that we
	 * previously associated to our player, change isPlayerGrounded false to
	 * indicate that our player is jumping.
	 */
	Object userData = contact.getFixtureA().getUserData();
	if (userData != null && (Integer) userData == 3) {
		isPlayerGrounded = false;
		hasDoubleJump = false;
	} else {
		userData = contact.getFixtureB().getUserData();
		if (userData != null && (Integer) userData == 3) {
			isPlayerGrounded = false;
		}
	}
}
 
Example #2
Source File: JumpingSample.java    From Codelabs with MIT License 6 votes vote down vote up
@Override
public void beginContact(Contact contact) {
	/*
	 * For each fixture contacting (A and B) check if it has user data
	 * associated. In that case and only if it is the number 3 that we
	 * previously associated to our player, change isPlayerGrounded and
	 * hasDobleJump variables to true.
	 */
	Object userData = contact.getFixtureA().getUserData();
	if (userData != null && (Integer) userData == 3) {
		isPlayerGrounded = true;
		hasDoubleJump = true;
	} else {
		userData = contact.getFixtureB().getUserData();
		if (userData != null && (Integer) userData == 3) {
			isPlayerGrounded = true;
			hasDoubleJump = true;
		}
	}
}
 
Example #3
Source File: Field.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
@Override public void endContact(Contact contact) {
    Fixture fixture = null;
    Ball ball = ballWithBody(contact.getFixtureA().getBody());
    if (ball != null) {
        fixture = contact.getFixtureB();
    }
    else {
        ball = ballWithBody(contact.getFixtureB().getBody());
        if (ball != null) {
            fixture = contact.getFixtureA();
        }
    }

    if (ball != null) {
        contactedBalls.add(ball);
        contactedFixtures.add(fixture);
    }
}
 
Example #4
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 #5
Source File: CollisionsSample.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 one of the fixture contacting is an static body, and is the wall,
	 * set ballTouchedWall to true.
	 */
	if (fixtureA.getBody().getType() == BodyType.StaticBody) {
		if (fixtureA.getBody().equals(walls)) {
			ballTouchedBox = false;
			ballTouchedWall = true;
		} else {
			ballTouchedBox = true;
			ballTouchedWall = false;
		}
	} else if (fixtureB.getBody().getType() == BodyType.StaticBody) {
		if (fixtureA.getBody().equals(walls)) {
			ballTouchedBox = false;
			ballTouchedWall = true;
		} else {
			ballTouchedBox = true;
			ballTouchedWall = false;
		}
	}
}
 
Example #6
Source File: BaseLevelController.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
public void init(GameLevel gameLevel, PhysicsWorld physicsWorld, Engine engine) {
    _gameLevel = gameLevel;
    _physicsWorld = physicsWorld;
    _engine = engine;

    _contactListener = new ContactListenerAdapter() {
        @Override
        public void beginContact(Contact contact) {
            checkCollision(contact);
        }
    };
    _physicsWorld.setContactListener(_contactListener);
}
 
Example #7
Source File: GameLevelController.java    From tilt-game-android with MIT License 5 votes vote down vote up
private void checkCollisionSound(Contact contact, Body bodyA, Body bodyB) {
    WorldManifold manifold = contact.getWorldManifold();
    Vector2 contactPoint = manifold.getPoints()[0];
    Vector2 vel1 = bodyA.getLinearVelocityFromWorldPoint(contactPoint);
    Vector2 vel2 = bodyB.getLinearVelocityFromWorldPoint(contactPoint);
    Vector2 impactVelocity = vel1.sub(vel2);
    float impactLen = impactVelocity.len();
    if (impactLen > MIN_BALL_SOUND_SPEED) {
        float dot = Math.abs(impactVelocity.dot(manifold.getNormal()));
        if (dot > MIN_IMPACT_SOUND_SPEED) {
            float volume = (float) Math.min((dot - MIN_IMPACT_SOUND_SPEED) / MAX_IMPACT_SOUND_SPEED, 1.0);
            SoundManager.getInstance().play(R.raw.bounce_1, volume);
        }
    }
}
 
Example #8
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 #9
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 #10
Source File: CarrotPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void beginContact(final Contact contact) {
	if (CARROT_IDENTIFIER.equals(contact.getFixtureA().getUserData())) {
		collectCarrot(contact.getFixtureA());
	} else if (CARROT_IDENTIFIER.equals(contact.getFixtureB().getUserData())) {
		collectCarrot(contact.getFixtureB());
	}
}
 
Example #11
Source File: CarImpactManager.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
@Override
public void process (Contact contact, ContactImpulse impulse) {
	Fixture a = contact.getFixtureA();
	Fixture b = contact.getFixtureB();

	ifCarThenCollide(contact, a, b, impulse);
	ifCarThenCollide(contact, b, a, impulse);
}
 
Example #12
Source File: NinjaRabbitPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void beginContact(final Contact contact) {
	// Foot sensor is touching the ground
	if (FOOT_IDENTIFIER.equals(contact.getFixtureA().getUserData()) ||
			FOOT_IDENTIFIER.equals(contact.getFixtureB().getUserData()) &&
			(GROUND_IDENTIFIER.equals(contact.getFixtureA().getUserData()) ||
			GROUND_IDENTIFIER.equals(contact.getFixtureB().getUserData()))) {
		groundContacts++;
	}
}
 
Example #13
Source File: NinjaRabbitPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void endContact(final Contact contact) {
	if (FOOT_IDENTIFIER.equals(contact.getFixtureA().getUserData()) ||
			FOOT_IDENTIFIER.equals(contact.getFixtureB().getUserData()) &&
			(GROUND_IDENTIFIER.equals(contact.getFixtureA().getUserData()) ||
			GROUND_IDENTIFIER.equals(contact.getFixtureB().getUserData()))) {
		groundContacts--;
	}

}
 
Example #14
Source File: B2DWorldContactListener.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
 
Example #15
Source File: GameWorldContactListener.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
@Override
public void beginContact (Contact contact) {
	// if(Box2DUtils.isCar(contact.getFixtureA()) || Box2DUtils.isCar(contact.getFixtureB()))
	// System.out.println("beginContact");
}
 
Example #16
Source File: B2DWorldContactListener.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
 
Example #17
Source File: LevelPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void postSolve(final Contact contact, final ContactImpulse impulse) {
	// TODO Auto-generated method stub
}
 
Example #18
Source File: ContactListenerMultiplexer.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void preSolve(final Contact contact, final Manifold oldManifold) {
	for (ContactListener listener : receivers) {
		listener.preSolve(contact, oldManifold);
	}
}
 
Example #19
Source File: ContactListenerMultiplexer.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void beginContact(final Contact contact) {
	for (ContactListener listener : receivers) {
		listener.beginContact(contact);
	}
}
 
Example #20
Source File: PhysicsWorld.java    From tilt-game-android with MIT License 4 votes vote down vote up
public List<Contact> getContactList() {
	return this.mWorld.getContactList();
}
 
Example #21
Source File: LevelPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void preSolve(final Contact contact, final Manifold oldManifold) {
	// TODO Auto-generated method stub
}
 
Example #22
Source File: LevelPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void endContact(final Contact contact) {
	// TODO Auto-generated method stub
}
 
Example #23
Source File: NinjaRabbitPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void preSolve(final Contact contact, final Manifold oldManifold) {
	// TODO Auto-generated method stub
}
 
Example #24
Source File: NinjaRabbitPhysicsProcessor.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void postSolve(final Contact contact, final ContactImpulse impulse) {
	// TODO Auto-generated method stub
}
 
Example #25
Source File: Field.java    From homescreenarcade with GNU General Public License v3.0 4 votes vote down vote up
@Override public void preSolve(Contact arg0, Manifold arg1) {
    // Not used.
}
 
Example #26
Source File: GameWorldContactListener.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
@Override
public void endContact (Contact contact) {
	// if(Box2DUtils.isCar(contact.getFixtureA()) || Box2DUtils.isCar(contact.getFixtureB()))
	// System.out.println("endContact");
}
 
Example #27
Source File: GameWorldContactListener.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
@Override
public void preSolve (Contact contact, Manifold oldManifold) {
}
 
Example #28
Source File: GameWorldContactListener.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
@Override
public void postSolve (Contact contact, ContactImpulse impulse) {
	impactManager.process(contact, impulse);
}
 
Example #29
Source File: ContactListenerMultiplexer.java    From ninja-rabbit with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void endContact(final Contact contact) {
	for (ContactListener listener : receivers) {
		listener.endContact(contact);
	}
}
 
Example #30
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);
	}
}