@Override public void addedToEngine(Engine engine) { super.addedToEngine(engine); this.camera = context.get(Camera.class); this.physics = context.get(World.class); if (Settings.DRAGGABLE_BOX2D_BODIES) { jointDef = new MouseJointDef(); jointDef.bodyA = context.get(LBBuilders.class).getBodyBuilder().build(); jointDef.collideConnected = true; jointDef.maxForce = Settings.DRAGGABLE_BOX2D_MAX_FORCE; } else { Log.error(tag, "A reference not set up a draggableRefBody"); } }
public JointSerializer(RubeScene scene,Json _json) { this.scene = scene; _json.setSerializer(RevoluteJointDef.class, new RevoluteJointDefSerializer()); _json.setSerializer(PrismaticJointDef.class, new PrismaticJointDefSerializer()); _json.setSerializer(PulleyJointDef.class, new PulleyJointDefSerializer()); _json.setSerializer(WeldJointDef.class, new WeldJointDefSerializer()); _json.setSerializer(FrictionJointDef.class, new FrictionJointDefSerializer()); _json.setSerializer(WheelJointDef.class, new WheelJointDefSerializer()); _json.setSerializer(RopeJointDef.class, new RopeJointDefSerializer()); _json.setSerializer(DistanceJointDef.class, new DistanceJointDefSerializer()); _json.setSerializer(GearJointDef.class, new GearJointDefSerializer()); mouseJointDefSerializer = new MouseJointDefSerializer(); _json.setSerializer(MouseJointDef.class, mouseJointDefSerializer); }
@SuppressWarnings("rawtypes") @Override public MouseJointDef read(Json json, JsonValue jsonData, Class type) { MouseJointDef defaults = RubeDefaults.Joint.mouseDef; MouseJointDef def = new MouseJointDef(); // Don't forget to set the target to the joint once it's created target = json.readValue("target", Vector2.class, defaults.target, jsonData); Vector2 anchorB = json.readValue("anchorB", Vector2.class, defaults.target, jsonData); if(target != null && anchorB != null) { def.target.set(anchorB); def.maxForce = json.readValue("maxForce", float.class, defaults.maxForce, jsonData); def.frequencyHz = json.readValue("frequency", float.class, defaults.frequencyHz, jsonData); def.dampingRatio = json.readValue("dampingRatio", float.class, defaults.dampingRatio, jsonData); } return def; }
/** * 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; }
@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; }
/** * Creates the MouseJoint definition. * * @param body * First body of the joint (i.e. ground, walls, etc.) */ private void createMouseJointDefinition(Body body) { mouseJointDef = new MouseJointDef(); mouseJointDef.bodyA = body; mouseJointDef.collideConnected = true; mouseJointDef.maxForce = 500; }
@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 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); }
/** * Constructor */ public B2FlxMouseJoint() { _mouseJointDef = new MouseJointDef(); _groundBody = B2FlxB.world.createBody(new BodyDef()); _testPoint = new Vector2(); _mouseTarget = new Vector2(); _removeBySchedule = false; }
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 void show() { // Stage stage = new ExtendedStage(this, new WisperChooseMenu()); initInputListener(); Gdx.input.setInputProcessor(stage); Gdx.input.setCatchBackKey(true); // Box2d world = new World(new Vector2(0, -9.81f), true); // newton -9.81f debugRenderer = new Box2DDebugRenderer(); initContactListener(); // Wisper & Batch timer = new Timer(); batch = new SpriteBatch(); wisper = new WisperBox2d(chosenWisper, world); // Temps new WisperBox2d(Wisper.BLACK_WISPER, world).setPosition( Config.APP_WIDTH / 2 - 10, Config.APP_HEIGHT / 2 - 10); new WisperBox2d(Wisper.BLUE_WISPER, world).setPosition( Config.APP_WIDTH / 2 + 10, Config.APP_HEIGHT / 2 + 10); // Tiled map TiledMap map = new TmxMapLoader().load("tiledmap/tiledmap.tmx"); mapRenderer = new OrthogonalTiledMapRenderer(map, Config.GAME_RATIO); box2dParser = new Box2DMapObjectParser(Config.GAME_RATIO); box2dParser.load(world, map); // Joint jointDef = new MouseJointDef(); jointDef.bodyA = box2dParser.getBodies().values().next(); jointDef.collideConnected = true; jointDef.bodyB = wisper.getBody(); }
@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 void create() { // C�mara del juego camera = new OrthographicCamera(); // Zona visible de la c�mara camera.viewportWidth = 480; camera.viewportHeight = 320; // Colca la c�mara en el centro de la pantalla camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0f); camera.update(); spriteBatch = new SpriteBatch(); font = new BitmapFont(); world = new World(new Vector2(0, -10f), true); renderer = new Box2DDebugRenderer(); // Define un cuerpo (la tierra) BodyDef groundDef = new BodyDef(); groundDef.type = BodyType.StaticBody; groundDef.position.set(new Vector2(0, 10)); Body groundBody= world.createBody(groundDef); PolygonShape groundShape = new PolygonShape(); groundShape.setAsBox(camera.viewportWidth * 2, 10.0f); groundBody.createFixture(groundShape, 0.0f); // A�ade un coche al mundo con una velocidad inicial car = WorldGenerator.createCar(world, 280, 60); car.setLinearVelocity(new Vector2(10f, 0)); Body circle = WorldGenerator.createCircleBody(world, 200, 200); // A�ade una rampa Body floor = WorldGenerator.createPolygonBody(world, 250, 70, 100, -50); mouseJointDef = new MouseJointDef(); mouseJointDef.bodyA = groundBody; mouseJointDef.collideConnected = true; mouseJointDef.maxForce = 500000; Gdx.input.setInputProcessor(this); }
@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; }