Jump to content
  • Advertisement
Sign in to follow this  
Fredrik1

really simple question about rotation (managed dx)

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

For fun I made a small app in mdx, this was to rotate a bitmap on the screen. However I cannot seem to get the rotation right, instead of rotating around it's middlepoint where I (belive I) tell it too, it will move to the top left and rotate with it's middlepoint on 0,0. This is the class that contains the bitmap
class SpaceShip
    {
        private Sprite m_sprite;
        private Texture m_texture;
        private Rectangle rect;

        private float angle;

        private Point Center;

        private Point position;

        public SpaceShip(int x, int y, string texture, Device device )
        {
            position = new Point(x, y);

            m_texture = TextureLoader.FromFile(device, texture);

            using (Surface s = m_texture.GetSurfaceLevel(0))
            {
                SurfaceDescription desc = s.Description;
                rect = new Rectangle(position, new Size(desc.Width, desc.Height) );
            }
            
            m_sprite = new Sprite(device);

        }

        public void Draw()
        {
            Center = new Point(position.X + (rect.Width / 2), position.Y + (rect.Height / 2));

            m_sprite.Begin(SpriteFlags.None);
                    
            m_sprite.Draw2D(m_texture, Center, angle, position, Color.White);
            
            m_sprite.End();

            angle += 0.05f;
        }
    }

I render it in a different class, which might be the cause of my problem:
public void DrawData()
        {
            device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
            device.BeginScene();

           // if (Units != null) 
            foreach (SpaceShip sp in Units)
            {
                sp.Draw();
            }

            device.EndScene();
            device.Present() ;
        }

Does anyone know where my problem lie.. I have been looking all over but I cannot figure it out.

Share this post


Link to post
Share on other sites
Advertisement
We need to see the code that actually calls the draw function, and any other code that would effect the world/object/view matrices (unless you're straying away from them). I don't see any information relating to rotation in this code :/

Share this post


Link to post
Share on other sites
That's all the code there is..

The entire grafics class look like this, the form I use just create a new unit and calls the drawunits method at the onpaint event.

class GraficsEngine
{
private int ScreenWidth = 800;
private int ScreenHeight = 600;

Device device;

ArrayList Units = new ArrayList();


public GraficsEngine(GameBoard WindowsHandle)
{
PresentParameters pp = new PresentParameters();
pp.SwapEffect = SwapEffect.Discard;

//Format current = Manager.Adapters[0].CurrentDisplayMode.Format;

pp.Windowed = true;

device = new Device(0, DeviceType.Hardware, WindowsHandle, CreateFlags.SoftwareVertexProcessing, pp);
}

public void LoadUnit()
{
SpaceShip sp = new SpaceShip(40, 40, "SpaceShip.bmp", device);

Units.Add(sp);
}

public void DrawUnits()
{
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
device.BeginScene();

// if (Units != null)
foreach (SpaceShip sp in Units)
{
sp.Draw();
}

device.EndScene();
device.Present() ;
}
}



Each grafical object would have it's representation here, and everything needed to render it should be in the spaceship class.

Here again is the draw method in the spaceship class..

In debugging this function works, the Center becomes 56,56 when I tell it to render a unit at 40,40.. however when going back to the DrawUnits function for presentation this value seems lost, and it then rotates around the 0,0 point.

[If I remove the center calculation, the unit would still revolve around 0,0, however it would be 40px away.]

public void Draw()
{
Center = new Point(position.X + (rect.Width / 2), position.Y + (rect.Height / 2));

m_sprite.Begin(SpriteFlags.None);

m_sprite.Draw2D(m_texture, Center, angle, position, Color.White);

m_sprite.End();

angle += 0.05f;
}


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!