• Create Account

# oler117

Member Since 28 Nov 2013
Offline Last Active Feb 03 2014 07:59 AM

### #5113827How to read depth buffer value at a pixel to CPU and get its world coordinate

Posted by on 02 December 2013 - 01:37 PM

Projects a 3D vector from screen space into object space.

```XMVECTOR XMVector3Unproject(
[in]  XMVECTOR V,
[in]  float ViewportX,
[in]  float ViewportY,
[in]  float ViewportWidth,
[in]  float ViewportHeight,
[in]  float ViewportMinZ,
[in]  float ViewportMaxZ,
[in]  XMMATRIX Projection,
[in]  XMMATRIX View,
[in]  XMMATRIX World
);
```

Is this all you need?

### #51133572D rotation problems

Posted by on 30 November 2013 - 04:16 PM

I've handled problem. The problem was in world matrix. The right variant (in my case) is World = scale * rotationZ * translation. And this line of code killed 2 days of my life

unbird, you are right about coefficients calculating and atan2, but it was another bug.

So the correct variant of setting GameObjects to draw lines:

```for (Line& line: SidesCoordList)
{
shared_ptr<GameObject> temp(new GameObject());
float length = sqrt( pow(line.point2.x - line.point1.x, 2) + pow(line.point2.y - line.point1.y, 2) );

float A = line.point1.y - line.point2.y;
float B = line.point1.x - line.point2.x;
float angle = atan2(A, B);

XMFLOAT2 center = XMFLOAT2(  (line.point2.x + line.point1.x) / 2.0f, (line.point2.y + line.point1.y) / 2.0f );

temp->SetPosition(center);
temp->SetRotation(-(XM_PIDIV2 - angle));
temp->SetScale(XMFLOAT2( 5.0f, length ));

sides.push_back(temp);
}
```

And here is function that defines if the object reach the line:

```bool BulletManager::IsIntersecting(Line line, XMFLOAT2 point, float eps)
{
if (!((point.x >= min(line.point1.x, line.point2.x) &&
point.x <= max(line.point1.x, line.point2.x)) ||
(point.y >= min(line.point1.y, line.point2.y) &&
point.y <= max(line.point1.y, line.point2.y))) )
return false;

// Constructing canonical line equation
float A = line.point1.y - line.point2.y;
float B = line.point2.x - line.point1.x;
float C = (-B * line.point2.y) - (A * line.point2.x);

// Calculating distance from bullet to line
float Dist = (abs((A * point.x) + (B * point.y) + C)) / sqrt(pow(A,2) + pow(B,2));

if (Dist <= eps)
return true;

return false;
}
```

Maybe it will be useful for someone. Who knows...

PARTNERS