• Advertisement
Sign in to follow this  

camera not passes through terrain

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


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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


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

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

[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;




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement