com.badlogic.gdx.physics.box2d.joints.MouseJoint Java Examples

The following examples show how to use com.badlogic.gdx.physics.box2d.joints.MouseJoint. 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: InputManagerGDX.java    From Entitas-Java with MIT License 5 votes vote down vote up
private void testPoint(Fixture fixture, PointerState<Vector2,Vector3> pointerState) {
    if (!fixture.testPoint(pointerState.coordinates.x, pointerState.coordinates.y))
        return;
    Integer index = (Integer) fixture.getBody().getUserData();
    GameEntity entity =  Indexed.getInteractiveEntity(index);
    if(entity.isDraggable()) {
        jointDef.bodyB = fixture.getBody();
        jointDef.target.set(pointerState.coordinates.x, pointerState.coordinates.y);
        joints[pointerState.pointer] = (MouseJoint) physics.createJoint(jointDef);
    }
}
 
Example #2
Source File: InputManagerGDX.java    From Entitas-Java with MIT License 5 votes vote down vote up
@Override
public void initialize() {
    if (engine.getManager(PhysicsManagerGDX.class) == null) throw new EntitasException("InputManagerGDX",
            "InputManagerGDX needs load PreferencesManagerGDX on the engine");
    this.camera = engine.getCamera();
    this.physics = engine.getPhysics();
    joints =  new MouseJoint[5];
    inputStateData = new InputStateData();

}
 
Example #3
Source File: DragAndDropSample.java    From Codelabs with MIT License 5 votes vote down vote up
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
	/*
	 * Define a new QueryCallback. This callback will be used in
	 * world.QueryAABB method.
	 */
	QueryCallback queryCallback = new QueryCallback() {

		@Override
		public boolean reportFixture(Fixture fixture) {
			boolean testResult;

			/*
			 * If the hit point is inside the fixture of the body, create a
			 * new MouseJoint.
			 */
			if (testResult = fixture.testPoint(touchPosition.x,
					touchPosition.y)) {
				mouseJointDef.bodyB = fixture.getBody();
				mouseJointDef.target.set(touchPosition.x, touchPosition.y);
				mouseJoint = (MouseJoint) world.createJoint(mouseJointDef);
			}

			return testResult;
		}
	};

	/* Translate camera point to world point */
	camera.unproject(touchPosition.set(screenX, screenY, 0));

	/*
	 * Query the world for all fixtures that potentially overlap the touched
	 * point.
	 */
	world.QueryAABB(queryCallback, touchPosition.x, touchPosition.y,
			touchPosition.x, touchPosition.y);

	return true;
}
 
Example #4
Source File: Box2dLightCustomShaderTest.java    From box2dlights with Apache License 2.0 5 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int newParam) {
	// translate the mouse coordinates to world coordinates
	testPoint.set(x, y, 0);
	camera.unproject(testPoint);

	// ask the world which bodies are within the given
	// bounding box around the mouse pointer
	hitBody = null;
	world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f,
			testPoint.x + 0.1f, testPoint.y + 0.1f);

	// if we hit something we create a new mouse joint
	// and attach it to the hit body.
	if (hitBody != null) {
		MouseJointDef def = new MouseJointDef();
		def.bodyA = groundBody;
		def.bodyB = hitBody;
		def.collideConnected = true;
		def.target.set(testPoint.x, testPoint.y);
		def.maxForce = 1000.0f * hitBody.getMass();

		mouseJoint = (MouseJoint) world.createJoint(def);
		hitBody.setAwake(true);
	}

	return false;
}
 
Example #5
Source File: Box2dLightTest.java    From box2dlights with Apache License 2.0 5 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int newParam) {
	// translate the mouse coordinates to world coordinates
	testPoint.set(x, y, 0);
	camera.unproject(testPoint);

	// ask the world which bodies are within the given
	// bounding box around the mouse pointer
	hitBody = null;
	world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f,
			testPoint.x + 0.1f, testPoint.y + 0.1f);

	// if we hit something we create a new mouse joint
	// and attach it to the hit body.
	if (hitBody != null) {
		MouseJointDef def = new MouseJointDef();
		def.bodyA = groundBody;
		def.bodyB = hitBody;
		def.collideConnected = true;
		def.target.set(testPoint.x, testPoint.y);
		def.maxForce = 1000.0f * hitBody.getMass();

		mouseJoint = (MouseJoint) world.createJoint(def);
		hitBody.setAwake(true);
	}

	return false;
}
 
Example #6
Source File: FluidSimulatorLiquid.java    From fluid-simulator-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
	touching = true;
	camera.unproject(testPoint.set(x, y, 0));
	testPoint2D.x = testPoint.x;
	testPoint2D.y = testPoint.y;
	oldDragPos.set(testPoint2D);
	
	if (button == Buttons.LEFT) {

		// Drag Mode
		if (isDragging) {
			for (Piece piece : pieces.values()) {
				hitBody = null;
				if (piece.body.getFixtureList().get(0).testPoint(testPoint2D)) {
					hitBody = piece.body;
					if (hitBody.getType() == BodyType.KinematicBody || hitBody.getType() == BodyType.StaticBody)
						continue;
					MouseJointDef def = new MouseJointDef();
					def.bodyA = groundBody;
					def.bodyB = hitBody;
					def.collideConnected = true;
					def.target.set(testPoint2D);
					def.maxForce = 10.0f * hitBody.getMass();
					mouseJoint = (MouseJoint)world.createJoint(def);
					hitBody.setAwake(true);
					break;
				}
			}
			if (mouseJoint != null)
				return false;
		}
		
		if (!IS_DESKTOP) {
			isRepulsing = true;
			isAttracting = false;
		} else {
			isAttracting = false;
			isRepulsing = false;
		}
	}
	if (button == Buttons.RIGHT) {
		isAttracting = true;
	}
	if (button == Buttons.MIDDLE) {
		isRepulsing = true;
	}
	return false;
}
 
Example #7
Source File: FluidSimulator.java    From fluid-simulator-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
	touching = true;
	camera.unproject(testPoint.set(x, y, 0));
	testPoint2D.x = testPoint.x;
	testPoint2D.y = testPoint.y;
	oldDragPos.set(testPoint2D);
	
	if (button == Buttons.LEFT) {

		// Drag Mode
		if (isDragging) {
			for (Piece piece : pieces.values()) {
				hitBody = null;
				if (piece.body.getFixtureList().get(0).testPoint(testPoint2D)) {
					hitBody = piece.body;
					if (hitBody.getType() == BodyType.KinematicBody || hitBody.getType() == BodyType.StaticBody)
						continue;
					MouseJointDef def = new MouseJointDef();
					def.bodyA = groundBody;
					def.bodyB = hitBody;
					def.collideConnected = true;
					def.target.set(testPoint2D);
					def.maxForce = 10.0f * hitBody.getMass();
					mouseJoint = (MouseJoint)world.createJoint(def);
					hitBody.setAwake(true);
					break;
				}
			}
			if (mouseJoint != null)
				return false;
		}
		
		if (!IS_DESKTOP) {
			isRepulsing = true;
			isAttracting = false;
		} else {
			isAttracting = false;
			isRepulsing = false;
		}
	}
	if (button == Buttons.RIGHT) {
		isAttracting = true;
	}
	if (button == Buttons.MIDDLE) {
		isRepulsing = true;
	}
	return false;
}
 
Example #8
Source File: FluidSimulatorMPM.java    From fluid-simulator-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
	touching = true;
	camera.unproject(testPoint.set(x, y, 0));
	testPoint2D.x = testPoint.x;
	testPoint2D.y = testPoint.y;
	oldDragPos.set(testPoint2D);
	
	if (button == Buttons.LEFT) {
		
		// MPM
		mpmSolver.pressed = true;
		mpmSolver.mx = (int)testPoint2D.x;
		mpmSolver.my = BOX_HEIGHT - (int)testPoint2D.y;

		// Drag Mode
		if (isDragging) {
			for (Piece piece : pieces.values()) {
				hitBody = null;
				if (piece.body.getFixtureList().get(0).testPoint(testPoint2D)) {
					hitBody = piece.body;
					if (hitBody.getType() == BodyType.KinematicBody || hitBody.getType() == BodyType.StaticBody)
						continue;
					MouseJointDef def = new MouseJointDef();
					def.bodyA = groundBody;
					def.bodyB = hitBody;
					def.collideConnected = true;
					def.target.set(testPoint2D);
					def.maxForce = 10.0f * hitBody.getMass();
					mouseJoint = (MouseJoint)world.createJoint(def);
					hitBody.setAwake(true);
					break;
				}
			}
			if (mouseJoint != null)
				return false;
		}
		
		if (!IS_DESKTOP) {
			isRepulsing = true;
			isAttracting = false;
		} else {
			isAttracting = false;
			isRepulsing = false;
		}
	}
	if (button == Buttons.RIGHT) {
		isAttracting = true;
	}
	if (button == Buttons.MIDDLE) {
		isRepulsing = true;
	}
	return false;
}