原文链接http://blog.csdn.net/cng1991/article/details/7310510
FrictionJoint关节和RevoluteJointDef类似,都是连接两个物体的,但是FrictionJoint连接的物体之间的距离是可变的。先贴代码
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();
@Override
public 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);
}
@Override
public void dispose()
{
world.dispose();
renderer.dispose();
world=null;
renderer=null;
hitBody=null;
mouseJoint=null;
}
@Override
public void pause()
{
}
@Override
public 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);
}
@Override
public void resize(int arg0, int arg1)
{
}
@Override
public void resume()
{
}
@Override
public boolean keyDown(int arg0)
{
return false;
}
@Override
public boolean keyTyped(char arg0)
{
return false;
}
@Override
public boolean keyUp(int arg0)
{
return false;
}
@Override
public boolean scrolled(int arg0)
{
return false;
}
Vector3 testPoint=new Vector3();
QueryCallback callback=new QueryCallback()
{
@Override
public boolean reportFixture(Fixture fixture)
{
if(fixture.testPoint(testPoint.x, testPoint.y))
{
hitBody=fixture.getBody();
return false;
}
else
{
return true;
}
}
};
@Override
public 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();
@Override
public 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;
}
@Override
public boolean touchMoved(int arg0, int arg1)
{
return false;
}
@Override
public 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
{
@Override
protected void createWorld (World 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 body=world.createBody(bd);
body.createFixture(shape, 20.0f);
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;//多大的力才能移动物体
fjd.maxTorque=10;
world.createJoint(fjd);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initialize(new MyGameListen(), false);
}
}
先创建两个物体ground和body。ground为不懂物体,body为可移动物体。