Sign in to follow this  

MDX Sprite Rotation

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using the Sprite class to render 2d images and everything is drawn fine untill I try and rotate the sprite. After I call Rotate() it stops drawing the images to the screen. Is there something I'm doing wrong or any better way of doing this or. . .?
public void Draw()
{
sprite.Begin(spriteFlags);
sprite.Draw(texture, new Vector3(x, y, 0), new Vector3(0, 0, 0), 16777215);
sprite.End();
}





public void Rotate(float _theta)
{
sprite.Transform = Matrix.RotationZ(_theta) * Matrix.Scaling(scalex, scaley, scalez) * Matrix.Translation(x, y, z);
//scalex = 1
//scaley = 1
//scalez = 0
//x = 0
//y = 0
//z = 0
//It still draws when _theta = 0, but not in any other case.
}





Share this post


Link to post
Share on other sites
It may be ther parameters you're passing into the draw function. I use it like this:
        private Vector3 location = Vector3.Empty;
private Vector3 pivot = Vector3.Empty;
private Vector3 offset = Vector3.Empty;
private Vector3 scale = new Vector3(1, 1, 1);

private float rotation = 0.0f;

private Matrix localTransform = Matrix.Identity;

public void Draw(Sprite drawSurface)
{
drawSurface.Transform = localTransform;
drawSurface.Draw(texture, sourceRectangle, pivot, offset, tintColor);
}

private void CalculateLocalTransform()
{
this.localTransform = Matrix.RotationZ(rotation) *
Matrix.Scaling(scale) *
Matrix.Translation(new Vector3((float)System.Math.Round(this.location.X), (float)System.Math.Round(this.location.Y), 0));
}






The pivot point is a hotspot relative to the top-left corner of the sprite's area. Offset is distance from the origin. If you're transforming with Matrix.Translation, you won't need to pass a transformation vector into the draw function, or you'll be drawing it twice.

Also, make sure your theta value is in radians.

Be mindful that the rotate function rotates around the origin, that's what you would use the offset (or "pivot") parameter for. It will rotate around that point.

Share this post


Link to post
Share on other sites

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this