Hi 2 all! I have some problems with rectangle rotation. The sense of my test application: there are player in the center of screen, he can rotate around and shoot; bullets have to reflect from walls. All works fine except of walls rendering. The issues related to walls definition:
struct Line
{
XMFLOAT2 point1;
XMFLOAT2 point2;
Line(XMFLOAT2 p1, XMFLOAT2 p2) : point1(p1), point2(p2) { };
};
vector<Line> SidesCoordList;
vector<GameObject> sides;
SidesCoordList.push_back(Line(XMFLOAT2(0.0f, 0.0f), XMFLOAT2(0.0f, 600.0f) ));
SidesCoordList.push_back(Line(XMFLOAT2(0.0f, 600.0f), XMFLOAT2(800.0f, 600.0f) ));
SidesCoordList.push_back(Line(XMFLOAT2(800.0f, 600.0f), XMFLOAT2(800.0f, 0.0f) ));
SidesCoordList.push_back(Line(XMFLOAT2(800.0f, 0.0f), XMFLOAT2(0.0f, 0.0f) ));
...
for (Line& line: SidesCoordList)
{
GameObject temp;
...
temp.SetPosition(center);
temp.SetRotation(XM_PIDIV2);
temp.SetScale(XMFLOAT2( 20.0f, length ));
sides.push_back(temp);
}
...
for (int i = 0; i<instanceCount; i++)
{
if (i >= sides->size()) break;
XMMATRIX mat = sides->at(i).GetWorldMatrix();
instances[i].M1 = mat.m[0];
instances[i].M2 = mat.m[1];
instances[i].M3 = mat.m[2];
instances[i].M4 = mat.m[3];
}
d3dContext_->UpdateSubresource(instanceBuffer, 0, 0, instances, 0, 0);
...
d3dContext_->DrawInstanced(vertexCount, instanceCount, 0, 0 );
Code for matrices from GameObject class:
XMMATRIX GameObject::GetWorldMatrix( )
{
XMMATRIX translation = XMMatrixTranslation( position_.x, position_.y, 0.0f );
XMMATRIX rotationZ = XMMatrixRotationZ( rotation_ );
XMMATRIX scale = XMMatrixScaling( scale_.x, scale_.y, 1.0f );
return rotationZ * scale * translation;
}
void GameObject::SetPosition( XMFLOAT2& position )
{
position_ = position;
}
void GameObject::SetRotation( float rotation )
{
rotation_ = rotation;
}
void GameObject::SetScale( XMFLOAT2& scale )
{
scale_ = scale;
}
HLSL code:
cbuffer cbMatrices : register( b0 )
{
matrix world;
matrix proj;
};
cbuffer cbParameters : register( b1 )
{
bool isTextured;
float4 color;
};
Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );
struct VS_Input
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
float4 W0 : TEXCOORD1;
float4 W1 : TEXCOORD2;
float4 W2 : TEXCOORD3;
float4 W3 : TEXCOORD4;
};
struct PS_Input
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
};
PS_Input VS_Main(VS_Input input)
{
PS_Input vsOut = ( PS_Input )0;
float4x4 world_ = float4x4(input.W0, input.W1, input.W2, input.W3);
float4x4 WVP = mul(world_, proj);
vsOut.position = mul(input.position, WVP);
vsOut.tex = input.tex;
return vsOut;
}
float4 PS_Main( PS_Input frag ) : SV_TARGET
{
if (isTextured)
return colorMap_.Sample( colorSampler_, frag.tex );
else
return float4(color.xyz, 1.0f);
return (float4)(0);
}
Quit strangely that all the objects (player, bullets) are rendered correctly, except of walls. Walls don't want to rotate on Pi/2, another values of angles make distortions similar to incorrect setting of projection matrix. I've used VS Graphics debugger to track values in matrices and it seems correct. On the attached image the first one is matrix of player object, the second one - wall.
I try to understand what I made wrong and debug code during the whole day. Help please