Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Libgdx从枪中爆炸_Java_Algorithm_Animation_Libgdx - Fatal编程技术网

Java Libgdx从枪中爆炸

Java Libgdx从枪中爆炸,java,algorithm,animation,libgdx,Java,Algorithm,Animation,Libgdx,在我的游戏中有一个坦克。我有一个木桶类,它扩展了Sprite这个类负责木桶相关的东西,其中之一就是拍摄时的爆炸动画。我就是这样做的: batch.draw(currShotAnimation, b2body.getPosition().x - currShotAnimation.getRegionWidth() / 2, b2body.getPosition().y + 10, 0, 0,

在我的游戏中有一个坦克。我有一个木桶类,它扩展了Sprite这个类负责木桶相关的东西,其中之一就是拍摄时的爆炸动画。我就是这样做的:

batch.draw(currShotAnimation,
            b2body.getPosition().x - currShotAnimation.getRegionWidth() / 2,
            b2body.getPosition().y + 10,
            0,
            0,
            currShotAnimation.getRegionWidth(),
            currShotAnimation.getRegionHeight(),
            1,
            1,
            getRotation());
当油箱向前看时,它正在工作: 但当他看不同的地方时,看起来很奇怪:

我怎样才能修复它,使它可以向各个方向工作

编辑: 在尝试了阿比舍克·雅利安(Abhishek Aryan)的建议后,我的像素测量是:

        batch.draw(currShotAnimation,
            b2body.getPosition().x + offset * MathUtils.cosDeg(getRotation()),
            b2body.getPosition().y + offset * MathUtils.sinDeg(getRotation()),
            currShotAnimation.getRegionWidth()/2f,
            currShotAnimation.getRegionHeight()/2f,
            currShotAnimation.getRegionWidth(),
            currShotAnimation.getRegionHeight(),
            1,
            1,
            getRotation());
看起来是这样的:
这里的问题是,draw方法在其左下角绘制对象,并围绕该对象旋转。如果您想了解我的意思,请用以下内容替换该位置:

b2body.getPosition().x,
b2body.getPosition().y,
现在你的爆炸应该一直在旋转,并且被放置在它的左下角。现在,尝试将您的originx和originy设置为0


这样,您的精灵将围绕其底部中心旋转,这将使事情变得更容易。但是,它仍将位于其左下角。然而,您不能仅仅添加一些值,这就是您在上面所做的。如果你的桶是朝下的,你不想在y值上加10,这会把精灵向上推,因为它应该向下推一点,所以它在你的水箱下面。您将需要使用一些三角学来根据精灵的旋转来定位精灵。寻找一个非常类似的问题。我希望您现在能理解您的问题。

这里的问题是,draw方法在其左下角绘制对象,并围绕该对象旋转。如果您想了解我的意思,请用以下内容替换该位置:

b2body.getPosition().x,
b2body.getPosition().y,
现在你的爆炸应该一直在旋转,并且被放置在它的左下角。现在,尝试将您的originx和originy设置为0


这样,您的精灵将围绕其底部中心旋转,这将使事情变得更容易。但是,它仍将位于其左下角。然而,您不能仅仅添加一些值,这就是您在上面所做的。如果你的桶是朝下的,你不想在y值上加10,这会把精灵向上推,因为它应该向下推一点,所以它在你的水箱下面。您将需要使用一些三角学来根据精灵的旋转来定位精灵。寻找一个非常类似的问题。我希望您现在能理解您的问题。

b2body是您的box2d body吗?如果是,则将仪表转换为像素,以便在屏幕上绘制

private float offset=10;
private float METER_PIXEL=32;

batch.draw(textureRegion,b2body.getPosition().x*METER_PIXEL+ offset *MathUtils.cosDeg(getRotation()),b2body.getPosition().y*METER_PIXEL+ offset*MathUtils.sinDeg(getRotation()),currShotAnimation.getRegionWidth()/2f,currShotAnimation.getRegionHeight()/2f,currShotAnimation.getRegionWidth(),currShotAnimation.getRegionHeight(),1,1,getRotation());
在这里,您将使用精灵旋转来旋转火焰动画。所以你需要根据b2dbody旋转来旋转你的精灵,这样你可以在这里使用,否则在这里你需要得到b2body旋转的弧度,并转换成度,在这里使用

这里我测试了一个纹理区域的偏移量

 public class TestGame extends Game implements InputProcessor{

    private SpriteBatch spriteBatch;
    private Sprite sprite;
    private TextureRegion textureRegion;
    private float offset = 20;
    private firstTex,secondTex; 

    @Override
    public void create() {

        spriteBatch=new SpriteBatch();

        textureRegion=new TextureRegion(firstTex=new Texture("im.png"));
        sprite=new Sprite(secondTex=new Texture("xyz.png"));
        sprite.setPosition(100,100);

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render() {

        Gdx.gl.glClearColor(1,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        spriteBatch.begin();
        sprite.draw(spriteBatch);
        spriteBatch.draw(textureRegion,sprite.getX()+ offset *MathUtils.cosDeg(sprite.getRotation()),sprite.getY()+(offset)*MathUtils.sinDeg(sprite.getRotation()),sprite.getWidth()/2f,sprite.getHeight()/2f,sprite.getWidth(),sprite.getHeight(),1,1,sprite.getRotation());
        spriteBatch.end();

        if(Gdx.input.isTouched()){
            float rotation=sprite.getRotation();
            rotation++;
            sprite.setRotation(rotation);
        }

    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        float rotation=sprite.getRotation();
        rotation++;
        sprite.setRotation(rotation);

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }

    @Override
    public void dispose(){
       firstTex.dispose();
       secondTex.dispose();
       spriteBatch.dispose();
   }
}

b2body是您的box2d body吗?如果是,则将仪表转换为像素,以便在屏幕上绘制

private float offset=10;
private float METER_PIXEL=32;

batch.draw(textureRegion,b2body.getPosition().x*METER_PIXEL+ offset *MathUtils.cosDeg(getRotation()),b2body.getPosition().y*METER_PIXEL+ offset*MathUtils.sinDeg(getRotation()),currShotAnimation.getRegionWidth()/2f,currShotAnimation.getRegionHeight()/2f,currShotAnimation.getRegionWidth(),currShotAnimation.getRegionHeight(),1,1,getRotation());
在这里,您将使用精灵旋转来旋转火焰动画。所以你需要根据b2dbody旋转来旋转你的精灵,这样你可以在这里使用,否则在这里你需要得到b2body旋转的弧度,并转换成度,在这里使用

这里我测试了一个纹理区域的偏移量

 public class TestGame extends Game implements InputProcessor{

    private SpriteBatch spriteBatch;
    private Sprite sprite;
    private TextureRegion textureRegion;
    private float offset = 20;
    private firstTex,secondTex; 

    @Override
    public void create() {

        spriteBatch=new SpriteBatch();

        textureRegion=new TextureRegion(firstTex=new Texture("im.png"));
        sprite=new Sprite(secondTex=new Texture("xyz.png"));
        sprite.setPosition(100,100);

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render() {

        Gdx.gl.glClearColor(1,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        spriteBatch.begin();
        sprite.draw(spriteBatch);
        spriteBatch.draw(textureRegion,sprite.getX()+ offset *MathUtils.cosDeg(sprite.getRotation()),sprite.getY()+(offset)*MathUtils.sinDeg(sprite.getRotation()),sprite.getWidth()/2f,sprite.getHeight()/2f,sprite.getWidth(),sprite.getHeight(),1,1,sprite.getRotation());
        spriteBatch.end();

        if(Gdx.input.isTouched()){
            float rotation=sprite.getRotation();
            rotation++;
            sprite.setRotation(rotation);
        }

    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        float rotation=sprite.getRotation();
        rotation++;
        sprite.setRotation(rotation);

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }

    @Override
    public void dispose(){
       firstTex.dispose();
       secondTex.dispose();
       spriteBatch.dispose();
   }
}

batch.draw中的第二个和第三个参数是您的问题。如果要将其居中,应将其与currShotAnimation.getRegionWidth等分。所以它应该是这样的:

batch.draw(currShotAnimation,
        b2body.getPosition().x + offset * MathUtils.cosDeg(getRotation()) - currShotAnimation.getRegionWidth() / 2,
        b2body.getPosition().y + offset * MathUtils.sinDeg(getRotation()) - currShotAnimation.getRegionHeight() / 2,
        currShotAnimation.getRegionWidth()/2f,
        currShotAnimation.getRegionHeight()/2f,
        currShotAnimation.getRegionWidth(),
        currShotAnimation.getRegionHeight(),
        1,
        1,
        getRotation());
旋转时,这将使图像中的点原点居中

更新:我认为你计算角度的方式是错误的。应该是这样的

   b2body.getPosition().y +=  (Math.sin(rotation) * speed);
   b2body.getPosition().x +=  (Math.cos(rotation) * speed);
在你的“batch.draw”中;x和y参数将其更改为:

 b2body.getPosition().x -  currShotAnimation.getRegionWidth() / 2
 b2body.getPosition().y -  currShotAnimation.getRegionHeight() / 2

这应该是原点的中心。

batch.draw中的第二个和第三个参数是您的问题。如果要将其居中,应将其与currShotAnimation.getRegionWidth等分。所以它应该是这样的:

batch.draw(currShotAnimation,
        b2body.getPosition().x + offset * MathUtils.cosDeg(getRotation()) - currShotAnimation.getRegionWidth() / 2,
        b2body.getPosition().y + offset * MathUtils.sinDeg(getRotation()) - currShotAnimation.getRegionHeight() / 2,
        currShotAnimation.getRegionWidth()/2f,
        currShotAnimation.getRegionHeight()/2f,
        currShotAnimation.getRegionWidth(),
        currShotAnimation.getRegionHeight(),
        1,
        1,
        getRotation());
旋转时,这将使图像中的点原点居中

更新:我认为你计算角度的方式是错误的。应该是这样的

   b2body.getPosition().y +=  (Math.sin(rotation) * speed);
   b2body.getPosition().x +=  (Math.cos(rotation) * speed);
在你的“batch.draw”中;x和y参数将其更改为:

 b2body.getPosition().x -  currShotAnimation.getRegionWidth() / 2
 b2body.getPosition().y -  currShotAnimation.getRegionHeight() / 2

这应该是原点的中心。

谢谢,但仍然有一些问题,jest edited my Posts b2body是您的box2d body吗?然后需要通过此b2body.getPosition.x*米\u像素将box2d位置转换为像素进行绘图,y方向相同,所以像素是一个,所以这不是问题。顺便说一句,我用你的github共享了我的代码。如果你想看看,请提供你的项目链接谢谢,但仍然有一些问题,jest edited my Posts b2body是您的box2d body吗?然后需要通过此b2body将box2d位置转换为像素。getPosition.x*METER_像素用于绘图,y方向相同。Somy METER_像素是一个,所以这不是问题。顺便说一句,我将我的代码与您的github一起共享。如果您想查看,请提供我您的项目链接是否解决了此问题?看起来你的问题在于你的计算能力angle@lan是的,jest又增加了一张照片clrearer@Roni能够
你可以取出旋转的代码,让它在0。这样就更容易看出定位是否已经起作用了,或者是旋转造成了混乱。@Leon目前不在我家。我回家后再做这个问题解决了吗?看起来你的问题在于你的计算能力angle@lan是的,jest又增加了一张照片clrearer@Roni你能拿出旋转的代码,让它在0。这样就更容易看出定位是否已经起作用了,或者是旋转造成了混乱。@Leon目前不在我家。我回家后会做的我也试过用阿比舍克雅利安的三角学但仍然不起作用我也试过用阿比舍克雅利安的三角学但仍然不起作用working@lan我到了以后再测试home@lan我回家后会测试一下