Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
    
See if this answer helps. –  David Gouveia May 3 '12 at 6:00
add comment

2 Answers 2

The reason it's expanding in both directions is because you're setting the origin as the centre point of the texture (new Vector2(Laser.sprite.Width/2, LaserHeight/2)). When you increase or decrease the size of the texture being drawn, it's still drawn in the same spot, it's just smaller in general.

If you want to shorten it based on the destination (or source) rectangle, you should set the origin to the bottom-centre (new Vector2(Laser.sprite.Width/2, LaserHeight)). Then, the position you draw it at will just be the front of your ship.

Let me know if this is not clear and I'll make a diagram.

share|improve this answer
add comment
up vote 3 down vote accepted

Actually, I had a brainwave and it worked out. I used the size (specifically, a Vector2) to scale it instead of crop it.

Here's the final code:

    spriteBatch.Draw(Laser.sprite, Laser.position + new Vector2(0, Laser.sprite.Height / 2),
null,
new Color(255, 255, 255, (byte)MathHelper.Clamp(Laser.Alpha, 0, 255)),
Laser.rotation,
new Vector2(Laser.center.X, Laser.sprite.Height),
new Vector2(Laser.StretchableSize.X, ChangeRange(LaserHeightMAX, 1, LaserHeight)),
SpriteEffects.None,
0);

ChangeRange is simply a function that converts a range into 0 to 1 for the size.

However, if there is another way, I'm still open to hearing it!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.