像导弹一样发射射弹 - box2d和libgdx

时间:2014-10-06 15:30:58

标签: libgdx box2d projectile

我想以恒定速度(x,y)发射像导弹一样的抛射物(物体),但我希望弹丸能够根据用户的触感飞行。

我怎样才能达到这样的目标?

P.S我试图做这样的事情,但这不是我需要的。无论用户触摸如何,它都会飞到恒定点。它也受到重力的影响:

package com.david.helpers;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.utils.Array;
import com.david.objects.Acorn;

public class InputHandler implements InputProcessor{

    private Body ground;
    private Vector2 target;
    private Vector3 temp;
    private OrthographicCamera camera;
    private Array<Acorn> acorns;
    private static final float Pixels_To_Meters = 32.0f;
    public boolean isTouchDown = false;
    public InputHandler(Body ground,  Array<Acorn> acorns, OrthographicCamera camera) {
        this.camera = camera;
        this.ground = ground;
        this.acorns = acorns;
        target = new Vector2();
        temp = new Vector3();
    }
    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        camera.unproject(temp.set(screenX, screenY, 0));
        target.x = temp.x;
        target.y = temp.y;
        isTouchDown = true;
        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        acorns.peek().getBody().applyLinearImpulse(20-target.x/Pixels_To_Meters, 20-target.y/Pixels_To_Meters*0.1f, target.x, target.y, true);
        isTouchDown = false;
        return true;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub
        camera.unproject(temp.set(screenX, screenY, 0));
        target.x = temp.x;
        target.y = temp.y;
        return true;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        // TODO Auto-generated method stub
        return false;
    }
    public Vector2 getTarget() {
        return this.target;
    }
}

无论用户触摸如何,身体都会移动到恒定点。

0 个答案:

没有答案