Drawing a Sprite left,top,right or bellow of another Rotating Sprite

Hey,

in my Programm i create a player, which extends a Sprite, and adds some features to it.
im using Box2d;
i create the sprite like this :

sprite = new Sprite(tex);
sprite.setPosition(X, Y);
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);

so every update call i set the Sprite to the location of its body:

Vector2 position = body.getPosition();
sprite.setPosition(position.x-sprite.getWidth()/2,position.y-sprite.getHeight()/2);
sprite.setRotation( MathUtils.radiansToDegrees*body.getAngle());

after that i simply draw the sprite with :

sprite.draw(batch);

now this works just perfectly.
However, i want a second Sprite, without a physics body to appear left,right,top or bellow of the player Sprite.
This Second sprite is supposed to stay exactly at its location besides the player, in this case left, even if the player does full rotations and fancy stuff. :wink:
Both Sprites have the same Dimensions.
ive drawn how the Second sprite is supposed to behave here : (secondsprite beeing black)

its kinda complicated but i will try to explain what happens.
I manage to get my second sprite to stay left of the playerSprite, or on top, or Right, or below as long as the player is not rotated.
However as soon as i start to rotate, the second sprite doesnt stay at the right place, it rotates in the right angle, but it kinda leaves the playSprites Side. :wink:

this creates the Second Sprite :

SecondSprite = new Sprite(new Texture(Gdx.files.internal("data/second.png")));
SecondSprite.setPosition(X-SecondSprite.getWidth(), Y); 
SecondSprite.setOrigin(SecondSprite.getWidth()/2, SecondSprite.getHeight()/2);
SecondSprite.setRotation(k);

the code that sets the Second Sprites position is :


Vector2 left = new Vector2(body.getPosition().x-sprite.getWidth(),body.getPosition().y);
SecondSprite.setPosition(left.x-sprite.getWidth()/2,right.y-sprite.getHeight()/2);
SecondSprite.setRotation( MathUtils.radiansToDegrees*body.getAngle());

But as mentioned it doesn work the way its supposed to be, im struggling with that problem since 2 days and dont get the Hang of it.
Would be really nice if someone could help me out.

Thanks in advance.

EDIT

best would be if theres a way to access the coordinate System the Sprite is using. If that would be possible it would help me out a lot, even in other situations.
If tryed the “Sprite.Translate” stuff but that gives me strange results.

Trig.

You need to simple do…

prepare for psuedo code.



sprite x = distanceFromOtherSprite * cos(angleOfOtherSprite)
sprite y - distanceFromOtherSprite * sin(angleOfOtherSprite)


Do this in your render loop, it will move the sprite around the central rotation of the other sprite while keeping the same distance.

EDIT: I am typing up an actual block of code…2 minutes…

public class SpriteTest implements Screen {

	Sprite sprite = new Sprite();
	Sprite spriteTwo = new Sprite();

	@Override
	public void render(float delta) {

		/* Set the origin to the center of the sprite */
		sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);

		/* Set the origin of sprite two to the center of sprite */
		spriteTwo.setOrigin(sprite.getX(), sprite.getY());
		/*
		 * Set the position of sprite two to be half the width and height
		 * (starting from center) away from sprite, give the cos/sin functions
		 * the angle and let the magic happen
		 */
		spriteTwo
				.setPosition(
						sprite.getWidth() / 2
								* MathUtils.cosDeg(sprite.getRotation()),
						sprite.getHeight() / 2
								* MathUtils.sinDeg(sprite.getRotation()));

	}

Something like that should work, using Box2D body for position and angle ofc but you get the idea.

Hey,

thanks for your help.
Ive toyed around with your code, but it dont work for me.
used this :

SecondSprite.setOrigin(sprite.getX(), sprite.getY());
        SecondSprite.setPosition(sprite.getWidth() / 2
                * MathUtils.cosDeg(sprite.getRotation()),
          sprite.getHeight() / 2
                * MathUtils.sinDeg(sprite.getRotation()));
        SecondSprite.setRotation( MathUtils.radiansToDegrees*body.getAngle());

i get this :

now if i rotate my first sprite(the middle one) , the second sprite rotates kinda around it, in a realy large radius.
And it also seems that the radius gets larger the more i rotate.
if i leave “SecondSprite.setRotation…” out, the result is nearly the same…
ive also tryed to set the secondsprite:
x to 32 * cos(angleOfOtherSprite)
y to 32 * sin(angleOfOtherSprite)
but there i also get some strange result.

Did you set the first sprites origin to the center? if you did not your second sprite would be rotatating around the bottom left corner of the second sprite, causing it to be all messed up:



/* Set the origin to the center of the sprite */
      sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);


This bit ^^^

yeah i do that in the constructor of the sprite.
but right now i also have it in my render method.
Again ive toyed around a bit, if i move my first sprite near the 0,0 ccordinate, the second sprite gets closer to the first sprite and its rotation radius gets smaller.

EDIT
well i cant belive it.
in my player class ive got a render class.
I call it from my main class, passing in my Spritbatch…
in that render class i draw the sprite with :

sprite.draw(batch);

apparently this gives me strange results. (no idea why) ^^
if i draw it with :

batch.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation);

it seems to work. (im still trying it and havent tryed rotation, but the position seems to be right)

Imo I would not use batch.draw().

It is more readable to have block of code that positions/handles rotations and then use, well I think so anyway. It does make your code more bulky.


sprite.draw(batch);

You just need to set position and angle, right before drawing. In your render loop.

Let me know how it goes.

hey,

sry for the late answer but i was busy these days.
yeah even if i set these directly in the render method it still doesnt work… :-/
but since i got it fully to work with

batch.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation);

i kinda gave up on the whole “sprite.draw(batch);” thing.

but many many thanks for your help :slight_smile: