Jump to content
  • Advertisement
Sign in to follow this  
jperezalv

Rotating sprite? (using c# Sprite class)

This topic is 4733 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 use some sprites in my managed directx application, which are made from the Sprite class. The problem is that, although the Sprite.Draw2D method has an option to specify the rotation center of the sprite, and the angle you want, it always rotates around the (0,0) coordinates of my window. Is there any way to rotate the sprite around the point I want using this class, or should I use a textured polygon and rotate it manually? Thanks Jorge

Share this post


Link to post
Share on other sites
Advertisement
OK, here is some code, but is basically the one you can find in almost every example of sprites use.

First, I load the texture, get the texture size and then create the sprite object:

texture=TextureLoader.FromFile(device,texturefile);
using (Surface s= texture.GetSurfaceLevel(0))
{
SurfaceDescription desc= s.Description;
texturesize=new Rectangle(0,0,desc.Width,desc.Height);
}
sprite=new Sprite(device);

Then, every time I need to show the sprite, I execute:

sprite.Begin(SpriteFlags.AlphaBlend);
sprite.Draw2D(texture,rotationCenter,rotationAngle,position,Color.White);
sprite.End();

In my case, rotationCenter==(40,25) and position==(0,500)

As I understand, the rotationCenter is relative to the texture, so it would be 40 pixels to the right and 25 pixels to the bottom (of the texture).

The sprite is a speed meter needle which I want to rotate so it indicates the various speeds the car can get to. But instead of rotating as it would be the normal way, it moves rotating around the top-left corner of my window.

Any suggestion?
Jorge

Share this post


Link to post
Share on other sites
Check the desc.Width and desc.Height parameters. I suspect that your texture is being resized to a power of 2. If (40,25) represents the center of your texture, then the texture is (80,50). This would get resized to (128,64) by the texture loader. Thus, when you specify a rotation center of (40,25), you are going to get strange-looking results.

I could be totally wrong, but it's worth looking into.

Share this post


Link to post
Share on other sites
Doing a quick search, it looks like the Draw2D function doesn't exist in the c++ DX SDK. I think it must be new, or something.

Anyhow, have you tried using the SetTransform and using a matrix to rotate the sprite? It should be a bit more complicated, but might actually work (that's how you'd do it in c++).

Hope this helps :).

Share this post


Link to post
Share on other sites
Quote:
Original post by sirob
Doing a quick search, it looks like the Draw2D function doesn't exist in the c++ DX SDK. I think it must be new, or something.

Anyhow, have you tried using the SetTransform and using a matrix to rotate the sprite? It should be a bit more complicated, but might actually work (that's how you'd do it in c++).

Hope this helps :).


He's using C#.

Share this post


Link to post
Share on other sites
Yeah, I know. I was saying that maybe since this was added to the managed SDK, it might not be too stable. I suggested the Matrix way since it clearly works in the unmanaged SDK. :)

Share this post


Link to post
Share on other sites
Well, the Draw2D method has existed at least since the Dec 2004 update and I haven't read about anyone having similar problems. So, while it's possible that this is more "managed badness," I don't think that's the case here.

Share this post


Link to post
Share on other sites
First of all, thanks for your comments.

The texture is 64x64 because I had already noticed that if it isn't, it gets resized. I have changed the sprite coordinates and make a lot of tests with different values of position, rotation center, angle, etc., without success. It always spins around the point (0,0) of my window. Anyway, I was already thinking of doing it as sirob said, with a transformation matrix and all that. I just was wondering if there was a way to do it with the Draw2D method.

Don't worry more about it.

Thanks again to both of you.
Jorge

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!