Original link http://blog.csdn.net/cng1991/article/details/7310510
Similar to the revolutejointdef, The frictionjoint joint connects two objects, but the distance between the objects connected by frictionjoint is variable. Post Code first
package com.cng;import com.badlogic.gdx.ApplicationListener;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.InputProcessor;import com.badlogic.gdx.graphics.GL10;import com.badlogic.gdx.graphics.OrthographicCamera;import com.badlogic.gdx.math.Vector2;import com.badlogic.gdx.math.Vector3;import com.badlogic.gdx.physics.box2d.Body;import com.badlogic.gdx.physics.box2d.BodyDef;import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;import com.badlogic.gdx.physics.box2d.Fixture;import com.badlogic.gdx.physics.box2d.QueryCallback;import com.badlogic.gdx.physics.box2d.World;import com.badlogic.gdx.physics.box2d.joints.MouseJoint;import com.badlogic.gdx.physics.box2d.joints.MouseJointDef;public abstract class BoxTest implements InputProcessor,ApplicationListener{OrthographicCamera camera;Box2DDebugRenderer renderer;MouseJoint mouseJoint;World world;Body grounpBody;Body hitBody;protected abstract void createWorld(World world);Vector2 tmp=new Vector2();@Overridepublic void create(){camera=new OrthographicCamera(48, 32);camera.position.set(0, 15, 0);renderer=new Box2DDebugRenderer();world=new World(new Vector2(0,-10), true);createWorld(world);BodyDef bodyDef=new BodyDef();grounpBody=world.createBody(bodyDef);Gdx.input.setInputProcessor(this);}@Overridepublic void dispose(){world.dispose();renderer.dispose();world=null;renderer=null;hitBody=null;mouseJoint=null;}@Overridepublic void pause(){}@Overridepublic void render(){GL10 gl=Gdx.graphics.getGL10();gl.glClear(GL10.GL_COLOR_BUFFER_BIT);camera.update();camera.apply(gl);world.step(Gdx.graphics.getDeltaTime(), 3, 3);renderer.render(world, camera.combined);}@Overridepublic void resize(int arg0, int arg1){}@Overridepublic void resume(){}@Overridepublic boolean keyDown(int arg0){return false;}@Overridepublic boolean keyTyped(char arg0){return false;}@Overridepublic boolean keyUp(int arg0){return false;}@Overridepublic boolean scrolled(int arg0){return false;}Vector3 testPoint=new Vector3();QueryCallback callback=new QueryCallback(){@Overridepublic boolean reportFixture(Fixture fixture){if(fixture.testPoint(testPoint.x, testPoint.y)){hitBody=fixture.getBody();return false;}else {return true;}}};@Overridepublic boolean touchDown(int x, int y, int arg2, int arg3){hitBody=null;camera.unproject(testPoint.set(x, y, 0));world.QueryAABB(callback, testPoint.x-0.0001f, testPoint.y-0.0001f, testPoint.x+0.0001f, testPoint.y+0.0001f);if(hitBody==grounpBody) hitBody=null;if(hitBody!=null&&hitBody.getType()==BodyType.KinematicBody){return false;}if(hitBody!=null){MouseJointDef def=new MouseJointDef();def.bodyA=grounpBody;def.bodyB=hitBody;def.collideConnected=true;def.target.set(testPoint.x, testPoint.y);def.maxForce=100*hitBody.getMass();mouseJoint=(MouseJoint) world.createJoint(def);hitBody.setAwake(true);}return false;}Vector2 target=new Vector2();@Overridepublic boolean touchDragged(int x, int y, int arg2){if(mouseJoint!=null){camera.unproject(testPoint.set(x, y, 0));mouseJoint.setTarget(target.set(testPoint.x,testPoint.y));}return false;}@Overridepublic boolean touchMoved(int arg0, int arg1){return false;}@Overridepublic boolean touchUp(int arg0, int arg1, int arg2, int arg3){if(mouseJoint!=null){world.destroyJoint(mouseJoint);mouseJoint=null;}return false;}}
Package COM. CNG; import android. OS. bundle; import COM. badlogic. GDX. backends. android. androidapplication; import COM. badlogic. GDX. math. vector2; import COM. badlogic. GDX. physics. box2d. body; import COM. badlogic. GDX. physics. box2d. bodydef; import COM. badlogic. GDX. physics. box2d. edgeshape; import COM. badlogic. GDX. physics. box2d. polygonshape; import COM. badlogic. GDX. physics. box2d. world; import COM. badlogic. GDX. physics. box2d. bodydef. bodytype; import COM. badlogic. GDX. physics. box2d. joints. frictionjointdef; public class mygameactivity extends androidapplication {class mygamelisten extends boxtest {@ overrideprotected void createworld (World) {body ground; {edgeshape shape = new edgeshape (); shape. set (New vector2 (-20, 0), new vector2 (20, 0); bodydef BD = new bodydef (); ground = World. createbody (BD); ground. createfixture (shape, 0) ;}{ polygonshape shape = new polygonshape (); shape. setasbox (2, 0.5f); bodydef BD = new bodydef (); BD. type = bodytype. dynamicbody; BD. position. set (0, 10); Body = World. createbody (BD); body. createfixture (shape, 20366f); frictionjointdef FJD = new frictionjointdef (); FJD. localanchora. set (0, 0); FJD. localanchorb. set (0, 0); FJD. bodya = ground; FJD. bodyb = body; FJD. collideconnected = true; FJD. maxforce = 10; // how powerful can I move an object FJD. maxtorque = 10; World. createjoint (FJD) ;}}@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); initialize (New mygamelisten (), false );}}
Create two objects, ground and body. The ground is a non-understand object, and the body is a movable object.