图片屏幕LibGdx--GearJoint齿轮关节
本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~
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.World; import com.badlogic.gdx.physics.box2d.joints.GearJointDef; import com.badlogic.gdx.physics.box2d.joints.RevoluteJoint; import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef; import com.joye3g.joint.util.Transform; public class GearJointDemo 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, 0f), true); //绘制面地 BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.position.set(-screenWidth / PXTM / 2, -screenHeight / PXTM / 2); Body mGroupBody = 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; mGroupBody.createFixture(fixtureDef); //绘制一个半径为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); Body mCircle1 = mWorld.createBody(mCircleBodyDef1); CircleShape mCircleShape1 = new CircleShape(); mCircleShape1.setRadius(1f); FixtureDef mCircleFix1 = new FixtureDef(); mCircleFix1.shape = mCircleShape1; mCircleFix1.density = 3.0f; mCircle1.createFixture(mCircleFix1); //绘制一个半径为1米的圆形 BodyDef mCircleBodyDef2 = new BodyDef(); mCircleBodyDef2.type = BodyType.DynamicBody; //圆形的位置在屏幕(200,190)的位置 Vector2 vector2 = Transform.ptm(200f, 190f, screenWidth, screenHeight, 0f, 0f, PXTM); mCircleBodyDef2.position.set(vector2); Body mCircle2 = mWorld.createBody(mCircleBodyDef2); CircleShape mCircleShape2 = new CircleShape(); mCircleShape2.setRadius(1f); FixtureDef mCircleFix2 = new FixtureDef(); mCircleFix2.shape = mCircleShape2; mCircleFix2.density = 3.0f; mCircle2.createFixture(mCircleFix2); //建创一个旋转节关 RevoluteJointDef revoluteJointDef = new RevoluteJointDef(); revoluteJointDef.initialize(mGroupBody, mCircle1, mCircle1.getWorldCenter()); revoluteJointDef.maxMotorTorque = 20f;//马达的预期最大扭矩 revoluteJointDef.motorSpeed = 50f;//马达的终究扭矩 revoluteJointDef.enableMotor = true;//动启马达 RevoluteJoint revoluteJoint1 = (RevoluteJoint) mWorld.createJoint(revoluteJointDef); //建创一个旋转节关 RevoluteJointDef revoluteJointDef2 = new RevoluteJointDef(); revoluteJointDef2.initialize(mGroupBody, mCircle2, mCircle2.getWorldCenter()); RevoluteJoint revoluteJoint2 = (RevoluteJoint) mWorld.createJoint(revoluteJointDef2); //建创一个齿轮节关,mCircle1为驱动的Body,mCircle2为被驱动的Body GearJointDef gearJointDef = new GearJointDef(); gearJointDef.bodyA = mCircle1; gearJointDef.bodyB = mCircle2; gearJointDef.joint1 = revoluteJoint1; gearJointDef.joint2 = revoluteJoint2; gearJointDef.ratio = 1f; mWorld.createJoint(gearJointDef); } @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; } }
文章结束给大家分享下程序员的一些笑话语录: 打赌
飞机上,一位工程师和一位程序员坐在一起。程序员问工程师是否乐意和他一起玩一种有趣的游戏。工程师想睡觉,于是他很有礼貌地拒绝了,转身要睡觉。程序员坚持要玩并解释说这是一个非常有趣的游戏:"我问你一个问题,如果你不知道答案,我付你5美元。然后你问我一个问题,如果我答不上来,我付你5美元。"然而,工程师又很有礼貌地拒绝了,又要去睡觉。 程序员这时有些着急了,他说:"好吧,如果你不知道答案,你付5美元;如果我不知道答案,我付50美元。"果然,这的确起了作用,工程师答应了。程序员就问:"从地球到月球有多远?"工程师一句话也没有说,给了程序员5美元。 现在轮到工程师了,他问程序员:"什么上山时有三条腿,下山却有四条腿?"程序员很吃惊地看着工程师,拿出他的便携式电脑,查找里面的资料,过了半个小时,他叫醒工程师并给了工程师50美元。工程师很礼貌地接过钱又要去睡觉。程序员有些恼怒,问:"那么答案是什么呢?"工程师什么也没有说,掏出钱包,拿出5美元给程序员,转身就去睡觉了。