1
\$\begingroup\$

I've been trying to draw an outline around a sprite using the code from this answer.

Details

My sprite is called Ship2Sprite.

This is how I create the rectangle:

 rectangle = New Rectangle(backgroundPos.X + NPCAI.enemyPosistion.X,
                           backgroundPos.Y + NPCAI.enemyPosistion.Y,
                           Ship2Sprite.Width, Ship2Sprite.Height)

This is how it looks without any rectangle drawn:

without rectangle

If I use this line of code

hudSpriteBatch.Draw(t, rectangle, New Rectangle(0, 0, 0, 0), Color.Red,
                    NPCAI.enemyAngle, New Vector2(0, 0), SpriteEffects.None, 0)

the following is the result in game

with rectangle

hudSpriteBatch.Draw(t, New Rectangle(rectangle.Left, rectangle.Top, 2, rectangle.Height), New Rectangle(0, 0, 0, 0), Color.White, NPCAI.enemyAngle, New Vector2(0, 0), SpriteEffects.None, 0) 
hudSpriteBatch.Draw(t, New Rectangle(rectangle.Right, rectangle.Top, 2, rectangle.Width), New Rectangle(0, 0, 0, 0), Color.White, NPCAI.enemyAngle, New Vector2(0, 0), SpriteEffects.None, 0) 
hudSpriteBatch.Draw(t, New Rectangle(rectangle.Left, rectangle.Top, rectangle.Width, 2), New Rectangle(0, 0, 0, 0), Color.White, NPCAI.enemyAngle, New Vector2(0, 0), SpriteEffects.None, 0)
hudSpriteBatch.Draw(t, New Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, 2), New Rectangle(0, 0, 0, 0), Color.White, NPCAI.enemyAngle, New Vector2(0, 0), SpriteEffects.None, 0)

However, when trying to use the code from the linked answer, this is the result:

line-rectangles

The first and third lines produce the correct lines, however the other 2 are in the correct orientation but not the correct position

Am I missing something somewhere?

EDIT

Based on Blau's answer, I have updated my code to this:

 Public Shared Sub DrawOutline(ByVal t As Texture2D, ByVal sprite As Texture2D, ByVal pos As Vector2, ByVal graphics As GraphicsDevice)


    Dim spriteBatch1 As New SpriteBatch(graphics)
    Dim rectangle As New Rectangle(Game.backgroundPos.X + pos.X, Game.backgroundPos.Y + pos.Y, sprite.Width, sprite.Height)
    Dim w = rectangle.Width
    Dim h = rectangle.Height
    Dim x = rectangle.X + w / 2
    Dim y = rectangle.Y + h / 2
    Dim transform As Matrix = Matrix.CreateRotationZ(NPCAI.enemyAngle) * Matrix.CreateTranslation(x, y, 0)

    spriteBatch1.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, Nothing, Nothing, Nothing, Nothing, transform)
    x = -w / 2
    y = -h / 2

    spriteBatch1.Draw(t, New Rectangle(x, y, w, 2), Nothing, Color.White)
    spriteBatch1.Draw(t, New Rectangle(x, y + h - 2, w, 2), Nothing, Color.White)
    spriteBatch1.Draw(t, New Rectangle(x, y, 2, h), Nothing, Color.White)
    spriteBatch1.Draw(t, New Rectangle(x + w - 2, y, 2, h), Nothing, Color.White)
    spriteBatch1.End()

End Sub

However the rectangle is rendering in the wrong position, which is slightly ahead of the sprite. Like this

EDIT 2, with the following code

 Public Shared Sub DrawOutline(ByVal t As Texture2D, ByVal sprite As Texture2D, ByVal pos As Vector2, ByVal graphics As GraphicsDevice, ByVal angle As Single)


    Dim spriteBatch1 = New SpriteBatch(graphics)
    Dim rectangle As New Rectangle(Game.backgroundPos.X + pos.X, Game.backgroundPos.Y + pos.Y, sprite.Width, sprite.Height)
    Dim w = rectangle.Width
    Dim h = rectangle.Height
    Dim x = rectangle.X + w / 128
    Dim y = rectangle.Y + h / 128



    Dim transform As Matrix = Matrix.CreateRotationZ(angle) * Matrix.CreateTranslation(x, y, 0)

    spriteBatch1.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, Nothing, Nothing, Nothing, Nothing, transform)
    x = -w / 128
    y = -h / 128
    ' spriteBatch1.Draw(t, rectangle, Color.Red)
    spriteBatch1.Draw(t, New Rectangle(x, y, w, 2), Nothing, Color.White)
    spriteBatch1.Draw(t, New Rectangle(x, y + h - 2, w, 2), Nothing, Color.White)
    spriteBatch1.Draw(t, New Rectangle(x, y, 2, h), Nothing, Color.White)
    spriteBatch1.Draw(t, New Rectangle(x + w - 2, y, 2, h), Nothing, Color.White)
    spriteBatch1.End()

End Sub
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I mischievously pinged the author of that code you linked, since that's where the problem seems to lie. :3 \$\endgroup\$
    – Anko
    Sep 2 '14 at 23:29
2
\$\begingroup\$

Pixel color addresing is bad, it will work on a square but not on a rectangle.

Change this:

 colors[ x * texture.Width + y ] = borderColor;
 colors[ x * texture.Height + y ] = Color.Transparent;

To this:

 colors[ x + texture.Width * y ] = borderColor;
 colors[ x + texture.Width * y ] = Color.Transparent;

EDIT: If you are using the other method, your problem is the rotation

You have to use a transform matrix and generate the rectangle based in (0,0) as origin.

var w = rectangle.Width;
var h = rectangle.Height;
var x = rectangle.X + w/2;
var y = rectangle.Y + h/2;
Matrix transform = Matrix.CreateRotationZ(NPCAI.enemyAngle) * Matrix.CreateTranslation(x,y,0);

husSpriteBatch.Begin(,.,..., transform);
x = -w/2;
y = -h/2;
hudSpriteBatch.Draw(t, new Rectangle(x, y, w, 2), null, Color.White);
hudSpriteBatch.Draw(t, new Rectangle(x, y+h-2, w, 2), null, Color.White);
hudSpriteBatch.Draw(t, new Rectangle(x, y, 2, h), null, Color.White);
hudSpriteBatch.Draw(t, new Rectangle(x+w-2, y, 2, h), null, Color.White);

husSpriteBatch.end();
\$\endgroup\$
4
  • \$\begingroup\$ Sorry I have updated the link to the correct answer because I got better results with the answer, I tried this way and cannot call the method from initialization because the texture isn't loaded yet, and when I call it into draw I get an invalid operation exception. \$\endgroup\$
    – user49155
    Sep 4 '14 at 15:23
  • \$\begingroup\$ if your intialization method has this code square = new Texture2D( GraphicsDevice, 100, 100 ); square.CreateBorder( 5, Color.Red ); It has to run without problem \$\endgroup\$
    – Blau
    Sep 4 '14 at 22:09
  • \$\begingroup\$ I tried the updated code in your answer, and it produces this, the rotation and rendering is fine, however it seems to be offset infront of my sprite for some reason, do you have any idea why this might be happening? I'm sure the position I'm passing to it is correct. Will update OP with details, Thanks \$\endgroup\$
    – user49155
    Sep 5 '14 at 17:50
  • \$\begingroup\$ I though you was rotating your ship about its center, better than divide by 128, change w/128 and h/128 by 0 \$\endgroup\$
    – Blau
    Sep 6 '14 at 7:35

You must log in to answer this question.