LibGdx--PulleyJoint滑轮关节

  1. package com.joye3g.joint;  
  2.   
  3. import com.badlogic.gdx.ApplicationListener;  
  4. import com.badlogic.gdx.Gdx;  
  5. import com.badlogic.gdx.graphics.GL10;  
  6. import com.badlogic.gdx.graphics.OrthographicCamera;  
  7. import com.badlogic.gdx.math.Vector2;  
  8. import com.badlogic.gdx.physics.box2d.Body;  
  9. import com.badlogic.gdx.physics.box2d.BodyDef;  
  10. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;  
  11. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;  
  12. import com.badlogic.gdx.physics.box2d.CircleShape;  
  13. import com.badlogic.gdx.physics.box2d.EdgeShape;  
  14. import com.badlogic.gdx.physics.box2d.FixtureDef;  
  15. import com.badlogic.gdx.physics.box2d.World;  
  16. import com.badlogic.gdx.physics.box2d.joints.PulleyJointDef;  
  17. import com.joye3g.joint.util.Transform;  
  18.   
  19. public class PulleyJointDemo implements ApplicationListener{  
  20.     private Box2DDebugRenderer renderer;  
  21.     private OrthographicCamera camera;  
  22.     private static final float PXTM = 30;  
  23.     private World mWorld;  
  24.     @Override  
  25.     public void create() {  
  26.         float screenWidth = Gdx.graphics.getWidth();  
  27.         float screenHeight = Gdx.graphics.getHeight();  
  28.         renderer = new Box2DDebugRenderer();  
  29.         camera = new OrthographicCamera(screenWidth / PXTM, screenHeight / PXTM);  
  30.         mWorld = new World(new Vector2(0f, -10f), true);  
  31.   
  32.         //绘制地面  
  33.         BodyDef bodyDef = new BodyDef();  
  34.         bodyDef.type = BodyType.StaticBody;  
  35.         bodyDef.position.set(-screenWidth / PXTM / 2, -screenHeight / PXTM / 2);  
  36.         Body mLine = mWorld.createBody(bodyDef);  
  37.         EdgeShape edgeShape = new EdgeShape();  
  38.         edgeShape.set(new Vector2(0f, 1f), new Vector2(screenWidth / PXTM, 1f));  
  39.         FixtureDef fixtureDef = new FixtureDef();  
  40.         fixtureDef.shape = edgeShape;  
  41.         mLine.createFixture(fixtureDef);  
  42.   
  43.         //绘制一个半径为1米的圆形  
  44.         BodyDef mCircleBodyDef1 = new BodyDef();  
  45.         mCircleBodyDef1.type = BodyType.DynamicBody;  
  46.         //圆形的位置在屏幕(60,190)的位置  
  47.         Vector2 vector1 = Transform.ptm(60f, 190f, screenWidth, screenHeight, 0f, 0f, PXTM);  
  48.         mCircleBodyDef1.position.set(vector1);  
  49.         Body mCircle1 = mWorld.createBody(mCircleBodyDef1);  
  50.         CircleShape mCircleShape1 = new CircleShape();  
  51.         mCircleShape1.setRadius(1f);  
  52.         FixtureDef mCircleFix1 = new FixtureDef();  
  53.         mCircleFix1.shape = mCircleShape1;  
  54.         mCircle1.createFixture(mCircleFix1);  
  55.           
  56.         //绘制一个半径为1米的圆形  
  57.         BodyDef mCircleBodyDef2 = new BodyDef();  
  58.         mCircleBodyDef2.type = BodyType.DynamicBody;  
  59.         //圆形的位置在屏幕(200,190)的位置  
  60.         Vector2 vector2 = Transform.ptm(200f, 190f, screenWidth, screenHeight, 0f, 0f, PXTM);  
  61.         mCircleBodyDef2.position.set(vector2);  
  62.         Body mCircle2 = mWorld.createBody(mCircleBodyDef2);  
  63.         CircleShape mCircleShape2 = new CircleShape();  
  64.         mCircleShape2.setRadius(1f);  
  65.         FixtureDef mCircleFix2 = new FixtureDef();  
  66.         mCircleFix2.shape = mCircleShape2;  
  67.         mCircle2.createFixture(mCircleFix2);  
  68.           
  69.         //创建一个滑轮关节  
  70.         PulleyJointDef pulleyJointDef = new PulleyJointDef();  
  71.         Vector2 groundAnchorA = new Vector2(-3f, 5f);//第一个滑轮锚点  
  72.         Vector2 groundAnchorB = new Vector2(3f, 5f);//第二个滑轮锚点  
  73.         Vector2 anchorA = mCircle1.getWorldCenter();//第一个Body实例的锚点  
  74.         Vector2 anchorB = mCircle2.getWorldCenter();//第二个Body实例的锚点  
  75.         float ratio = 1f;//设置滑轮关节的拉伸比例  
  76.         pulleyJointDef.initialize(mCircle1, mCircle2, groundAnchorA, groundAnchorB, anchorA, anchorB, ratio);  
  77.         pulleyJointDef.lengthA = 4f;//设置滑轮关节的第一条距离拉伸的最大长度  
  78.         pulleyJointDef.lengthB = 8f;//设置滑轮关节的第二条距离拉伸的最大长度  
  79.         mWorld.createJoint(pulleyJointDef);  
  80.     }  
  81.   
  82.     @Override  
  83.     public void resize(int width, int height) {  
  84.     }  
  85.   
  86.     @Override  
  87.     public void render() {  
  88.         GL10 gl=Gdx.graphics.getGL10();  
  89.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
  90.         mWorld.step(Gdx.graphics.getDeltaTime(), 8, 6);  
  91.         renderer.render(mWorld, camera.combined);  
  92.     }  
  93.   
  94.     @Override  
  95.     public void pause() {  
  96.     }  
  97.   
  98.     @Override  
  99.     public void resume() {  
  100.     }  
  101.   
  102.     @Override  
  103.     public void dispose() {  
  104.     }  
  105. }  
[html]  view plain copy
  1. package com.joye3g.joint.util;  
  2.   
  3. import com.badlogic.gdx.math.Vector2;  
  4.   
  5. public class Transform {  
  6.     /**  
  7.      * @param x_px              图片所在x坐标  
  8.      * @param y_px              图片所在y坐标  
  9.      * @param screenWidth       屏幕宽度  
  10.      * @param screenHeight      屏幕高度  
  11.      * @param width_px          图片宽度  
  12.      * @param height_px         图片高度  
  13.      * @param scale             缩放比例  
  14.      * @return                  (x,y)直接设置为body的position可使body与图片重合  
  15.      */  
  16.     public static Vector2 ptm(float x_px, float y_px, float screenWidth, float screenHeight, float width_px, float height_px, float scale){  
  17.         Vector2 vector2 = new Vector2();  
  18.         vector2.x = -(screenWidth - x_px * 2 - width_px) / scale / 2;  
  19.         vector2.y = -(screenHeight - y_px * 2 - height_px) / scale / 2;  
  20.         return vector2;  
  21.     }  
  22.   
  23.     /**  
  24.      * @param x_m               body所在x坐标  
  25.      * @param y_m               body所在y坐标  
  26.      * @param screenWidth       屏幕宽度  
  27.      * @param screenHeight      屏幕高度  
  28.      * @param wh                (x,y)body的宽高  
  29.      * @param scale             缩放比例  
  30.      * @return                  (x,y)直接设置为图片的position可使图片与body重合  
  31.      */  
  32.     public static Vector2 mtp(float x_m, float y_m, float screenWidth, float screenHeight, Vector2 wh, float scale){  
  33.         Vector2 vector2 = new Vector2();  
  34.         vector2.x = x_m * scale + screenWidth / 2 - wh.x * scale;  
  35.         vector2.y = y_m * scale + screenHeight / 2 - wh.y * scale;  
  36.         return vector2;  
  37.     }  
  38. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值