Archived

This topic is now archived and is closed to further replies.

How 2 constaint camera movement to terrain limits?

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

I''m working in VC and DX8 and I''m having a 3d scene where I can move my camera over and around a terrain, the thing is that I wish to keep a constraint on my camera so that I cannot move it out of the boundaries of the terrain. The thing is that I''m not using a heightmap or skybox.

Share this post


Link to post
Share on other sites
Do an Inverse on the camera (view) matrix to get its location in the world. If its outside the boundary, set the position inside the inversed view matrix to be the boundary (the position is at _31, _32, _33, being x,y,z). Then inverse the camera view matrix again and set that as being the camera view matrix.



float maxx, maxy, maxz;
float minx, miny, minz;

maxx = 1000; maxy = 1000; maxz = 1000;
minx = -1000; miny = -1000; minz = -1000;

D3DXMATRIX tempMatrix;
D3DXMATRIX CameraViewMatrix;

Device->GetTransformState(D3DTS_VIEW, &CameraViewMatrix);

D3DXMatrixInverse(&tempMatrix, 0, &CameraViewMatrix);

if(tempMatrix._31 < minx)
{
tempMatrix._31 = minx;
}

if(tempMatrix._31 > maxx)
{
tempMatrix._31 = maxx;
}

if(tempMatrix._32 < miny)
{
tempMatrix._32 = miny;
}

if(tempMatrix._32 > maxy)
{
tempMatrix._32 = maxy;
}

if(tempMatrix._33 < minz)
{
tempMatrix._33 = minz;
}

if(tempMatrix._33 > maxz)
{
tempMatrix._33 = maxz;
}

D3DXMatrixInverse(&CameraViewMatrix, 0, &tempMatrix);

Device->SetTransform(D3DTS_VIEW, &CameraViewMatrix);



Something like that.


[edited by - Joeyblow2 on November 12, 2003 12:32:51 PM]

Share this post


Link to post
Share on other sites