Java Code Examples for com.badlogic.gdx.physics.box2d.ContactImpulse#getNormalImpulses()

The following examples show how to use com.badlogic.gdx.physics.box2d.ContactImpulse#getNormalImpulses() . 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: 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);
	}
}