Archived

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

malyskolacek

problem with movement dependent on camera pos.

Recommended Posts

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

Share this post


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

Share this post


Link to post
Share on other sites