LibGdx--WeldJoint焊接关节

package com.joye3g.joint;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
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.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.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.WeldJointDef;
import com.joye3g.joint.util.Transform;

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

		//绘制地面
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.StaticBody;
		bodyDef.position.set(-screenWidth / PXTM / 2, -screenHeight / PXTM / 2);
		Body mLine = mWorld.createBody(bodyDef);
		EdgeShape edgeShape = new EdgeShape();
		edgeShape.set(new Vector2(0f, 1f), new Vector2(screenWidth / PXTM, 1f));
		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = edgeShape;
		mLine.createFixture(fixtureDef);

		//绘制一个长方形
		BodyDef mRecBodyDef1 = new BodyDef();
		mRecBodyDef1.type = BodyType.DynamicBody;
		//长方形的位置在屏幕(60,190)的位置
		Vector2 vector1 = Transform.ptm(60f, 190f, screenWidth, screenHeight, 0f, 0f, PXTM);
		mRecBodyDef1.position.set(vector1);
		Body mRecBody1 = mWorld.createBody(mRecBodyDef1);
		PolygonShape mRecShape1 = new PolygonShape();
		mRecShape1.setAsBox(1f, 1f);
		FixtureDef mCircleFix1 = new FixtureDef();
		mCircleFix1.shape = mRecShape1;
		mCircleFix1.density = 3.0f;//两个Body都必须设置此项
		mRecBody1.createFixture(mCircleFix1);

		//绘制一个长方形
		BodyDef mRecBodyDef2 = new BodyDef();
		mRecBodyDef2.type = BodyType.DynamicBody;
		//长方形的位置在屏幕(200,190)的位置
		Vector2 vector2 = Transform.ptm(200f, 190f, screenWidth, screenHeight, 0f, 0f, PXTM);
		mRecBodyDef2.position.set(vector2);
		Body mRecBody2 = mWorld.createBody(mRecBodyDef2);
		PolygonShape mRecShape2 = new PolygonShape();
		mRecShape2.setAsBox(3.6f, 1f);
		FixtureDef mCircleFix2 = new FixtureDef();
		mCircleFix2.shape = mRecShape2;
		mCircleFix2.density = 3.0f;//两个Body都必须设置此项
		mRecBody2.createFixture(mCircleFix2);

		//绘制一个半径为1米的圆形(静态)
		BodyDef mCircleBodyDef3 = new BodyDef();
		mCircleBodyDef3.type = BodyType.StaticBody;
		//圆形的位置在屏幕(60,100)的位置
		Vector2 vector3 = Transform.ptm(60f, 100f, screenWidth, screenHeight, 0f, 0f, PXTM);
		mCircleBodyDef3.position.set(vector3);
		Body mCircle3 = mWorld.createBody(mCircleBodyDef3);
		CircleShape mCircleShape3 = new CircleShape();
		mCircleShape3.setRadius(1f);
		FixtureDef mCircleFix3 = new FixtureDef();
		mCircleFix3.shape = mCircleShape3;
		mCircle3.createFixture(mCircleFix3);
		
		//创建一个焊接关节
		WeldJointDef weldJointDef = new WeldJointDef();
		weldJointDef.initialize(mRecBody1, mRecBody2, mRecBody1.getWorldCenter());
		mWorld.createJoint(weldJointDef);
	}

	@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() {
	}
}
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;
	}
}


你可能感兴趣的:(libgdx,box2D,关节,焊接关节,WeldJoint)