2
\$\begingroup\$

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();
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Try using vector math instead of cos/sin angles. Something like this might work for you;

public void moveAlien() {
    float velocity = 50;

    Vector2 ap = new Vector2(AlienBody.getPosition());
    Vector2 mp = new Vector2(Marinebody.getPosition());

    Vector2 delta = mp.sub(ap).nor(); // This is not a unit vector pointing in the direction from ap to mp

    // Take alien current position and add the delta times velocity times delta-time
    Vector2 newPos = new Vector2(AlienBody.getPosition());
    newPos.add(delta.mul(velocity * Gdx.graphics.getDeltaTime()));

    // At this point newPos has the new position of the alien, now you just need to apply that to 
    // the AlienBody position
}

I can't see in your code how you apply the newly created position vAlien to the alien body, so I've left that out. But at the end of my moveAlien method, take newPos and set that to the position of the AlienBody.

\$\endgroup\$
5
  • \$\begingroup\$ I've tried it and didn't work. First, the alien moves extremely fast to the shooter's position and then when it reaches him, it doesn't move anymore. I also didn't understand what you've meant with newPos.add(delta.mul(velocity * Gdx.graphics.getDeltaTime())); because it says that is wrong. So I'm just doing Alienbody.setTransform(newPos.x * Gdx.graphics.getDeltaTime() * velocity, newPos.y * Gdx.graphics.getDeltaTime()* velocity, angle); which angle it's the rotation. Can you help me please? \$\endgroup\$
    – Igor Lopes
    Commented Mar 12, 2015 at 15:14
  • \$\begingroup\$ If it moves too fast, change velocity to a lower value. And where is it supposed to go when it has reached the player? And what, specifically, does it say is wrong with; newPos.add(delta.mul(velocity * Gdx.graphics.getDeltaTime())); \$\endgroup\$
    – bornander
    Commented Mar 12, 2015 at 15:32
  • \$\begingroup\$ I've tried change the velocity, but it doesn't work. I get a syntax error with newPos.add(delta.mul(velocity * Gdx.graphics.getDeltaTime())); like this function doesn't exists. \$\endgroup\$
    – Igor Lopes
    Commented Mar 12, 2015 at 15:52
  • \$\begingroup\$ Yeah, sorry, the method is called scl, not mul. So change scl to mul. \$\endgroup\$
    – bornander
    Commented Mar 12, 2015 at 15:56
  • \$\begingroup\$ Yeah the function it's right now, but it doesn't move the alien. I was trying to debug and got those print statements as the NewPos.x 393.9828 394.0326 393.92984 394.04578 If you take a look you will see there is something wrong it won't move, it always decrements and increments \$\endgroup\$
    – Igor Lopes
    Commented Mar 12, 2015 at 16:12
1
\$\begingroup\$

I had the solution for awhile, but I forgot to post. Here's the method to move a list of enemies towards the player.

public void moveAlien() {

for (Aliens a : aliens) { //List of Aliens (Enemy)
    Sprite s = a.getSprite(); //Get current enemy's sprite
    float targetX = spacemarine.getX(); //Player's X
    float targetY = spacemarine.getY(); //Player's Y
    float spriteX = s.getX(); //Enemy's X
    float spriteY = s.getY(); //Enemy's Y
    float x2 = s.getX(); //Enemy's new X
    float y2 = s.getY(); //Enemy's new Y
    float angle; // We use a triangle to calculate the new trajectory
        angle = (float) Math
                .atan2(targetY - spriteY, targetX - spriteX);
        x2 += (float) Math.cos(angle) * 125
                * Gdx.graphics.getDeltaTime();
        y2 += (float) Math.sin(angle) * 125
                * Gdx.graphics.getDeltaTime();
        s.setPosition(x2, y2); //Set enemy's new positions.
    }

}

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .