DGLeiva
DGLeiva

Reputation: 111

How to move a Sprite after rotation In the indicated direction? Libgdx - Java - Android

I have a video game in which an arrow moves towards the side where it is pointing, after rotation the arrow, example:

enter image description here

I need to move the sprite To the same direction in which the arrow points after it has been rotation. A bit of code As I'm trying to do:

int count = 0;
@Override
protected void handleInput() {
     if(Gdx.input.justTouched()){
         // move to the direction of pointing:
         arrow.setPosition(x, y);
     }

}

public void update(float dt){

    count++;
    // rotate sprite:
    arrow.setRotation(count);

}

Upvotes: 3

Views: 1580

Answers (3)

DGLeiva
DGLeiva

Reputation: 111

I resolved with this page: enter link description here

        float mX = 0;
float mY = 0;
int velocity = 5;
private float posAuxX = 0;
private float posAuxY = 0;
int count = 0;
public void update(float dt){
    count2++;
    flecha.setRotation(count2);
    if (count2 >= 360){
        count2 =  0;
    }
    position = new Vector2((float) Math.sin(count2) * velocity,
            (float) Math.cos(count2 * velocity));
    mX = (float) Math.cos(Math.toRadians(flecha.getRotation()));
    mY = (float) Math.sin(Math.toRadians(flecha.getRotation()));
    position.x = mX;
    position.y = mY;
    if (position.len() > 0){
        position = position.nor();
    }
    position.x = position.x * velocity;
    position.y = position.y * velocity;
    posAuxX = flecha.getX();
    posAuxY = flecha.getY();

}

flecha.setPosition(posAuxX, posAuxY);

Upvotes: 0

user201891
user201891

Reputation:

In the book "Beginning Java Game Development with LibGDX" the author makes a game that I think demonstrates the behaviour you want. The game is "Starfish Collector" from chapter 3. The player moves a turtle to collect starfish. The left and right arrow keys rotate the turtle, and the up arrow key moves the turtle forward in the direction he is currently facing.

The source code for the game can be downloaded from the author's Github account here. (I don't know why he put it in a zip file.)

The relevant code looks like this:

@Override
public void update(float dt) {
    // process input
    turtle.setAccelerationXY(0, 0);

    if (Gdx.input.isKeyPressed(Keys.LEFT)) {
        turtle.rotateBy(90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
        turtle.rotateBy(-90 * dt);
    }
    if (Gdx.input.isKeyPressed(Keys.UP)) {
        turtle.accelerateForward(100);
    }
    // ...

Where turtle extends some custom classes that extend Actor.

The code for accelerateForward looks like this:

public void accelerateForward(float speed) {
    setAccelerationAS(getRotation(), speed);
}

And then the code for setAccelerationAS looks like this:

// set acceleration from angle and speed
public void setAccelerationAS(float angleDeg, float speed) {
    acceleration.x = speed * MathUtils.cosDeg(angleDeg);
    acceleration.y = speed * MathUtils.sinDeg(angleDeg);
}

Note that this last bit of code is probably exactly what user unexistential was referring to.

(I recommend this book if you're learning LibGDX and game development. It's very good.)

See also:

Upvotes: 2

nradk
nradk

Reputation: 700

The simplest way would be to use the sine and cosine of the rotation amount to determine the x and y components of the translation vector.

Upvotes: 1

Related Questions