0
\$\begingroup\$

My goal is to find the function to stretch a sprite like my example below, and then get it to animate during a certain time. It is like an interpolation, where the starting point is the original image, and the final point is the stretched image. Any idea?

Here is an example, hope it is clear.

enter image description here

\$\endgroup\$
  • 4
    \$\begingroup\$ Your question is a combination of a few simple techniques. Scaling a sprite and changing a value over time. You may find it useful in the future to break apart your problem into smaller problems, that are more generic and easily searchable. \$\endgroup\$ – MichaelHouse Aug 29 '13 at 18:42
  • \$\begingroup\$ Thanks a lot. I found very useful your links and your suggestion. \$\endgroup\$ – Francesco Bonizzi Aug 29 '13 at 19:16
2
\$\begingroup\$

The SpriteBatch class's Draw method has an overload that allows you to specify a scale factor. Thus, to render a sprite in a stretched frame, do something like:

// Scale the sprite 50% along X (like in your example).
var scale = new Vector2(0.5f, 1.0f);

// Draw the sprite with that scale. 
spriteBatch.Draw(
  texture,
  position,
  null,
  Color.White,
  0.0f,
  Vector2.Zero, 
  scale,
  SpriteEffects.None,
  0
);

To achieve an animation of this effect, store your scale factor somewhere (a member in your Game class would be simple to start with), and update it over time in your game's Update method. The Update method gives you a GameTime object containing the elapsed time since the last update. Every update, adjust your scaling factor by the amount you want to scale per second using the TotalSeconds member of the elapsed time span object.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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