Mr. Developer
Mr. Developer

Reputation: 3417

how to rotate a vector along with a sprite?

my need is to rotate a spaceship together with a carrier , I'll explain , I start from the start with this :

Step 1

Point A on the vector is the point of origin , and coincides with the sprite of the spacecraft . Now , my problem is that when the spacecraft rotates , the red dot ( which is another sprite ) to the head of the spacecraft must remain on the head .

enter image description here

If I rotate the spacecraft with the touch screen , I could use the red dot remain on point B on regular basis .

This is because I have to feed the spaceship , so I can compare the point of origin of the ship with the point of the red dot so as to obtain the direction in which it must continue.

I'm new to libgdx . I hope I was clear .

Thank You

Extract of my code:

    sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth()/2, Gdx.graphics.getHeight() / 2 - sprite.getHeight()/2);
    point.setPosition(sprite.getX() + (sprite.getWidth()/2 - point.getWidth()/2), sprite.getY() + (sprite.getHeight() / 2 + point.getHeight()/2));

    if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){

        if(Gdx.input.getX() < Gdx.graphics.getWidth() / 2)
        {
            //System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
            sprite.setRotation(rotation++);
            point.setPosition(sprite.getX() + (sprite.getWidth()/2 - point.getWidth()/2), sprite.getY() + (sprite.getHeight() / 2 + point.getHeight()/2));
        }
        else
        {
            //System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
            sprite.setRotation(rotation--);
            point.setPosition(sprite.getX() + (sprite.getWidth()/2 - point.getWidth()/2), sprite.getY() + (sprite.getHeight() / 2 + point.getHeight()/2));
        }
    }

EDIT AFTER ANSWER i have added the code for Actor

public class MyActor extends Actor {
    Sprite sprite;

    public MyActor() {
        sprite = new Sprite();
        sprite.setTexture(new Texture("rocket.png"));

        setWidth(sprite.getWidth());
        setHeight(sprite.getHeight());
        setBounds(0, 0, getWidth(), getHeight());
        setTouchable(Touchable.enabled);
        setX(200);
        setY(100);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(sprite, getX(), getY());
    }
}

but i'm not sure for the implementation...can you give me some advice?

Upvotes: 1

Views: 143

Answers (2)

MilanG
MilanG

Reputation: 7114

Just rotate red dot the same way you rotate the spaceship. Just offset rotation center of red dot, to be at same position where rotation point of spaceship is.. So don't rotate red dot around it's center but around spaceship center.

Upvotes: 1

Tanmay Patil
Tanmay Patil

Reputation: 7057

I would suggest you to use Actor instead of Sprite.

You would get access to many higher level functionalities not available for sprites.

You can even group the actors and rotate groups as you wish.

Reference: Scene2D

Hope this helps.
Good luck.

Upvotes: 0

Related Questions