libgdx之PrismaticJoint关节


原文链接http://blog.csdn.net/cng1991/article/details/7309778

PrismaticJoint关节其实就是限定物体移动路径的关节。先贴代码:

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.PrismaticJoint;
import com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef;




public class MyGameActivity extends AndroidApplication {
	
	PrismaticJoint m_joint;
	class MyGameListen  extends BoxTest
	{
		Body ground;
		@Override
		protected void createWorld (World world) {
			{
				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);
			}
			{
				PolygonShape shape=new PolygonShape();
				shape.setAsBox(2, 5);
				
				BodyDef bd=new BodyDef();
				bd.allowSleep=false;
				bd.position.set(-10,10);
				bd.type=BodyType.DynamicBody;
				
				Body body=world.createBody(bd);
				body.createFixture(shape, 0.5f);
				
				PrismaticJointDef def=new PrismaticJointDef();
				
				Vector2 axis=new Vector2(1,0);
				axis.nor();
				def.initialize(ground, body, new Vector2(0,0), axis);
				def.motorSpeed=10.0f;//设置运动的速度极其方向
				def.maxMotorForce=10000;
				def.enableMotor=true;//是否自动运动
				def.lowerTranslation=0;//运动范围最小值
				def.upperTranslation=20;//运动范围最大值
				def.enableLimit=true;
				m_joint=(PrismaticJoint) world.createJoint(def);
			}
		}

	}	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		initialize(new MyGameListen(), false);
	}
	
	
}


通过def.initialize(ground, body, new Vector2(0,0), axis);来申明被限定运动路径的物体body。new Vector2(0,0)代表在ground上的锚点,即连接点。axis设置body物体的运动方向。def可设置是否自动运动,运动路径是否有限等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值