public class PhysicsRevoluteJointExample extends BasePhysicsJointExample {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public Scene onLoadScene() {
final Scene scene = super.onLoadScene();
this.initJoints(scene);
Toast.makeText(this, "In this example, the revolute joints have their motor enabled.", Toast.LENGTH_LONG).show();
return scene;
}
// ===========================================================
// Methods
// ===========================================================
private void initJoints(final Scene pScene) {
final int centerX = CAMERA_WIDTH / 2;
final int centerY = CAMERA_HEIGHT / 2;
final int spriteWidth = this.mBoxFaceTextureRegion.getTileWidth();
final int spriteHeight = this.mBoxFaceTextureRegion.getTileHeight();
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(10, 0.2f, 0.5f);//密度,弹性,摩擦
for(int i = 0; i < 3; i++) {
//三个盒子位置
final float anchorFaceX = centerX - spriteWidth * 0.5f + 220 * (i - 1);
final float anchorFaceY = centerY - spriteHeight * 0.5f;
//盒子固定
final AnimatedSprite anchorFace = new AnimatedSprite(anchorFaceX, anchorFaceY, this.mBoxFaceTextureRegion);
final Body anchorBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, anchorFace, BodyType.StaticBody, objectFixtureDef);
//移动的圆
final AnimatedSprite movingFace = new AnimatedSprite(anchorFaceX, anchorFaceY + 90, this.mCircleFaceTextureRegion);
final Body movingBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, movingFace, BodyType.DynamicBody, objectFixtureDef);
anchorFace.animate(200);
anchorFace.animate(200);
anchorFace.setUpdatePhysics(false);
movingFace.setUpdatePhysics(false);
pScene.getTopLayer().addEntity(anchorFace);
pScene.getTopLayer().addEntity(movingFace);
//三条线
final Line connectionLine = new Line(anchorFaceX + spriteWidth / 2, anchorFaceY + spriteHeight / 2, anchorFaceX + spriteWidth / 2, anchorFaceY + spriteHeight / 2);
pScene.getBottomLayer().addEntity(connectionLine);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(anchorFace, anchorBody, true, true, false, false){
@Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
final Vector2 movingBodyWorldCenter = movingBody.getWorldCenter();
connectionLine.setPosition(connectionLine.getX1(), connectionLine.getY1(), movingBodyWorldCenter.x * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, movingBodyWorldCenter.y * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
}
});
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(movingFace, movingBody, true, true, false, false));
final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(anchorBody, movingBody, anchorBody.getWorldCenter());//绕中心转
revoluteJointDef.enableMotor = true;//马达
revoluteJointDef.motorSpeed = 10;
revoluteJointDef.maxMotorTorque = 200;//扭矩
this.mPhysicsWorld.createJoint(revoluteJointDef);
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}