Jump to content
  • Advertisement
Sign in to follow this  
mikezilla

Sprite rotation

This topic is 4744 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

Does anybody know how to get a sprite to rotate around a spot besides the origin (0, 0, 0)? I created a Matrix, set the TransformZ property with the angle, then called the Sprite.Draw method. However it only rotates around the origin no matter what I enter for the center parameter of Sprite.Draw. Also I found some other posts that mentioned possibly changing the position parameter of Sprite.Draw using the following equation: x' = cos(theta)*x - sin(theta)*y y' = sin(theta)*x + cos(theta)*y is this what I would set for the position parameter for the Sprite.Draw call? Theta I'm assuming is the angle of rotation in degrees...?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Theta I'm assuming is the angle of rotation in degrees...?
If those are c++ functions, then they take the angle in radians.

As for rotating your sprite, I don't know what API you're using so I can't give specific advice. But the usual method for positioning and orienting an object (be it a mesh, sprite, or whatever) is to first rotate it to the desired orientation, and then translate it to the desired position. This is assuming that your sprite is initially centered at the origin. The sequence of operations can also be concatenated into a single transformation matrix.

Share this post


Link to post
Share on other sites
You need to translate the point about which you are going to rotate to the origin, then apply the rotation, and then undo the point translation

Share this post


Link to post
Share on other sites
Ok say I have the following code

Surface s = texture.GetSurfaceLevel(0);
SurfaceDescription desc = s.Description;
Rectangle textureSize = new Rectangle(0, 0, desc.Width, desc.Height);
Vector3 centerVec = new Vector3
((descOverwatch.Width/2), (descOverwatch.Height), 0); // center of sprite
Vector3 positVec = new Vector3(140, 138, 0); // top left corner of sprite

OnPaint
{
...

sprite.Draw
(texture,
textureSize,
centerVec,
positVec,
System.Drawing.Color.White);

}

rotateSprite(float newAngle)
{
float radians = newAngle * (float)(Math.PI/180);
sprite.Transform =
Matrix.RotationZ(radians) *
Matrix.Translation(centerVec);

overwatchSprite.Draw
(texture,
textureSize,
centerVec,
positVec,
System.Drawing.Color.White);
}

For some reason the sprite rotates around the positVec and NOT centerVec...

Any suggestions or help would be very much appreciated!!!

Share this post


Link to post
Share on other sites
This is a clippet from something I've been working on when I get bored of my current project:

m_Sprite.Transform = Matrix.RotationZ(m_Rotation) *	// Rotation
Matrix.Scaling(m_ScaleX, m_ScaleY, 0f) * // Scaling
Matrix.Translation(m_x, m_y, 0f); // Location

m_Sprite.Draw(
m_Surface.m_Texture, // Texture
Rectangle.Empty, // Crop rectangle (Whole texture)
new Vector3(m_Surface.Width / 2, m_Surface.Height / 2, 0), // center
Vector3.Empty, // Offset position
Color.White.ToArgb()); // or System.Drawing.Color.FromArgb(255, 255, 255, 255));


You'll notice that it on the transforming, it does the rotation first, then the scaling and then the matrix translation. With the draw function, it gets the center of the texture for the third parameter. It doesn't use the offset position because it is managed in the Transform property. Enjoy it!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!