Skip to content Skip to sidebar Skip to footer

How To Use A Sprite As An Anchor Point Of Another Sprite?

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 pu

Solution 1:

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

Post a Comment for "How To Use A Sprite As An Anchor Point Of Another Sprite?"