I'm trying to shoot bullets where the mouse is aiming using one image as a test. I'm trying to get the mechanics like this
I have some kind of algorithm to handle that but the results arn't too good. Can anyone help me make the bullet fly towards the mouse's exact location? My code:
public class GameScreen implements Screen{
MyGame mg;
SpriteBatch batch;
Sprite s;
float timeStep=1f/60f;
float turretX=10f;
float turretY=10f;
float bulletSpeed=0.1f;
float bulletX=turretX;
float bulletY=turretY;
float dirX= 100 - turretX;
float dirY= 100 - turretY;
public GameScreen(MyGame mg) {
this.mg = mg;
float dirLength= (float) Math.sqrt(dirX*dirX + dirY*dirY);
dirX=dirX/dirLength;
dirY=dirY/dirLength;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
update(delta);
batch.begin();
batch.draw(s, bulletX, bulletY);
batch.end();
}
public void update(float delta) {
if(Gdx.input.isKeyPressed(Keys.SPACE)) {
dirX= Gdx.input.getX() - turretX;
dirY= Gdx.input.getY() - turretY;
}
bulletX=bulletX+(dirX*bulletSpeed*timeStep);
bulletY=bulletY+(dirY*bulletSpeed*timeStep);
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() {
batch = new SpriteBatch();
s = new Sprite(new Texture(Gdx.files.internal("HUDS/button_Back.png")));
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}