1

andengineでレーシングゲームを作成しました.100x30pxの長方形のようなプレイヤーがいます.弾丸を発射するfire()関数があります.コードは以下の通りです.正常に動作します.しかし問題はプレイヤーが回転しているときです.プレイヤーは車の画像です。方向ボタン()で回転します。弾丸を撃つと、車の0x0pxから始まります。しかし、車の100x15pxのように、車の前で動き始めなければなりません。どうすればよいですか?

public void fire() {


        float startBulletX=player.getX();
        float startBulletY=player.getY();
        Sprite bullet=new Sprite(startBulletX,startBulletY,bulletRegion);

           final float xComp = (float) Math.cos(MathUtils.degToRad(this.player.getRotation() + 90));
           final float yComp = (float) Math.sin(MathUtils.degToRad(this.player.getRotation() + 90));
           final Vector2 velocity = Vector2Pool.obtain(xComp * 10, yComp * 10);


           bullet.setRotation(this.player.getRotation());

           final FixtureDef bulletFixtureDef1 = PhysicsFactory.createFixtureDef(0, 0, 0);
           this.mBulletBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, bullet, BodyType.KinematicBody, bulletFixtureDef1);
           this.mBulletBody.setLinearVelocity(velocity);
           Vector2Pool.recycle(velocity);
           this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bullet, this.mBulletBody, true, false));




        this.scene.attachChild(bullet);
    }

//オンスクリーンコントロールボタン

 public void addDirectionButton() {
       player = new AnimatedSprite(centerX, centerY, this.playerTextureRegion);
        final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 200, new IAnalogOnScreenControlListener() {

                 public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {

                         final Body carBody = DeffenseActivity.this.mCarBody;

                         final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5);
                         carBody.setLinearVelocity(velocity);
                         Vector2Pool.recycle(velocity);

                         final float rotationInRad = (float)Math.atan2(-pValueX, pValueY);

                         player.setRotation(MathUtils.radToDeg(rotationInRad));

                 }

                 public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {

                 }
         });
         analogOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
         analogOnScreenControl.getControlBase().setAlpha(0.5f);
         analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
         analogOnScreenControl.getControlBase().setScale(1.25f);
         analogOnScreenControl.getControlKnob().setScale(1.25f);
         analogOnScreenControl.refreshControlKnobPosition();

         this.scene.setChildScene(analogOnScreenControl);

         this.mBoundChaseCamera.setChaseEntity(player);
       a=new Sprite(0,0,bulletRegion);
         player.attachChild(a);

         final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
         this.mCarBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, carFixtureDef);
         this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, this.mCarBody, true, false));
         this.scene.attachChild(player);

       }
4

0 に答える 0