# Why am I getting rendered rectangles that are off relative to their bounding rectangles in this code?

I am rendering different rectangles with the help of the DirectXTK (PrimitiveBatch) and surrounding them with a BoundingOrientedBox (DirectXCollision.h). For visualization I draw the corners of the BoundingBox to make sure that everything works fine. And it is!

Except when I change the y position of the rectangle.

The strange thing is that my BoundingBox corners are below the actual rectangle, but the center of the rectangle and the center of the bounding box are both at the same and right position, only my rendered rectangle is mispositioned.

Here how I set up the BoundingBox:

this->boundingBox = new BoundingOrientedBox(XMFLOAT3(XMVectorGetX(this->position), XMVectorGetY(this->position), XMVectorGetZ(this->position)), XMFLOAT3(width/2.0f, height/2.0f, depth/2.0f), XMFLOAT4(XMVectorGetX(this->orientationQuaternion), XMVectorGetY(this->orientationQuaternion), XMVectorGetZ(this->orientationQuaternion), XMVectorGetW(this->orientationQuaternion)));


And this is how I set-up my vertices for the rectangle:

float x = XMVectorGetX(this->position) - getWidth()/2;
float y = XMVectorGetY(this->position) - getHeight()/2;
float z = XMVectorGetZ(this->position) - getDepth()/2;

// FRONT
this->vertexPosition[0] = VertexPositionColor(XMVectorSet(x, y + getHeight(), z, 1), Colors::Blue);
this->vertexPosition[1] = VertexPositionColor(XMVectorSet(x + getWidth(), y + getHeight(), z, 1), Colors::Blue);
this->vertexPosition[2] = VertexPositionColor(XMVectorSet(x, y, z, 1), Colors::Blue);
this->vertexPosition[3] = VertexPositionColor(XMVectorSet(x + getWidth(), y, z, 1), Colors::Blue);
// RIGHT
this->vertexPosition[4] = VertexPositionColor(XMVectorSet(x + getWidth(), y + getHeight(), z, 1), Colors::Blue);
this->vertexPosition[5] = VertexPositionColor(XMVectorSet(x + getWidth(), y + getHeight(), z + getDepth(), 1), Colors::Blue);
this->vertexPosition[6] = VertexPositionColor(XMVectorSet(x + getWidth(), y, z, 1), Colors::Blue);
this->vertexPosition[7] = VertexPositionColor(XMVectorSet(x + getWidth(), y, z + getDepth(), 1), Colors::Blue);
// TOP
this->vertexPosition[8] = VertexPositionColor(XMVectorSet(x, y + getHeight(), z + getDepth(), 1), Colors::Blue);
this->vertexPosition[9] = VertexPositionColor(XMVectorSet(x + getWidth(), y + getHeight(), z + getDepth(), 1), Colors::Blue);
this->vertexPosition[10] = VertexPositionColor(XMVectorSet(x, y + getHeight(), z, 1), Colors::Blue);
this->vertexPosition[11] = VertexPositionColor(XMVectorSet(x + getWidth(), y + getHeight(), z, 1), Colors::Blue);
// BACK
this->vertexPosition[12] = VertexPositionColor(XMVectorSet(x + getWidth(), y + getHeight(), z + getDepth(), 1), Colors::Blue);
this->vertexPosition[13] = VertexPositionColor(XMVectorSet(x, y + getHeight(), z + getDepth(), 1), Colors::Blue);
this->vertexPosition[14] = VertexPositionColor(XMVectorSet(x + getWidth(), y, z + getDepth(), 1), Colors::Blue);
this->vertexPosition[15] = VertexPositionColor(XMVectorSet(x, y, z + getDepth(), 1), Colors::Blue);
// LEFT
this->vertexPosition[16] = VertexPositionColor(XMVectorSet(x, y + getHeight(), z + getDepth(), 1), Colors::Blue);
this->vertexPosition[17] = VertexPositionColor(XMVectorSet(x, y + getHeight(), z, 1), Colors::Blue);
this->vertexPosition[18] = VertexPositionColor(XMVectorSet(x, y, z + getDepth(), 1), Colors::Blue);
this->vertexPosition[19] = VertexPositionColor(XMVectorSet(x, y, z, 1), Colors::Blue);
// BOTTOM
this->vertexPosition[20] = VertexPositionColor(XMVectorSet(x, y, z, 1), Colors::Blue);
this->vertexPosition[21] = VertexPositionColor(XMVectorSet(x + getWidth(), y, z, 1), Colors::Blue);
this->vertexPosition[22] = VertexPositionColor(XMVectorSet(x, y, z + getDepth(), 1), Colors::Blue);
this->vertexPosition[23] = VertexPositionColor(XMVectorSet(x + getWidth(), y, z + getDepth(), 1), Colors::Blue);


This is how I calculate and apply the world transforms:

For the corner points:

    XMMATRIX scale    = XMMatrixScaling(0.01f, 0.01f, 0.01f);
XMMATRIX trans    = XMMatrixTranslation(XMVectorGetX(cornerPoint), XMVectorGetY(cornerPoint), XMVectorGetZ(cornerPoint));
g_pEffectPositionNormal->SetWorld(scale * trans);

g_pBall->Draw(g_pEffectPositionNormal, g_pInputLayoutPositionNormal);


The cornerPoint is a vector with the position for one of the corners of the bounding box.

And my rectangle:

g_pEffectPositionNormal->SetWorld(body->getOrientation() * XMMatrixTranslation(XMVectorGetX(body->getPos()), XMVectorGetY(body->getPos()), XMVectorGetZ(body->getPos())));

g_pEffectPositionNormal->Apply(pd3dImmediateContext);
pd3dImmediateContext->IASetInputLayout(g_pInputLayoutPositionNormal);

g_pPrimitiveBatchPositionColor->Begin();
g_pPrimitiveBatchPositionColor->Draw(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, body->getVertexPosition(), 24);
g_pPrimitiveBatchPositionColor->End();


Where getPos() returns the center point position of my rectangle. Can you see something obvious wrong about how I do it?

• That the most descriptive title you have? People experiencing the same problem should be able to find this question in the future. – MichaelHouse Dec 5 '13 at 22:28
• @Byte56 I also think the poor title is also the reason the question has few views. – concept3d Dec 6 '13 at 0:53
• I am not quite sure how to summarize this problem. I have tried to give it a more desriptive title. Any suggestions? – puelo Dec 6 '13 at 11:52
• I have added the calculations for the world matrices.. I think something might be wrong there – puelo Dec 6 '13 at 13:29

I solved it. The problem was that I was not defining the vertices in object space.

Changed:

float x = XMVectorGetX(this->position) - getWidth()/2;
float y = XMVectorGetY(this->position) - getHeight()/2;
float z = XMVectorGetZ(this->position) - getDepth()/2;


To:

float x = - getWidth()/2;
float y = - getHeight()/2;
float z = - getDepth()/2;

• So, for the benefit of other users who may try to search for this kind of question later, can you edit your answer to detail exactly what that change to your code looked like? – user1430 Dec 7 '13 at 20:26
• You are right. Done – puelo Dec 7 '13 at 20:27
• Awesome. The next thing you should do is mark this answer as "accepted" using the green check box (you will need to wait a little before the system will allow you to do this, though). – user1430 Dec 7 '13 at 20:29
• Yep. I can not accept at the moment. Will do it as soon as it is possible. – puelo Dec 7 '13 at 20:30