Cropping a line (laser beam) in XNA
- by electroflame
I have a laser sprite that I wish to crop. I want to crop it so when it collides with an item, I can calculate the distance between the starting point, and the ending point, and only draw that. This eliminates the "overdraw" of a laser drawing past an item.
Essentially, I'm trying to crop a line, but also keep that line "attached" to the nose of my ship. The line should not be drawn past the nose of my ship, that should be the starting point. There is no rotation to worry about.
Currently, I thought that doing this through SpriteBatch would be best. This is my current Spritebatch code:
spriteBatch.Draw(Laser.sprite, new Rectangle((int)Laser.position.X, (int)Laser.position.Y,
Laser.sprite.Width, LaserHeight),
new Rectangle(0, 0, (int)(Laser.sprite.Width), LaserHeight),
new Color(255, 255, 255, (byte)MathHelper.Clamp(Laser.Alpha, 0, 255)),
Laser.rotation, new Vector2(Laser.sprite.Width/2, LaserHeight/2), SpriteEffects.None, 0);
But this doesn't quite work. It does only draw part of the sprite, but when LaserHeight is incremented, it lengthens the line in both ways! I believe this is due to some stupid error on my part with the Origin of the draw.
Quick recap: I need to have my laser sprite drawn with the bottom of it at the nose of my ship, and then use LaserHeight to crop the image so only part of it is drawn.
I have a feeling my explanation is a bit...lacking. So if you require more information, please say so and I will try to provide more information.
Thanks in advance!