Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created August 7, 2012 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save demonixis/3283596 to your computer and use it in GitHub Desktop.
Save demonixis/3283596 to your computer and use it in GitHub Desktop.
Translation and rotation on a spriteBatch
protected SpriteSortMode _spriteSortMode;
protected BlendState _blendState;
protected SamplerState _samplerState;
protected DepthStencilState _depthStencilState;
protected RasterizerState _rasterizerState;
protected Effect _effect;
protected Matrix _transformMatrix;
protected float _rotation;
protected float _zoom;
public YourConstructor()
{
_spriteSortMode = SpriteSortMode.Immediate;
_blendState = BlendState.AlphaBlend;
_samplerState = SamplerState.LinearClamp;
_depthStencilState = DepthStencilState.None;
_rasterizerState = RasterizerState.CullNone;
_effect = null;
_transformMatrix = Matrix.Identity;
_rotation = 0.0f;
_zoom = 1.0f;
}
public override void Draw(GameTime gameTime)
{
_transformMatrix = GetTransformMatrix();
spriteBatch.Begin(_spriteSortMode, _blendState, _samplerState, _depthStencilState, _rasterizerState, _effect, _transformMatrix);
spriteBatch.Draw(....);
// Et d'autres appels à spriteBatch si tu a besoin
spriteBatch.End()
}
protected Matrix GetTransformMatrix(float rotation, float zoom)
{
Matrix translateToOrigin = Matrix.CreateTranslation(-YnG.Width / 2, -YnG.Height / 2, 0);
Matrix rotation = Matrix.CreateRotationZ(MathHelper.ToRadians(_rotation)); // Rotation
Matrix zoom = Matrix.CreateScale(_zoom); // Zoom
Matrix translateBackToPosition = Matrix.CreateTranslation(YnG.Width / 2, YnG.Height / 2, 0);
Matrix composition = translateToOrigin * rotation * zoom * translateBackToPosition;
return composition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment