how to use a sprite as an anchor point of another

2019-08-07 01:43发布

I want to know , if you can , join two sprites in libgdx .

Let me explain, I have a spaceship that I have to turn in my game ... and is the first sprite .

another sprite I would put on the tip of the spacecraft so that when it turns me I through the second sprite I can always know the coordinates and so derive a vector in order to advance my ship .

I'm new to libgdx and if anyone had any idea would be grateful.

Thank You.

EDIT : I would follow that always render the position of the other sprites , currently does not happen , tips ? Thank You.

@Override
public void render () {

    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()));

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

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

    batch.begin();
    spacesprite.draw(batch);
    sprite.draw(batch);
    point.draw(batch);
    batch.end();
}

EDIT 2

Space Ship

As you can see in the picture , I would like that when I rotate the sprite of the spacecraft , the red dot ( which is another sprite ) remain on the tip of the spacecraft . So you can calculate a vector with the points of origin of the two sprites .

I hope to have been clear . A union of two sprites is not possible in libgdx else?

EDIT 3

Red Dot is not follow spaceship

picture , I wish that when the ship rotates the red ball remains always on the tip of the spacecraft . is it possible?

1条回答
▲ chillily
2楼-- · 2019-08-07 02:13

If your two sprite have the same width can use this:

this is psudo code mor or less.

float pointY = yourSprite.getY() + yourSprite.getHeight();
float pointX = yourSprite.getX(); 

yourOtherSprite. setX(pointX);
yourOtherSprite. setY(pointY);

if diferente width

float pointY = yourSprite.getY() + yourSprite.getHeight();
float pointX = yourSprite.getX() + (yourSprite.getWidth() / 2); 

yourOtherSprite. setX(pointX - (thisSprite.getWidth() / 2));
yourOtherSprite. setY(pointY);

I can not test now, but so you have an idea.

New

I do not know if is this what you mean if not so comment it and delete:

P.s: points* is a Vector2:

pointSpriteA.x = yourSprite.getX() + (yourSprite.getHeight() / 2);
pointSpriteB.x = yourOtherSprite.getY() + (yourOtherSprite.getHeight() / 2);

pointSpriteA.y = yourSprite.getY() + (yourSprite.getWidth() / 2);
pointSpriteB.y = yourOtherSprite.getY() + (yourOtherSprite.getWidth() / 2);

pointC.X = (pointSpriteA.x + pointSpriteB.x) / 2);
pointC.y = (pointSpriteA.Y + pointSpriteB.y) / 2);

the pointC vector is the center of the two vectors.

if that's what you want, and ask yourself, how to rotate? sprites can this helps.

LibGDX - Rotate a 2d array of sprites around their center

查看更多
登录 后发表回答