LibGdx--MouseJoint鼠标关节

package com.joye3g.joint;

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.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.joints.MouseJoint;
import com.badlogic.gdx.physics.box2d.joints.MouseJointDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.joye3g.joint.util.Transform;

public class MouseJointDemo implements ApplicationListener, InputProcessor{
	private Box2DDebugRenderer renderer;
	private OrthographicCamera camera;
	private static final float PXTM = 30;
	private Body mCircle1;
	private World mWorld;
	private MouseJoint mouseJoint;
	private Body mGroupBody;
	private float screenWidth, screenHeight;
	@Override
	public void create() {
		screenWidth = Gdx.graphics.getWidth();
		screenHeight = Gdx.graphics.getHeight();
		renderer = new Box2DDebugRenderer();
		camera = new OrthographicCamera(screenWidth / PXTM, screenHeight / PXTM);
		mWorld = new World(new Vector2(0f, -5f), true);

		//绘制四面墙
		float x = screenWidth / PXTM;
		float y = screenHeight / PXTM;
		BodyDef edgeBodyDef = new BodyDef();
		edgeBodyDef.position.set(-x / 2, -y / 2);
		edgeBodyDef.type = BodyType.StaticBody;
		mGroupBody = mWorld.createBody(edgeBodyDef);		
		EdgeShape edge = new EdgeShape();
		FixtureDef edgeShapeDef = new FixtureDef();
		edgeShapeDef.shape = edge;
		edge.set(new Vector2(0f, 0f), new Vector2(x, 0f));
		mGroupBody.createFixture(edgeShapeDef);
		edge.set(new Vector2(x, 0f), new Vector2(x, y));
		mGroupBody.createFixture(edgeShapeDef);
		edge.set(new Vector2(x, y), new Vector2(0, y));
		mGroupBody.createFixture(edgeShapeDef);
		edge.set(new Vector2(0, y), new Vector2(0, 0));
		mGroupBody.createFixture(edgeShapeDef);

		//绘制一个半径为1米的圆形
		BodyDef mCircleBodyDef1 = new BodyDef();
		mCircleBodyDef1.type = BodyType.DynamicBody;
		//圆形的位置在屏幕(60,190)的位置
		Vector2 vector1 = Transform.ptm(60f, 190f, screenWidth, screenHeight, 0f, 0f, PXTM);
		mCircleBodyDef1.position.set(vector1);
		mCircle1 = mWorld.createBody(mCircleBodyDef1);
		CircleShape mCircleShape1 = new CircleShape();
		mCircleShape1.setRadius(1f);
		FixtureDef mCircleFix1 = new FixtureDef();
		mCircleFix1.shape = mCircleShape1;
		mCircleFix1.density = 1.0f;
		mCircle1.createFixture(mCircleFix1);

		Gdx.input.setInputProcessor(this);
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void render() {
		GL10 gl=Gdx.graphics.getGL10();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		mWorld.step(Gdx.graphics.getDeltaTime(), 8, 6);
		renderer.render(mWorld, camera.combined);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean keyDown(int keycode) {
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		//当按下时,如果鼠标关节为空,则实例化一个鼠标关节
		if(mouseJoint == null){
			MouseJointDef mouseJointDef = new MouseJointDef();
			mouseJointDef.bodyA = mGroupBody;
			mouseJointDef.bodyB = mCircle1;
			mouseJointDef.maxForce = 100f;//拉力
			mouseJointDef.collideConnected = true;
			//设置关节点为圆的圆心
			mouseJointDef.target.x = mCircle1.getPosition().x;
			mouseJointDef.target.y = mCircle1.getPosition().y;
			mouseJoint = (MouseJoint) mWorld.createJoint(mouseJointDef);
		}
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		//当抬起时,如果鼠标关节不为空,则销毁关节
		if(mouseJoint != null){
			mWorld.destroyJoint(mouseJoint);
			mouseJoint = null;
		}
		System.out.println(screenX + ":" + screenY);
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		if(mouseJoint != null){
			//将坐标点进行转换并设置给鼠标关节
			Vector2 vector2 = Transform.ptm(screenX, screenHeight - screenY, screenWidth, screenHeight, 0, 0, PXTM);
			mouseJoint.setTarget(vector2);
		}
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		return false;
	}
}
package com.joye3g.joint.util;

import com.badlogic.gdx.math.Vector2;

public class Transform {
	/**
	 * @param x_px				图片所在x坐标
	 * @param y_px				图片所在y坐标
	 * @param screenWidth		屏幕宽度
	 * @param screenHeight		屏幕高度
	 * @param width_px			图片宽度
	 * @param height_px			图片高度
	 * @param scale				缩放比例
	 * @return					(x,y)直接设置为body的position可使body与图片重合
	 */
	public static Vector2 ptm(float x_px, float y_px, float screenWidth, float screenHeight, float width_px, float height_px, float scale){
		Vector2 vector2 = new Vector2();
		vector2.x = -(screenWidth - x_px * 2 - width_px) / scale / 2;
		vector2.y = -(screenHeight - y_px * 2 - height_px) / scale / 2;
		return vector2;
	}

	/**
	 * @param x_m				body所在x坐标
	 * @param y_m				body所在y坐标
	 * @param screenWidth		屏幕宽度
	 * @param screenHeight		屏幕高度
	 * @param wh				(x,y)body的宽高
	 * @param scale				缩放比例
	 * @return					(x,y)直接设置为图片的position可使图片与body重合
	 */
	public static Vector2 mtp(float x_m, float y_m, float screenWidth, float screenHeight, Vector2 wh, float scale){
		Vector2 vector2 = new Vector2();
		vector2.x = x_m * scale + screenWidth / 2 - wh.x * scale;
		vector2.y = y_m * scale + screenHeight / 2 - wh.y * scale;
		return vector2;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值