Dexteroid CN
Dexteroid CN

Reputation: 81

How to move an object depending on the camera in OpenGL.

As shown in the image below. 3D Ball

The user moves the ball by changing x,y,z coordinates which correspond to right,left, up, down, near, far movements respectively. But when we change the camera from position A to position B things look weird. Right doesn't look right any more, that because the ball still moves in previous coordinate frame shown by previous z in the image. How can I make the ball move in such a away that changing camera doesn't affect they way its displacement looks.

simple example: if we place the camera such that it looking from positive X axis, the change in the values of z coordinate now, will look like right and left movements. However in reality changing z should be near and far always.

Upvotes: 1

Views: 935

Answers (1)

Dexteroid CN
Dexteroid CN

Reputation: 81

Thought i will answer it here:

I solved it by simply multiplying the cam model view matrix to the balls coordinates.

Here is the code:

   glGetDoublev( GL_MODELVIEW_MATRIX, cam );
     matsumX=cam[0]*tx+cam[1]*ty+cam[2]*tz+cam[3];
     matsumY=cam[4]*tx+cam[5]*ty+cam[6]*tz+cam[7];
     matsumZ=cam[8]*tx+cam[9]*ty+cam[10]*tz+cam[11];

where tx,ty,tz are ball's original coordinates and matsumX, matsumY, matsumZ are the new coordinates which change according the camera.

Upvotes: 1

Related Questions