@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 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; }
pjd.enableLimit = true; m_joint = (PrismaticJoint)world.createJoint(pjd);
world.createJoint(jd); prevBody = body;
world.createJoint(jd); prevBody = body; world.createJoint(jd); shape.dispose();
rjd.maxMotorTorque = 50.0f; rjd.enableMotor = true; world.createJoint(rjd); pjd.enableLimit = true; world.createJoint(pjd);
world.createJoint(jd); prevBody = body; world.createJoint(jd); Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); jd.initialize(prevBody, body, anchor); world.createJoint(jd); Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f); jd.initialize(prevBody, body, anchor); world.createJoint(jd);
jd.maxTorque = mass * radius * gravity; world.createJoint(jd);
jd.upperTranslation = 1; world.createJoint(jd);
private void showFrictionJoint() { setTitle("Friction Joint"); Body square = createPolygon(BodyType.DynamicBody, 0, 3.6f, 1f, 0.1f, .4f, .5f, .5f); // Define the friction joint FrictionJointDef frictionJointDef = new FrictionJointDef(); frictionJointDef.initialize(groundBody, square, new Vector2(SCENE_WIDTH*.5f,SCENE_HEIGHT*.5f)); frictionJointDef.collideConnected=true; frictionJointDef.maxForce = 6.0f; frictionJointDef.maxTorque = -.3f; bodies.add(square); joints.add(world.createJoint(frictionJointDef)); }
private void showWeldJoint() { setTitle("Weld Joint"); Body smallBall = createSphere(BodyType.DynamicBody, -1f, 5f, .3333f, .4f, .4f, .25f); Body bigBall = createSphere(BodyType.DynamicBody, 1f, 5f, .3f, .4f, .4f, .3f); // Define the weld joint WeldJointDef weldJointDef = new WeldJointDef(); weldJointDef.bodyA=smallBall; weldJointDef.bodyB=bigBall; weldJointDef.localAnchorA.set(0,0); weldJointDef.localAnchorB.set(.55f,0); bodies.add(bigBall); bodies.add(smallBall); joints.add(world.createJoint(weldJointDef)); }
private void showDistanceJoint() { setTitle("Distance Joint"); Body smallBall = createSphere(BodyType.DynamicBody, 0f, 3.75f, .8f, .8f, .4f, .25f); Body bigBall = createSphere(BodyType.DynamicBody, 3.0f, 4.5f, .8f, 1f, .4f, .5f); // Define the distance joint DistanceJointDef distanceJointDef = new DistanceJointDef(); distanceJointDef.bodyA=smallBall; distanceJointDef.bodyB=bigBall; distanceJointDef.collideConnected=false; distanceJointDef.length = 2.0f; distanceJointDef.localAnchorA.set(0,0); distanceJointDef.localAnchorB.set(0,0); bodies.add(bigBall); bodies.add(smallBall); joints.add(world.createJoint(distanceJointDef)); }
private void showRopeJoint() { setTitle("Rope Joint"); Body smallBall = createSphere(BodyType.DynamicBody, 0f, 3.75f, .8f, .8f, .4f, .25f); Body bigBall = createSphere(BodyType.DynamicBody, 3.0f, 4.5f, .8f, 1f, .4f, .5f); // Define the rope joint RopeJointDef ropeJointDef = new RopeJointDef(); ropeJointDef.bodyA=smallBall; ropeJointDef.bodyB=bigBall; ropeJointDef.collideConnected=true; ropeJointDef.maxLength = 4.0f; ropeJointDef.localAnchorA.set(0,0); ropeJointDef.localAnchorB.set(0.0f,0); bodies.add(bigBall); bodies.add(smallBall); joints.add(world.createJoint(ropeJointDef)); }
private void showRevoluteJoint() { setTitle("Revolute Joint"); Body smallBall = createSphere(BodyType.StaticBody, 0f, 3.75f, 1f, 1f, 0f, .25f); Body bigBall = createSphere(BodyType.DynamicBody, 0f, 3.75f, 1f, 1f, 0f, .5f); // Define the revolute joint RevoluteJointDef revoluteJointDef = new RevoluteJointDef(); revoluteJointDef.bodyA=smallBall; revoluteJointDef.bodyB=bigBall; revoluteJointDef.collideConnected=false; revoluteJointDef.localAnchorA.set(0,0); revoluteJointDef.localAnchorB.set(-2.0f,0); revoluteJointDef.enableMotor=true; revoluteJointDef.maxMotorTorque=360; revoluteJointDef.motorSpeed=100f*MathUtils.degreesToRadians; bodies.add(bigBall); bodies.add(smallBall); joints.add(world.createJoint(revoluteJointDef)); }
private void showWheelJoint() { setTitle("Wheel Joint"); Body wheel = createSphere(BodyType.DynamicBody, 0, 3f, .4f, .5f, .3f, .5f); // Define the wheel joint WheelJointDef wheelJointDef = new WheelJointDef(); wheelJointDef.bodyA=groundBody; wheelJointDef.bodyB=wheel; wheelJointDef.collideConnected=true; wheelJointDef.localAnchorA.set(0,0); wheelJointDef.localAnchorB.set(0,0); wheelJointDef.motorSpeed = 5f; wheelJointDef.enableMotor = true; wheelJointDef.maxMotorTorque = 50f; wheelJointDef.dampingRatio = 0.5f; wheelJointDef.frequencyHz = 0.2f; bodies.add(wheel); joints.add(world.createJoint(wheelJointDef)); }
private void showPrismaticJoint() { setTitle("Prismatic Joint"); Body square = createPolygon(BodyType.DynamicBody, 0, 3.6f, .8f, .8f, .4f, .5f, .5f); // Define the prismatic joint PrismaticJointDef prismaticJointDef = new PrismaticJointDef(); prismaticJointDef.initialize(groundBody, square, new Vector2(SCENE_WIDTH*.5f,SCENE_HEIGHT*.5f), new Vector2(SCENE_WIDTH*.5f+1f,0)); prismaticJointDef.lowerTranslation =-2; prismaticJointDef.upperTranslation = 2; prismaticJointDef.enableLimit = true; prismaticJointDef.enableMotor = true; prismaticJointDef.maxMotorForce = 100; prismaticJointDef.motorSpeed = 20f * MathUtils.degreesToRadians; bodies.add(square); joints.add(world.createJoint(prismaticJointDef)); }
private void showPulleyJoint() { setTitle("Pulley Joint"); Body smallBall = createSphere(BodyType.DynamicBody, -1f, 5f, .3333f, .4f, .4f, .25f); Body bigBall = createSphere(BodyType.DynamicBody, 1f, 5f, .3f, .4f, .4f, .3f); // Define the pulley joint PulleyJointDef pulleyJointDef = new PulleyJointDef(); pulleyJointDef.bodyA=smallBall; pulleyJointDef.bodyB=bigBall; pulleyJointDef.collideConnected=true; pulleyJointDef.groundAnchorA.set(SCENE_WIDTH*.5f-1, SCENE_HEIGHT*.5f); pulleyJointDef.groundAnchorB.set(SCENE_WIDTH*.5f+1f, SCENE_HEIGHT*.5f); pulleyJointDef.localAnchorA.set(0,0); pulleyJointDef.localAnchorB.set(0,0); pulleyJointDef.lengthA = 0.7f; pulleyJointDef.lengthB = 0.7f; pulleyJointDef.ratio=1f; bodies.add(bigBall); bodies.add(smallBall); joints.add(world.createJoint(pulleyJointDef)); }
/** * @return finished attached joint using GDXJoint data */ protected Joint attach(World world, JointDef def){ Body[] bodies = getBodyAB(world); if(bodies[0] == null || bodies[1] == null){ Log.error("GDXJoint.attach", "Body null! bodyA:" + this.bodyA + " bodyB:" + this.bodyB); return null; } def.bodyA = bodies[0]; def.bodyB = bodies[1]; def.type = jointType; def.collideConnected = collideConnected; Joint joint = world.createJoint(def); Log.log("GDXJoint.attach", "Successfully created joint " + toString()); joint.setUserData(name); return joint; }
@Override public void createBodies(World world) { this.anchorBody = Box2DFactory.createCircle(world, this.cx, this.cy, 0.05f, true); // Joint angle is 0 when flipper is horizontal. // The flipper needs to be slightly extended past anchorBody to rotate correctly. float ext = (this.flipperLength > 0) ? -0.05f : +0.05f; // Width larger than 0.12 slows rotation? this.flipperBody = Box2DFactory.createWall(world, cx+ext, cy-0.12f, cx+flipperLength, cy+0.12f, 0f); flipperBody.setType(BodyDef.BodyType.DynamicBody); flipperBody.setBullet(true); flipperBody.getFixtureList().get(0).setDensity(5.0f); jointDef = new RevoluteJointDef(); jointDef.initialize(anchorBody, flipperBody, new Vector2(this.cx, this.cy)); jointDef.enableLimit = true; jointDef.enableMotor = true; // counterclockwise rotations are positive, so flip angles for flippers extending left jointDef.lowerAngle = (this.flipperLength>0) ? this.minangle : -this.maxangle; jointDef.upperAngle = (this.flipperLength>0) ? this.maxangle : -this.minangle; jointDef.maxMotorTorque = 1000f; this.joint = (RevoluteJoint)world.createJoint(jointDef); flipperBodySet = Collections.singletonList(flipperBody); this.setEffectiveMotorSpeed(-this.downspeed); // Force flipper to bottom when field is first created. }
@Override public boolean touchDown (int x, int y, int pointer, int button) { // Mouse coordinates to world coordinates viewport.getCamera().unproject(point.set(x, y, 0)); if(button == Input.Buttons.RIGHT) { hitBody = null; // Check if a interactive body has been right-clicked world.QueryAABB(callback, point.x - 0.0001f, point.y - 0.0001f, point.x + 0.0001f, point.y + 0.0001f); if (hitBody == groundBody) hitBody = null; // if we hit an interactive body 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(point.x, point.y); def.maxForce = 1000.0f * hitBody.getMass(); mouseJoint = (MouseJoint)world.createJoint(def); hitBody.setAwake(true); } } return false; }