0
votes

I have not found answer for this question anywhere, so let's go.

  1. What i expect:

I want to render rocket. Rocket is flying from given start point with evaluated angle. I'm evaluating angle like this:

getVelocity().angle() - 90f

My problem is to calibrate rocket position on top of the rocket. Image below shows how should it work:

plan

In the top picture is how libgdx render not rotated texture region. In the bottom picture is what i expect: I want to move and rotate texture region with given angle to have (x,y) coordinate on the top of rocket.

  1. What i have:

I tired to write method to draw sprite how i expect but i failed. I think it is caused due to fact that i don't understand documentation of this method.

Following manual:

void com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height. The rectangle is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter clockwise rotation of the rectangle around originX, originY.

My code:

public static void drawRotatedTex(SpriteBatch pmRenderer, TextureRegion pmTex, float pmPosX, float pmPosY, float pmAngle)
{
    pmRenderer.begin();     
    pmRenderer.draw(
            pmTex, pmPosX, pmPosY, -pmTex.getRegionWidth()/2, pmTex.getRegionHeight(), pmTex.getRegionWidth(), pmTex.getRegionHeight(), 1f, 1f, pmAngle);
    pmRenderer.end();
}

Results:

enter image description here

It is moment of collision. As we can see coordinates are offset in relation to rocket.

I don't ask about full solution. For me will be sufficient if someone explain me (on drawing or something) like this method works.

EDIT

Moderation suggested that this question is duplicate of:

libgdx: Rotate a texture when drawing it with spritebatch

I read this topic, but it is not my solution. I know how to rotate my sprite by i don't have idea why coordinates of rocket are offset in relation to rocket top.

EDIT

Invocation of my drawRotatedTex from rocket class:

@Override
public void render(Renderer pmRenderer, float pmX, float pmY) {
    SpriteBatch lvSpritebatch = pmRenderer.getSpriteBatch();
    Sprite lvSprite = null;

    if(mIsExploding)
    {
        if((lvSprite = mExplosion.getTexture()) != null)
        {
            lvSpritebatch.begin();
            lvSpritebatch.draw(lvSprite, pmX + lvSprite.getWidth()/2, pmY - lvSprite.getHeight()/2);
            lvSpritebatch.end();
        }
    }
    else
    {
        lvSprite = mAnimation.getTexture();
        RendererTools.drawRotatedTex(lvSpritebatch,lvSprite,pmX,pmY,getVelocity().angle() - 90f);
    }
}
1
I'm not sure what is going on in your results - Do the green lines signify the coordinates of the rocket that exploded? So the fire explosion sprite should be happening where they cross? Is it only the explosion that doesn't match the coordinates - or is the explosion happening at the correct position compared to the rocket - or does the green line coordinate correspond to one of the three rockets I see.DoubleDouble
@DoubleDouble I will explain. On the result picture is moment of collision (rocket vs ground). Black field is gorund (i havent textured it because it is early version of game). Green lines have nothing to do with example, they are only mouse position. Explosion sprite has correct coordinates because they are near ground. Rocket and explosion has shared coordinates (same object - see my edit i will paste code). I ask why rocket sprite position is offseted in relation to own coordinates.Piotr Zych

1 Answers

0
votes

It is not very clear what you are asking, if it's only about moving the origin you would not need that much text. Anyway, I will take a shot at it.

If you want to accomplish what you have in your picture you setup your sprite like this:

sprite.setOrigin(s.getWidth()/2, s.getHeight()); //Centers sprite on top of image.

If I now rotate it in the draw method it rotates around the top center.

sprite.rotate(5);
sprite.draw(batch);

Despite being rotated around the center top of itself it remains position remains the same. If I would set the origin far away from the image and rotate it then the image would travel very far but the position remains the same. Still if you would move it's position 10 pixels to the right the image (wherever it may be) will be moved to the right.

Hope this helps.