#### Archived

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

# Tilt landscape relative to camera

This topic is 5366 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am coding a monkeyball style game. Click here for a video of it in action! I am implementing the control. It is working as can be seen in this video. The problem I have is that the tilt of the land is currently relative to the world coordinate system. It needs to be relative to the direction of the camera. This is my code for tilting the land so far:
// rotate land and water about ball
D3DXMATRIXA16 rottemp1, rottemp2;
D3DXMatrixTranslation(&rottemp2, m_Phs->m_p[0], m_Phs->m_p[1], m_Phs->m_p[2]);
D3DXMatrixRotationYawPitchRoll(&rottemp1, 0.0f, FCtrl->JoyY, FCtrl->JoyX);
D3DXMatrixMultiply(&rottemp2, &rottemp1, &rottemp2);
D3DXMatrixTranslation(&rottemp1, -m_Phs->m_p[0], -m_Phs->m_p[1], -m_Phs->m_p[2]);
D3DXMatrixMultiply(&rottemp2, &rottemp1, &rottemp2);
m_PositionLand = rottemp2;
m_PositionWater = rottemp2;  
That works great if the camera is pointing the correct way on the landscape (as you will have seen in the above video). I have written code to extract the angle of the camera relative to [1 0 0]. This has been tested and works:
// get direction camera is pointing
D3DXVECTOR3 direc = m_cam_lookat - m_cam_position;
direc.y = 0.0f;
D3DXVec3Normalize(&direc, &direc);
// get angle rotated round the y axis
D3DXVECTOR3 forw = D3DXVECTOR3(1.0f,0.0f,0.0f);
FLOAT cosangle = D3DXVec3Dot(&direc, &forw);
// make full 360 degrees
FLOAT angle = acos(cosangle);
if (direc[2] > 0) angle = (D3DX_PI - angle) + D3DX_PI;   
Now how can I change the first bit of code to use this so that the land tilts relative to the direction the camera is looking in? It sounds so simple, but I have been scratching my head for a few hours now and I cant seem to get my head around it. Thanks in advance [edited by - Kieran on March 4, 2004 5:31:18 PM]

##### Share on other sites
a friendly bump, im sure someone here will be able to help.

##### Share on other sites
in the friendly world of relativity, I''d think you''ll be better off keeping the whole universe as it is, and rotate the camera rotate the gravity vector according to the camera up vector, for example.

back on subject, you have the axis of rotation (the direction of the camera), and the angle required, create a rotation matrix with those two.

D3DXMatrixRotationAxis(&Matrix, Camera.Direction, tiltangle);

##### Share on other sites
Thanks for the function idea. I have never seen that one before. I was under the impression that if you want to rotate around an arbitury axis, you have to use quaternions. The code is now:

// get direction camera is pointingD3DXVECTOR3 direc = m_cam_position - m_cam_lookat;direc.y = 0.0f;D3DXVec3Normalize(&direc, &direc);// get forward tilt axisD3DXVECTOR3 ft;D3DXVec3Cross(&ft, &m_cam_updir, &direc);D3DXVec3Normalize(&ft, &ft);// rotate land and water about ballD3DXMATRIXA16 rottemp1, rottemp2;// translateD3DXMatrixTranslation(&rottemp2, m_Phs->m_p[0], m_Phs->m_p[1], m_Phs->m_p[2]);// rotateD3DXMatrixRotationAxis(&rottemp1, &direc, FCtrl->JoyX);D3DXMatrixMultiply(&rottemp2, &rottemp1, &rottemp2);D3DXMatrixRotationAxis(&rottemp1, &ft, FCtrl->JoyY);3DXMatrixMultiply(&rottemp2, &rottemp1, &rottemp2);// translateD3DXMatrixTranslation(&rottemp1, -m_Phs->m_p[0], -m_Phs->m_p[1], -m_Phs->m_p[2]);D3DXMatrixMultiply(&rottemp2, &rottemp1, &rottemp2);// storem_PositionLand = rottemp2;m_PositionWater = rottemp2;

As for the whole relativity argument, I agree that in general it would be better to do it that way, but as my world is very simple, its just as easy to rotate the world (with a slight performace hit I accept). I will try adapting my physics to how it is now. If that doesnt work, I will try your idea.

Thanks again!

[edited by - Kieran on March 7, 2004 12:47:30 PM]

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633311
• Total Posts
3011312
• ### Who's Online (See full list)

There are no registered users currently online

×