简体   繁体   English

AndEngine和Box2D中的弹簧墙

[英]Spring wall in AndEngine and Box2D

I use Box2D in my game, but I would like to have a nicely looking spring - wall to push the player when touched. 我在游戏中使用Box2D,但我希望有一个外观漂亮的弹簧-碰壁时可以推动玩家。 It would look like this (in 3 frames): 看起来像这样(3帧): 在此处输入图片说明

Question: how to implement it? 问题:如何实施? Can I attach a wall effect to an animated sprite? 我可以在动画精灵上附加墙效果吗?

The answer is: Prismatic Joint. 答案是:棱柱接头。 I divided the image into 2 parts: static and dynamic (the moving bar). 我将图像分为两部分:静态和动态(移动条)。 The below code is for creation of a prismatic joint in orientation like in the image in my question: 以下代码用于创建棱柱形接头,其方向类似于我所询问的图像:

//prismatic joint
    final Sprite springFrameT = new Sprite(pX, pY, mSpringFrameTRegion, getVertexBufferObjectManager());
    final Sprite springBarT = new Sprite(pX, pY + mSpringFrameTRegion.getHeight()-mSpringBarTRegion.getHeight(), 
            mSpringBarTRegion, getVertexBufferObjectManager());        
    mMainScene.attachChild(springFrameT);
    mMainScene.attachChild(springBarT);        
    mMapSprites.add(springFrameT);
    mMapSprites.add(springBarT);

    final Body springFrameBody = PhysicsFactory.createBoxBody(mPhysicsWorld, springFrameT, BodyType.StaticBody, FIXTURE_DEF);
    final Body springBarBody = PhysicsFactory.createBoxBody(mPhysicsWorld, springBarT, BodyType.DynamicBody, SPRING_FIXTURE_DEF);        
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(springFrameT, springFrameBody, false, false));
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(springBarT, springBarBody, true, true));

    final PrismaticJointDef prismaticJointDef = new PrismaticJointDef();        
    prismaticJointDef.initialize(springFrameBody, springBarBody, springFrameBody.getWorldCenter(), // new Vector2(springFrameT.getWidth(), springFrameT.getHeight()/2), 
            new Vector2(0, 1.0f)); 
    prismaticJointDef.lowerTranslation = -0.5f;
    prismaticJointDef.upperTranslation = 0.5f;
    prismaticJointDef.enableLimit = true;
    prismaticJointDef.enableMotor = true;
    prismaticJointDef.maxMotorForce = 100.0f;
    prismaticJointDef.motorSpeed = 100000f;
    prismaticJointDef.collideConnected = false;
    this.mPhysicsWorld.createJoint(prismaticJointDef);         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM