AndEngine之PhysicsRevoluteJointExample

本文介绍了一个使用物理铰链关节的示例程序,通过创建静态盒子和动态圆形物体,并利用铰链关节让圆形物体围绕盒子旋转。示例中详细展示了如何设置物体属性、创建关节及连接线等。
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
    // ===========================================================
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值