/** * Create mouse joint with ground body, target body and location on target body * @param groundBody * @param targetBody * @param locationWorld * @return */ public MouseJoint createMouseJoint(Body groundBody, Body targetBody, Vector2 locationWorld){ MouseJointDef md = new MouseJointDef(); md.bodyA = groundBody; md.bodyB = targetBody; md.target.set(locationWorld.x, locationWorld.y); md.collideConnected = true; md.maxForce = 10000.0f * targetBody.getMass(); MouseJoint _mouseJoint = (MouseJoint)world.createJoint(md); targetBody.setAwake(true); return _mouseJoint; }
/** * Create mouse joint with default ground body, target body and location on target body * @param targetBody * @param locationWorld * @return */ public MouseJoint createMouseJoint(Body targetBody, Vector2 locationWorld){ BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(new Vector2(locationWorld.x * WORLD_TO_BOX, locationWorld.y * WORLD_TO_BOX)); bodyDef.linearDamping = 0.3f; Body body = world.createBody(bodyDef); return createMouseJoint(body, targetBody, locationWorld); }
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); } }
@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(); }
@Override public boolean touchDown (int x, int y, int pointer, int button) { // translate the mouse coordinates to world coordinates camera.unproject(testPoint.set(x, y, 0)); // ask the world which bodies are within the given // bounding box around the mouse pointer hitBody = null; world.QueryAABB(callback, testPoint.x - 0.0001f, testPoint.y - 0.0001f, testPoint.x + 0.0001f, testPoint.y + 0.0001f); if (hitBody == groundBody) hitBody = null; // ignore kinematic bodies, they don't work with the mouse joint if (hitBody != null && hitBody.getType() == BodyType.KinematicBody) return false; // 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; }
@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); } else { for (Body box : boxes) world.destroyBody(box); boxes.clear(); createBoxes(); } return false; }
@Override public boolean reportFixture(Fixture fixture) { if (!fixture.testPoint(aux.x, aux.y)) return true; mouseJointDef.bodyB = fixture.getBody(); mouseJointDef.target.set(aux.x, aux.y); mouseJoint = (MouseJoint) world.createJoint(mouseJointDef); return false; }
@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; }
@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; }
@Override public boolean reportFixture(Fixture fixture) { if (!fixture.testPoint(tmp.x, tmp.y)) return true; jointDef.bodyB = fixture.getBody(); jointDef.target.set(tmp.x, tmp.y); joint = (MouseJoint) context.get(World.class).createJoint(jointDef); return false; }
@Override public boolean touchDown (int x, int y, int pointer, int newParam) { if(world == null)return false; // translate the mouse coordinates to world coordinates testPoint.set(Engine.screenToWorld(x, y).scl(1/Box2dObject.RADIO)); // 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 = fixBody; 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 true; } return false; }
/** * Sacado del ejemplo de AE demostrando MouseJoint. * * Esta función crea un {@link MouseJoint}. * * @param entidad * the entidad * @param pTouchAreaLocalX * the touch area local x * @param pTouchAreaLocalY * the touch area local y * @return the mouse joint */ private MouseJoint createMouseJoint(final IEntity entidad, final float pTouchAreaLocalX, final float pTouchAreaLocalY) { final Body body = ((ObjetoFisico<?>) entidad.getUserData()).getCuerpo(); final MouseJointDef mouseJointDef = new MouseJointDef(); final float[] coordsEscena = entidad .convertLocalCoordinatesToSceneCoordinates(pTouchAreaLocalX, pTouchAreaLocalY); final Vector2 localPoint = Vector2Pool.obtain( (coordsEscena[0] - (entidad.getWidth() * entidad .getOffsetCenterX())) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, (coordsEscena[1] - (entidad.getHeight() * entidad .getOffsetCenterY())) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT); /* * realmente el MouseJoint solo usa un cuerpo(el bodyB) pero es * obligatorio suministrarle otro por lo que tradicionalmente se usa el * del suelo ( que suele estar siempre presente) */ mouseJointDef.bodyA = suelo; mouseJointDef.bodyB = body; mouseJointDef.dampingRatio = 0.00f; mouseJointDef.frequencyHz = 10f; mouseJointDef.maxForce = (100 * body.getMass() * 4); mouseJointDef.collideConnected = true; //si desactivamos la colision las piezas ignorarán el suelo :-) mouseJointDef.target.set(localPoint); Vector2Pool.recycle(localPoint); return (MouseJoint) mundo.createJoint(mouseJointDef); }
public void setupMouseJoint(World world, BodyDef bd) { BodyDef groundBodyDef = new BodyDef(); groundBodyDef.position.set(0, 0); mMouseJointBody = world.createBody(groundBodyDef); // create a mousejoint MouseJointDef mouseJointDef = new MouseJointDef(); mouseJointDef.bodyA = mMouseJointBody; mouseJointDef.bodyB = mBody; mouseJointDef.dampingRatio = 0.2f; mouseJointDef.frequencyHz = 30; mouseJointDef.maxForce = 20000.0f; mouseJointDef.collideConnected = true; mouseJointDef.target.set(bd.position); mMouseJoint = (MouseJoint) world.createJoint(mouseJointDef); }
@Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { if (slingshotEnabled == false) return false; /* Translate the mouse coordinates to world coordinates */ stage.getCamera().unproject(testPoint.set(screenX, screenY, 0)); testPoint.x *= G.WORLD_TO_BOX; testPoint.y *= G.WORLD_TO_BOX; hitBody = null; body.getWorld().QueryAABB(callback, testPoint.x - 0.0001f, testPoint.y - 0.0001f, testPoint.x + 0.0001f, testPoint.y + 0.0001f); if (hitBody == groundBody) hitBody = null; if (hitBody == null) return false; /* Ignore kinematic bodies, they don't work with the mouse joint */ if (hitBody.getType() == BodyType.KinematicBody) return false; if (hitBody.equals(this.body)) { MouseJointDef def = new MouseJointDef(); def.bodyA = groundBody; def.bodyB = hitBody; def.collideConnected = true; def.target.set(testPoint.x, testPoint.y); def.maxForce = a_max * hitBody.getMass(); startPoint.set(testPoint.x, testPoint.y); mouseJoint = (MouseJoint)((body.getWorld()).createJoint(def)); hitBody.setAwake(true); } return false; }
@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; }
@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; }
private long createProperJoint(JointDef paramJointDef) { if (paramJointDef.type == JointDef.JointType.DistanceJoint) { DistanceJointDef localDistanceJointDef = (DistanceJointDef)paramJointDef; return jniCreateDistanceJoint(this.addr, localDistanceJointDef.bodyA.addr, localDistanceJointDef.bodyB.addr, localDistanceJointDef.collideConnected, localDistanceJointDef.localAnchorA.x, localDistanceJointDef.localAnchorA.y, localDistanceJointDef.localAnchorB.x, localDistanceJointDef.localAnchorB.y, localDistanceJointDef.length, localDistanceJointDef.frequencyHz, localDistanceJointDef.dampingRatio); } if (paramJointDef.type == JointDef.JointType.FrictionJoint) { FrictionJointDef localFrictionJointDef = (FrictionJointDef)paramJointDef; return jniCreateFrictionJoint(this.addr, localFrictionJointDef.bodyA.addr, localFrictionJointDef.bodyB.addr, localFrictionJointDef.collideConnected, localFrictionJointDef.localAnchorA.x, localFrictionJointDef.localAnchorA.y, localFrictionJointDef.localAnchorB.x, localFrictionJointDef.localAnchorB.y, localFrictionJointDef.maxForce, localFrictionJointDef.maxTorque); } if (paramJointDef.type == JointDef.JointType.GearJoint) { GearJointDef localGearJointDef = (GearJointDef)paramJointDef; return jniCreateGearJoint(this.addr, localGearJointDef.bodyA.addr, localGearJointDef.bodyB.addr, localGearJointDef.collideConnected, localGearJointDef.joint1.addr, localGearJointDef.joint2.addr, localGearJointDef.ratio); } if (paramJointDef.type == JointDef.JointType.MouseJoint) { MouseJointDef localMouseJointDef = (MouseJointDef)paramJointDef; return jniCreateMouseJoint(this.addr, localMouseJointDef.bodyA.addr, localMouseJointDef.bodyB.addr, localMouseJointDef.collideConnected, localMouseJointDef.target.x, localMouseJointDef.target.y, localMouseJointDef.maxForce, localMouseJointDef.frequencyHz, localMouseJointDef.dampingRatio); } if (paramJointDef.type == JointDef.JointType.PrismaticJoint) { PrismaticJointDef localPrismaticJointDef = (PrismaticJointDef)paramJointDef; return jniCreatePrismaticJoint(this.addr, localPrismaticJointDef.bodyA.addr, localPrismaticJointDef.bodyB.addr, localPrismaticJointDef.collideConnected, localPrismaticJointDef.localAnchorA.x, localPrismaticJointDef.localAnchorA.y, localPrismaticJointDef.localAnchorB.x, localPrismaticJointDef.localAnchorB.y, localPrismaticJointDef.localAxisA.x, localPrismaticJointDef.localAxisA.y, localPrismaticJointDef.referenceAngle, localPrismaticJointDef.enableLimit, localPrismaticJointDef.lowerTranslation, localPrismaticJointDef.upperTranslation, localPrismaticJointDef.enableMotor, localPrismaticJointDef.maxMotorForce, localPrismaticJointDef.motorSpeed); } if (paramJointDef.type == JointDef.JointType.PulleyJoint) { PulleyJointDef localPulleyJointDef = (PulleyJointDef)paramJointDef; return jniCreatePulleyJoint(this.addr, localPulleyJointDef.bodyA.addr, localPulleyJointDef.bodyB.addr, localPulleyJointDef.collideConnected, localPulleyJointDef.groundAnchorA.x, localPulleyJointDef.groundAnchorA.y, localPulleyJointDef.groundAnchorB.x, localPulleyJointDef.groundAnchorB.y, localPulleyJointDef.localAnchorA.x, localPulleyJointDef.localAnchorA.y, localPulleyJointDef.localAnchorB.x, localPulleyJointDef.localAnchorB.y, localPulleyJointDef.lengthA, localPulleyJointDef.lengthB, localPulleyJointDef.ratio); } if (paramJointDef.type == JointDef.JointType.RevoluteJoint) { RevoluteJointDef localRevoluteJointDef = (RevoluteJointDef)paramJointDef; return jniCreateRevoluteJoint(this.addr, localRevoluteJointDef.bodyA.addr, localRevoluteJointDef.bodyB.addr, localRevoluteJointDef.collideConnected, localRevoluteJointDef.localAnchorA.x, localRevoluteJointDef.localAnchorA.y, localRevoluteJointDef.localAnchorB.x, localRevoluteJointDef.localAnchorB.y, localRevoluteJointDef.referenceAngle, localRevoluteJointDef.enableLimit, localRevoluteJointDef.lowerAngle, localRevoluteJointDef.upperAngle, localRevoluteJointDef.enableMotor, localRevoluteJointDef.motorSpeed, localRevoluteJointDef.maxMotorTorque); } if (paramJointDef.type == JointDef.JointType.WeldJoint) { WeldJointDef localWeldJointDef = (WeldJointDef)paramJointDef; return jniCreateWeldJoint(this.addr, localWeldJointDef.bodyA.addr, localWeldJointDef.bodyB.addr, localWeldJointDef.collideConnected, localWeldJointDef.localAnchorA.x, localWeldJointDef.localAnchorA.y, localWeldJointDef.localAnchorB.x, localWeldJointDef.localAnchorB.y, localWeldJointDef.referenceAngle); } if (paramJointDef.type == JointDef.JointType.RopeJoint) { RopeJointDef localRopeJointDef = (RopeJointDef)paramJointDef; return jniCreateRopeJoint(this.addr, localRopeJointDef.bodyA.addr, localRopeJointDef.bodyB.addr, localRopeJointDef.collideConnected, localRopeJointDef.localAnchorA.x, localRopeJointDef.localAnchorA.y, localRopeJointDef.localAnchorB.x, localRopeJointDef.localAnchorB.y, localRopeJointDef.maxLength); } if (paramJointDef.type == JointDef.JointType.WheelJoint) { WheelJointDef localWheelJointDef = (WheelJointDef)paramJointDef; return jniCreateWheelJoint(this.addr, localWheelJointDef.bodyA.addr, localWheelJointDef.bodyB.addr, localWheelJointDef.collideConnected, localWheelJointDef.localAnchorA.x, localWheelJointDef.localAnchorA.y, localWheelJointDef.localAnchorB.x, localWheelJointDef.localAnchorB.y, localWheelJointDef.localAxisA.x, localWheelJointDef.localAxisA.y, localWheelJointDef.enableMotor, localWheelJointDef.maxMotorTorque, localWheelJointDef.motorSpeed, localWheelJointDef.frequencyHz, localWheelJointDef.dampingRatio); } return 0L; }