2
Java中的代碼(Android Studio)libgdx,...當你點擊/觸摸時如何計算圓的彈丸(像一個球)屏幕,你會如何顯示它? 像射擊一個籃球 ....例如圓是在0,0(x,y)與50度角... 感謝!Java中的代碼(Android Studio)libgdx,如何計算彈丸
Java中的代碼(Android Studio)libgdx,...當你點擊/觸摸時如何計算圓的彈丸(像一個球)屏幕,你會如何顯示它? 像射擊一個籃球 ....例如圓是在0,0(x,y)與50度角... 感謝!Java中的代碼(Android Studio)libgdx,如何計算彈丸
如果您使用的Box2D然後拋運動是由您的Box2D引擎處理。你只需要應用線速度。
float speed,angle;
Vector2 startingVelocity =new Vector2(speed,speed);
startingVelocity.rotate((float) angle - 45);
body.setLinearVelocity(startingVelocity);
速度和角度由您提供。
如果你不使用box2d,那麼你需要像這樣處理你的球的位置和速度。
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;
}
}
非常感謝你,非常感謝。 –
問題不明確。發佈和圖像示例,並添加一些你已經嘗試過的代碼。 – JonZarate
歡迎來到Stack Overflow!請參考[遊覽](http://stackoverflow.com/tour),環顧四周,閱讀[幫助中心](http://stackoverflow.com/help),特別是[我該如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)和[我可以問什麼問題?](http://stackoverflow.com/help/on-topic)。 –
你使用box2d嗎? – Aryan