I'm kind new to LibGdx and I'm still learning. I'm working on a small game project to learn this nice framework. However, I'm having some issues with my game. My game is a top down shooter. There is a human character(space marine) that has to shoot an alien, which will be always moving in his direction until it dies. My code doesn't have any bullets yet.I was able to make the shooter rotate in the direction of the cursor, as well, the alien rotate in the direction of the shooter, but I can't make the alien move in his direction. I'm pretty sure that my method that does that is completely wrong. I've tried a lot to fix it , but I can't get it fixed ! I appreciate your attention guys ! You can find the full code of the main class here: https://github.com/Igor-Lopes/LibGdx/blob/master/Splash.java#L125
public void moveAlien() {
float mX = 0;
float mY = 0;
int velocity = 50;
vAlien = new Vector2(-1 * (float) Math.sin(Alienbody.getAngle()) * velocity,
(float) Math.cos(Alienbody.getAngle() * velocity));
mX = (float) Math.cos(Math.toRadians(spacemarine.getRotation()));
mY = (float) Math.sin(Math.toRadians(spacemarine.getRotation()));
vAlien.x = mX;
vAlien.y = mY;
if (vAlien.len() > 0) {
vAlien = vAlien.nor();
}
vAlien.x = vAlien.x * velocity;
vAlien.y = vAlien.y * velocity;
vAlien.x += vAlien.x * Gdx.graphics.getDeltaTime();
vAlien.y += vAlien.x * Gdx.graphics.getDeltaTime();
}
public float rotateMarine() {
float angle = 0;
float mouseX = 0;
float mouseY = 0;
mouseX = Gdx.input.getX();
mouseY = 677 - Gdx.input.getY();
angle = (float) Math.toDegrees(Math.atan2(mouseX - spacemarine.getX(),
mouseY - spacemarine.getY()));
if (angle < 0)
angle += 360;
spacemarine.setRotation(angle * -1);
return angle;
}
public float rotateAlien(Sprite s, float posX, float posY) {
float angle = 0;
float mouseX = 0;
float mouseY = 0;
mouseX = posX;
mouseY = posY;
angle = (float) Math.toDegrees(Math.atan2(mouseX - s.getX(), mouseY - s.getY()));
if (angle < 0)
angle += 360;
s.setRotation(angle * -1);
return angle;
}
@Override
public void render(float delta) {
float angle;
moveAlien();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
angle = rotateMarine();
rotateAlien(alien, spacemarine.getX(),
spacemarine.getY());
if (Gdx.input.isKeyPressed(Input.Keys.W) == true
&& spacemarine.getY() < 560) {
Marinebody.setTransform(spacemarine.getX(), spacemarine.getY() + 4,
angle);
}
if (Gdx.input.isKeyPressed(Input.Keys.S) == true
&& spacemarine.getY() > 0) {
Marinebody.setTransform(spacemarine.getX(), spacemarine.getY() - 4,
angle);
}
if (Gdx.input.isKeyPressed(Input.Keys.D) == true
&& spacemarine.getX() < 920) {
Marinebody.setTransform(spacemarine.getX() + 4, spacemarine.getY(),
angle);
}
if (Gdx.input.isKeyPressed(Input.Keys.A) == true
& spacemarine.getX() > 8) {
Marinebody.setTransform(spacemarine.getX() - 4, spacemarine.getY(),
angle);
}
alien.setPosition(Alienbody.getPosition().x, Alienbody.getPosition().y);
spacemarine.setPosition(Marinebody.getPosition().x, Marinebody.getPosition().y);
batch.begin();
spacemarine.draw(batch);
alien.draw(batch);
batch.end();
// stage.act();
// stage.draw();
}