problem with movement dependent on camera pos.

Started by
2 comments, last by malyskolacek 22 years, 5 months ago
Hi, my problem is I want to move an object(box) for example to the right side of camera view. I don''t want to move it along any axis, but in any direction of the cam. view. I have 2 floats: m_fX and m_fZ, which contain angles of camera rotation. So I used this formula to move the ob- ject: m_object''sX+=sinf(m_fX)*(value determining how much to move the obj.:2,-1,0,3,etc.)*0.1f m_object''sY-=cosf(m_fY)*(as above) It doesn''t work correctly. Can anybody take look at it? Thanks in advance
Advertisement
void CCamera::Render()
{
if (FreeLook) // If the User Controls the View of the Cam
{
glRotatef(Xrot , 1.0 , 0.0 , 0.0 );
glRotatef(Yrot , 0.0 , 1.0 , 0.0 );
glRotatef(Zrot , 0.0 , 0.0 , 1.0 );
glTranslatef( -position.x , -position.y , -position.z );
}
else
{
// Camera Looking at an Object
// The View of the Cam --> CamPosition - Object( that we are looking)
Vector3f view = Get3DVector( &LookAt , &position );
Normalize3DVector( &view ); // Len of the Vector to 1

Vector2f XZ; // 2 Dimensional Vecor & Scaling them to the Len of 1!
GLfloat c;
c = (float)1/sqrt( view.x * view.x + view.z * view.z );
XZ.x = (float)view.x * c;
XZ.y = (float)view.z * c;

// Calculate the Rotation to the Object the Cam is Fixed at!
Xrot = -asin( view.y ) * RADtoDEG;
Yrot = RADtoDEG * atan2( XZ.y , XZ.x ) + 90;
Zrot = 0.0;

glRotatef(Xrot , 1.0 , 0.0 , 0.0 );
glRotatef(Yrot , 0.0 , 1.0 , 0.0 );
glRotatef(Zrot , 0.0 , 0.0 , 1.0 );
glTranslatef( -position.x , -position.y , -position.z );
}

}

I think that was what you needed ...

And if you need it in the Other Direction ... From Angles to a Vector ( for Collision Detection Maybe )

Direction.x = -sin(Yrot*PI_OVER_180) * cos (Xrot * PI_OVER_180);
Direction.y = sin(Xrot*PI_OVER_180);
Direction.z = cos(Yrot*PI_OVER_180) * cos (Xrot * PI_OVER_180);



J@n
J.A.N.K.E.Y.: Journeying Artificial Nocturnal Killing and Exploration Youth
Does anybody have an example in DX8?
I meant object moving with mouse like in 3dsmax4.

This topic is closed to new replies.

Advertisement