您的位置:首页 > 编程语言

[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();
// }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: