XNA stretching sprite
I'd like to stretch a sprite based on the movemnt/velocity direction. for simplicity sake the sprite is white 5x5 pixels. incl开发者_运维技巧uding a black 1x1 border
So for example if the sprite moves from current location towards '3oclock' that it would stretch/scale like thin line diagonally, similar for any other angle/radian based on random movement.
I tried taking the: vector2 position - original position, and then using this with: length() for either x and y scale values but this produces large scale blocks and not a line?
Any ideas? on scaling and calculating the angle of direction
Paul.
This was some time ago, so may not be much use to the original person who made the request. But you have three options. One is to use Draw(Texture2D, Rectangle, ... )
rather than Draw(Texture2D, Vector2 ...)
. The Vector2
just sets the x
and y
coordinates, so if you resize using that method you will resize the sprite as a whole. The Rectangle contains X
, Y
& separate Height
and Width
values, so using the Rectangle
you can resize along one axis. You will still get a problem with the ends, but that can be fixed by treating the ends as separate sprites. I use this method for variable sized borders.
The second, which will work in the case of a simple sprite as you describe, is to create the texture at runtime. Create a new Texture2D
of the desired size, then create an array of colors big enough to cover each pixel, and assign that array using <Texture2D>.SetData(Color[])
. There is a fair bit of computational overhead with this, but I've used it to highlight a variable sized bounding box at runtime, so it works ok.
Of course, the primitives option looks good too. :)
I think you'll have to create multiple sprites to display the line. You'll need transitional sprites for the inner sections of the line and sprites for the end points.
Please state your question clearly. Could you upload any screenshots? From what I understand, though, you should scale the sprite in X direction using the distance formula and then Rotate it using the tan inverse function.
精彩评论