camera not passes through terrain

Started by
4 comments, last by chench 12 years, 7 months ago
I don't want to let the camera passes through terrain.

here my code:



float fHeight = m_pTerrain->GetHeight((int)m_pCamera->GetPosition().x,
(int)m_pCamera->GetPosition().z);

if ( fHeight > m_pCamera->GetPosition().y )
{
m_pCamera->SetPosition(m_pCamera->GetPosition().x, fHeight + 4.0f,
m_pCamera->GetPosition().z);
}




I know it is not work!

1. the function GetHeight(int x, int z), and the paramer x and z is the terrain, not the camera,
but i don't know how to get the terrain's point

2.


m_pCamera->SetPosition(m_pCamera->GetPosition().x, fHeight + 4.0f,
m_pCamera->GetPosition().z);



do not know if there is wrong


Advertisement
It looks reasonable enough, what happening exactly? This bit:

if ( fHeight > m_pCamera->GetPosition().y )

You should change it to:

if ( fHeight + 4.0 > m_pCamera->GetPosition().y )

Since the camera should always be 4 units above the terrain (from what your code does). Otherwise it can get very close to the terrain before finally jumping up 4 units which wouldn't look as good.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.


It looks reasonable enough, what happening exactly? This bit:

if ( fHeight > m_pCamera->GetPosition().y )

You should change it to:

if ( fHeight + 4.0 > m_pCamera->GetPosition().y )

Since the camera should always be 4 units above the terrain (from what your code does). Otherwise it can get very close to the terrain before finally jumping up 4 units which wouldn't look as good.



thanks, it is not jumping up now, but still under the terrain

thanks, it is not jumping up now, but still under the terrain


Can you post the code for the terrain's GetHeight() ?

thanks, it is not jumping up now, but still under the terrain


All I can think of is GetHeight is not working as it should. Simplest thing to try is to reverse the x/z you pass in so its:

float fHeight = m_pTerrain->GetHeight((int)m_pCamera->GetPosition().z, (int)m_pCamera->GetPosition().x);

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.


[quote name='chench' timestamp='1316014861' post='4861594']
thanks, it is not jumping up now, but still under the terrain


Can you post the code for the terrain's GetHeight() ?
[/quote]





float CTerrain::GetHeight(int x, int z)
{
x += (int)( m_position.x + m_vfScale.x*m_cxDIB/2.0f );
z += (int)( m_position.z + m_vfScale.z*m_czDIB/2.0f );

float rx, rz;
rx = x/m_vfScale.x;
rz = m_czDIB - z/m_vfScale.z;

long col1, row1, col2, row2;
float h1, h2, h3, h4, ha, hb;

col1 = (DWORD)rx;
row1 = (DWORD)rz;

if ( col1 > m_cxDIB || col1 < 0 || row1 > m_czDIB || row1 < 0)
{
return 0;
}
else
{

col2 = ( col1 == m_cxDIB )? col1 : col1 + 1;
row2 = ( row1 == m_czDIB )? row1 : row1 + 1;

h1 = ( m_pHeightMap[ row1*m_cxDIB + col1 ].position.y );
h2 = ( m_pHeightMap[ row1*m_cxDIB + col2 ].position.y );
h3 = ( m_pHeightMap[ row2*m_cxDIB + col1 ].position.y );
h4 = ( m_pHeightMap[ row2*m_cxDIB + col2 ].position.y );

ha = ( h2*(rx - col1) + h1*(col2 - rx) );
hb = ( h4*(rx - col1) + h3*(col2 - rx) );

return ( hb*(rz - row1) + ha*(row2 - rz) );
}

}




TerrainVertex* m_pHeightMap



typedef struct TerrainVertex
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
D3DXVECTOR2 texture1;
D3DXVECTOR2 texture2;

}TerrainVertex;




This topic is closed to new replies.

Advertisement