Sign in to follow this  

Position of camera and objects

This topic is 4340 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 have a camera class and an class for every object in my game (lines, rectangles, sprites, layers, etc); how should i have my render function so it first positions the camera then draws the objects in their correct possible? my first idea was doing something like:
render()
{
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(Camera.X, Camera.Y, 0) // this is a 2d app.

   glPushMatrix();
   for (long i=0 ; i<LineList.Total() ; i++)
      LineList.Item(i)->Draw();
   glPopMatrix();

   glPushMatrix();
   for (long i=0 ; i<RectangleList.Total() ; i++)
      RectangleList.Item(i)->Draw();
   glPopMatrix();

   ...
}


Within each Draw() function, the position of the object is placed using the translate as well, is this correct? Using the camera's position in relation to where the object should be placed?

Share this post


Link to post
Share on other sites
Because there is no camera in OpenGL you simulate one by moving the world in the opposite direction of the camera movement so that's why you have to move the world the other way than the camera movement.

so if the camera goes to the left the world actually goes to the right

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
the negative?? really? y's that?
Well, you could easily get it work with the positive translation vector by fudging your camera control, but that's not really the 'correct' way to do it. The reasons why it should be negative have to do with with coordinate systems and how they relate to each other. It's actually a little hard to explain succinctly, but it's explained pretty well in the Red Book (which is available online).

I'll try to give a simple example. Let's say your camera position (in 3D) is (0,0,5) and is looking down the +z axis. An object at (0,0,15) in world space should be transformed to (0,0,10) in view space; the translation that accomplishes that is (0,0,-5), i.e. the negative of the camera position. In other words, you need to move the world by the negative of the camera position in order for the camera to be at the origin in view space.

Share this post


Link to post
Share on other sites
hmm... i think i get that.. so if the camera is at 100, 100, 0 looking at an object that's 200, 150, -1; i would move the camera -100, -100 so when the object is moved 200, 150, it will be in the camera at 100, 50, -1 is this right?

edit: and knowing that, i would need to rotate the camera in the negative direction as well then right?

Share this post


Link to post
Share on other sites

This topic is 4340 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this