Skip to content Skip to sidebar Skip to footer

Code In Java(android Studio) Libgdx, How To Calculate For A Projectile

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 ba

Solution 1:

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

float speed,angle;

Vector2startingVelocity=newVector2(speed,speed);
startingVelocity.rotate((float) angle - 45);

body.setLinearVelocity(startingVelocity);

speed and angle is provided by you.

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

publicclassTestGameextendsGameimplementsInputProcessor{

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

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

    @Overridepublicvoidcreate() {

        spriteBatch=newSpriteBatch();
        ballTex=newTexture("image/ball.png");
        ball=newSprite(ballTex);
        ball.setSize(50,50);
        ball.setPosition(0,0);

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

    @Overridepublicvoidrender() {
        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();
    }

    privatevoidupdateBall(){

        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);
        }

    }

    @Overridepublicvoidresize(int width, int height) {
        super.resize(width, height);
    }

    @Overridepublicvoiddispose() {
        super.dispose();
        ballTex.dispose();
        spriteBatch.dispose();
    }

    @OverridepublicbooleankeyDown(int keycode) {

        returnfalse;
    }

    @OverridepublicbooleankeyUp(int keycode) {
        returnfalse;
    }

    @OverridepublicbooleankeyTyped(char character) {
        returnfalse;
    }

    @OverridepublicbooleantouchDown(int screenX, int screenY, int pointer, int button) {

        isFired=true;
        returnfalse;
    }

    @OverridepublicbooleantouchUp(int screenX, int screenY, int pointer, int button) {
        returnfalse;
    }

    @OverridepublicbooleantouchDragged(int screenX, int screenY, int pointer) {
        returnfalse;
    }

    @OverridepublicbooleanmouseMoved(int screenX, int screenY) {
        returnfalse;
    }

    @Overridepublicbooleanscrolled(int amount) {
        returnfalse;
    }
}

Post a Comment for "Code In Java(android Studio) Libgdx, How To Calculate For A Projectile"