Public Group

# Sprite rotation

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

## 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 on other sites
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 on other sites
Sorry about that! I'm programming in C# with DirectX 9.

##### 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 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.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 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);	// Locationm_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!

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×