Java(Android Studio)libgdx中的代码,如何计算弹丸

问题描述:

Java(Android Studio)libgdx中的代码,...单击/触摸屏幕时如何计算圆(如球)的弹丸,以及如何显示? 就像打篮球....例如,圆圈位于0,0(x,y)中,夹角为50度... 谢谢!

Code in Java(Android Studio) libgdx,... how to calculate for a projectile for a circle(like a ball) when you click/touch the screen and how would you display it? like shooting a basketball....for example circle is in 0,0(x,y) with an angle of 50 degrees... THANKS!!

如果您使用的是box2d,则子弹运动将由box2d引擎处理.您只需要应用线速度即可.

If you're using box2d then projectile motion is handled by your box2d engine. You just need to apply linear velocity.

float speed,angle;

Vector2 startingVelocity =new Vector2(speed,speed);
startingVelocity.rotate((float) angle - 45);

body.setLinearVelocity(startingVelocity);

速度和角度由您提供.

speed and angle is provided by you.

如果您不使用box2d,则需要像这样处理球的位置和速度.

If you're not using box2d then you need to handle position and velocity of your ball like this.

public class TestGame extends Game implements InputProcessor{

    SpriteBatch spriteBatch;
    Sprite ball;
    Texture ballTex;
    boolean isFired;

    Vector2 gravity;
    private float throwAngle=50;
    private float deltaTime=2;
    private Vector2 initialVelocity;

    @Override
    public void create() {

        spriteBatch=new SpriteBatch();
        ballTex=new Texture("image/ball.png");
        ball=new Sprite(ballTex);
        ball.setSize(50,50);
        ball.setPosition(0,0);

        Gdx.input.setInputProcessor(this);
        gravity=new Vector2(0, -Gdx.graphics.getHeight()*.05f);
        float throwVelocity=Gdx.graphics.getWidth()*.3f;
        initialVelocity=new Vector2((float)(throwVelocity*Math.sin(throwAngle * Math.PI / 180)),(float)(throwVelocity*Math.cos(throwAngle * Math.PI / 180)));
    }

    @Override
    public void render() {
        super.render();

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

        updateBall();

        spriteBatch.begin();
        ball.draw(spriteBatch);
        spriteBatch.end();
    }

    private void updateBall(){

        if(isFired){

            float delta=Gdx.graphics.getDeltaTime();
            initialVelocity.x=initialVelocity.x+gravity.x*delta*deltaTime;
            initialVelocity.y=initialVelocity.y+gravity.y*delta*deltaTime;

            ball.setPosition(ball.getX()+initialVelocity.x * delta * deltaTime,ball.getY()+initialVelocity.y * delta * deltaTime);
        }

    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
    }

    @Override
    public void dispose() {
        super.dispose();
        ballTex.dispose();
        spriteBatch.dispose();
    }

    @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) {

        isFired=true;
        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;
    }
}