Cropping a line (laser beam) in XNA

Posted by electroflame on Game Development See other posts from Game Development or by electroflame
Published on 2012-05-03T03:57:22Z Indexed on 2012/06/02 10:51 UTC
Read the original article Hit count: 367

Filed under:
|
|
|

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!

© Game Development or respective owner

Related posts about XNA

Related posts about 2d