原文链接http://blog.csdn.net/cng1991/article/details/7310665
初看WeldJointDef与RevoluteJointDef并无区别,申明时两个可互相替换,即他们的继承自统一接口。
但看了效果之后知道了WeldJointDef还是有点不一样的,RevoluteJointDef关节使用线类似的东西连接物体的,而WeldJointDef连接物体使用类似一根有弹性的钢条。所以RevoluteJointDef连接的物体由于重力的作用会自动下垂。而WeldJointDef连接的物体会稍微弹起。还是自己看效果吧。。。。
贴下代码:
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.WeldJointDef;
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(-40, 0), new Vector2(40, 0));
BodyDef bd=new BodyDef();
ground=world.createBody(bd);
ground.createFixture(shape, 0);
}
WeldJointDef wjd;
Body preBody=ground;
{
PolygonShape shape=new PolygonShape();
shape.setAsBox(0.5f, 0.125f);
for(int i=0;i<15;i++)
{
BodyDef bd=new BodyDef();
bd.type=BodyType.DynamicBody;
bd.position.set(-14+i, 10);
Body body=world.createBody(bd);
body.createFixture(shape, 2.0f);
wjd=new WeldJointDef();
wjd.initialize(preBody, body, new Vector2(-14.5f+i, 10));
preBody=body;
world.createJoint(wjd);
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initialize(new MyGameListen(), false);
}
}