Search code examples
c++directxgame-enginegame-physicsdirect2d

Not being able to rotate object in D2D1


i am new to Direct 2D and a little lost. I am trying to rotate an object but the way i am doing, it is rotating the whole screen of game rather than just one object. I am clueless on how to approach about rotating just specific object. I hope i have provided enough information, but if not please let me know. And i'm sorry if it is stupidly simple question. Any help will be much appreciated.

void SpriteSheet::Draw(bool rotate)
{

ID2D1Effect *chromakeyEffect = NULL;
ID2D1Effect *scaleit = NULL;
ID2D1Effect * rotation = NULL;

D2D1_POINT_2F ptss = { 50, 50 };
D2D1_VECTOR_3F vec{ -5.0f, -500.0f, 100.0f };


D2D1_VECTOR_3F vector{ 0.0f, 1.0f, 0.0f };

// this is the rotating part
if (rotate)
{
    //D2D1::Matrix3x2F::Identity()._11;
    gfx->GetRenderTarget()->SetTransform(
        D2D1::Matrix3x2F::Rotation(20, D2D1::Point2F(100, 100)));
}



gfx->GetDeviceContext()->CreateEffect(CLSID_D2D1ChromaKey, &chromakeyEffect);
gfx->GetDeviceContext()->CreateEffect(CLSID_D2D1Scale, &scaleit);
gfx->GetDeviceContext()->CreateEffect(CLSID_D2D12DAffineTransform, &rotation);

// applying chroma key
chromakeyEffect->SetInput(0, bmp);
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, vector);
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_TOLERANCE, 0.8f);
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_INVERT_ALPHA, false);
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_FEATHER, false);

// scale the object
scaleit->SetInputEffect(0, chromakeyEffect);
//scaleit->SetValue(D2D1_SCALE_PROP_BORDER_MODE, vec);
//scaleit->SetValue(D2D1_SCALE_PROP_CENTER_POINT, D2D1::Vector2F(1.0f, 1.0f));
scaleit->SetValue(D2D1_SCALE_PROP_SCALE, D2D1::Vector2F(0.50f, 0.50f));

// draw it on screen
gfx->GetDeviceContext()->DrawImage(scaleit, ptss);

if (chromakeyEffect) chromakeyEffect->Release();        
}

Solution

  • Just came across Rotation matrix, and that solved the issue. I'm taking the scale effect and applying rotation to it.

    if(rotate)
    {
        rotation->SetInputEffect(0, scaleit);
        D2D1_MATRIX_3X2_F matrix = D2D1::Matrix3x2F::Rotation(98, D2D1::Point2F(50, 40));
    
        rotation->SetValue(D2D1_2DAFFINETRANSFORM_PROP_TRANSFORM_MATRIX, matrix);
    }