[LIBGDX学习]LibGDX代码详解(十六)Box2D prismatic joint
2018-02-23 17:27
375 查看
package com.mygdx.game.box2d; import com.badlogic.gdx.Input.Keys; 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.EdgeShape; import com.badlogic.gdx.physics.box2d.PolygonShape; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.physics.box2d.joints.PrismaticJoint; import com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef; public class Prismatic extends Box2DTest { PrismaticJoint m_joint; @Override protected void createWorld (World world) { Body ground; { BodyDef bd = new BodyDef(); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40, 0)); ground.createFixture(shape, 0); shape.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(2, 5); BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; //bd.position.set(-10, 10);// 相当于第二个轴的位置 bd.position.set(2, 2); bd.angle = 0.5f * (float)Math.PI; bd.allowSleep = false; Body body = world.createBody(bd); body.createFixture(shape, 5.0f); PrismaticJointDef pjd = new PrismaticJointDef();// 就是会使物体移动 //Vector2 axis = new Vector2(2, 1); Vector2 axis = new Vector2(1.5f, 1); axis.nor();// 变成标准长度 pjd.initialize(ground, body, new Vector2(0, 0), axis);// axis 就是连着地面的那根不动的轴 pjd.motorSpeed = 10.0f; pjd.maxMotorForce = 10000.0f; pjd.enableMotor = true; // pjd.lowerTranslation = 0;// 可移动的距离,上下限? // pjd.upperTranslation = 20.0f; pjd.lowerTranslation = 0f; pjd.upperTranslation = 20f; pjd.enableLimit = true; m_joint = (PrismaticJoint)world.createJoint(pjd); } } public boolean keyDown (int keyCode) { if (keyCode == Keys.L) m_joint.enableLimit(!m_joint.isLimitEnabled());// 这个一关就无限运动 if (keyCode == Keys.M) m_joint.enableMotor(!m_joint.isMotorEnabled()); if (keyCode == Keys.S) m_joint.setMotorSpeed(-m_joint.getMotorSpeed()); return false; } public void render () { super.render(); // if (renderer.batch != null) { // renderer.batch.begin(); // // renderer.batch.drawText(renderer.font, "Keys: (l) limits, (m) motors, (s) speed", 0, // Gdx.app.getGraphics().getHeight(), // // Color.WHITE); // // renderer.batch.drawText(renderer.font, "Motor Force = " + m_joint.getMotorForce(), 0, // // Gdx.app.getGraphics().getHeight() - 15, Color.WHITE); // renderer.batch.end(); // } } }
相关文章推荐
- [LIBGDX学习]LibGDX代码详解(九)Box2D revolute joint
- [LIBGDX学习]LibGDX代码详解(十四)Box2D apply force
- [LIBGDX学习]LibGDX代码详解(二十二)Box2D 弹性
- [LIBGDX学习]LibGDX代码详解(十一)Box2D bridge
- [LIBGDX学习]LibGDX代码详解(三)Box2D character control
- [LIBGDX学习]LibGDX代码详解(十三)Box2D wield joint
- [LIBGDX学习]LibGDX代码详解(二十三)Box2D 传送带
- [LIBGDX学习]LibGDX代码详解(十八)Box2D
- [LIBGDX学习]LibGDX代码详解(十七) Box2D character collision
- [LIBGDX学习]LibGDX代码详解(十九)Box2D 金字塔
- [LIBGDX学习]LibGDX代码详解(十五)Box2D
- [LIBGDX学习]LibGDX代码详解(十八)Box2D body type
- [LIBGDX学习]LibGDX代码详解(二十一)Box2D 子弹穿透
- [LIBGDX学习]LibGDX代码详解(四)Box2D
- [LIBGDX学习]LibGDX代码详解(五)Box2D
- [LIBGDX学习]LibGDX代码详解(七)Box2D
- [LIBGDX学习]LibGDX代码详解(二十四)Bresenham快速画直线算法
- [LIBGDX学习]LibGDX代码详解(三十四)Bullet
- LIBGDX学习]LibGDX代码详解(三十七)Bullet
- [LIBGDX学习]LibGDX代码详解(二十五)Buffer测试