]]>Man, I make 2D games for fun still using DirectDraw ...like it's 1999!.. I guess that is abstract enough for me. I tried OpenGL CG shaders, though the learning curve was a bit steep with loading the shaders and how they work with vertex buffer and transformations (while I'm more interested in sprites)

]]>I believe you can solve your gluLookAt issue, by thinking of this:

- gluLookAt changes the current OpenGL transformation for rendering.
So an object rendered on a code line after that, will move in relation to the gluLookAt camera.

You have your stationary sprite (stationary is what you mean by static right?)

- You have your stationary Box2D
Think of where in your code you render the sprite, and where you render the Box2D. Are they both shortly after gluLookAt?

From your description, it sounds like one of them is rendered before gluLookAt, and the other one after.

]]>Hmm... OK. But, please tell me how would I go about doing that? I searched a lot on the internet but every implementation of the camera uses gluLookAt(). I'm really confused. Please help. Thanks.

]]>If I understand this correctly, I think the problem is understanding what a camera really is. A camera is just a transformation matrix (aka view matrix) that you must calculate the inverse to and apply to all objects in your world. gluLookAt() is a fixed function method that will set the transformation matrix for the current matrix stack. Typically you call this once, then push the stack, then render all your objects by first pushing their individual matrices on the stack, rendering the object, then popping the stack so those object transformations don't effect future renders.

I don't know how the Box2D rendering sample works (or if you rolled your own), but I would say stop what your doing immediately. Fixed function programming is deprecated. You should be using programmable shaders (minimum OpenGL 2.x API) and performing your transformations on the GPU. I don't know of any sites where you can start, but try opengl-tutorial.org. Alternatively, consider an engine suited for 2D gameplay, such as Torque 2D, which I just now found out is open-source.

]]>Umm... Okay! My code is a little messy. But, I'll try to make it understandable to the best of my efforts. Here goes...

This is the camera class,

PerspectiveView.h:

`#include "Include.h" #define PI 3.14 #define PIdiv180 (PI/180) struct P_3D_Vector { GLfloat x, y, z; }; struct P_2D_Vector { GLfloat x, y; }; P_3D_Vector View_3D_Vector(GLfloat x, GLfloat y, GLfloat z); class Perspective_View { private: P_3D_Vector View_Direction; P_3D_Vector Right_Vector; P_3D_Vector Up_Vector; P_3D_Vector Position; GLfloat Rotated_X, Rotated_Y, Rotated_Z; public: Perspective_View(); void Render_View(void); void Look(P_3D_Vector Direction); void Rotate_X(GLfloat Angle); // To rotate the camera along the X-axis void Rotate_Y(GLfloat Angle); // To rotate the camera along the Y-axis void Rotate_Z(GLfloat Angle); // To rotate the camera along the Z-axis void Look_Z(GLfloat Distance); // To translate the camera along the X-axis void Look_Y(GLfloat Distance); // To translate the camera along the Y-axis void Look_X(GLfloat Distance); // To translate the camera along the Z-axis };`

The Include.h is a single header containing all the headers of all the different libraries.

PerspectiveView.cpp:

`#include "Include.h" #include "PerspectiveView.h" #define SQR(P) (P*P) #define NULL_VECTOR View_3D_Vector(0.0f, 0.0f, 0.0f) P_3D_Vector View_3D_Vector(GLfloat x, GLfloat y, GLfloat z) { P_3D_Vector Temporary; Temporary.x = x; Temporary.y = y; Temporary.z = z; return Temporary; } GLfloat Get_View_3D_Vector_Length(P_3D_Vector *V) { return (GLfloat)(sqrt(SQR(V->x) + SQR(V->y) + SQR(V->z))); } P_3D_Vector Normalize_3D_Vector(P_3D_Vector V) { P_3D_Vector Resultant; float l = Get_View_3D_Vector_Length(&V); if(l == 0.0f) return NULL_VECTOR; Resultant.x = V.x / l; Resultant.y = V.y / l; Resultant.z = V.z / l; return Resultant; } P_3D_Vector operator+ (P_3D_Vector V, P_3D_Vector U) { P_3D_Vector Resultant; Resultant.x = V.x + U.x; Resultant.y = V.y + U.y; Resultant.z = V.z + U.z; return Resultant; } P_3D_Vector operator- (P_3D_Vector V, P_3D_Vector U) { P_3D_Vector Resultant; Resultant.x = V.x - U.x; Resultant.y = V.y - U.y; Resultant.z = V.z - U.z; return Resultant; } P_3D_Vector operator* (P_3D_Vector V, float R) { P_3D_Vector Resultant; Resultant.x = V.x * R; Resultant.y = V.y * R; Resultant.z = V.z * R; return Resultant; } P_3D_Vector Cross_Product (P_3D_Vector *U, P_3D_Vector *V) { P_3D_Vector Resultant_Vector; Resultant_Vector.x = U->y * V->z - U->z * V->y; Resultant_Vector.y = U->z * V->x - U->x * V->z; Resultant_Vector.z = U->x * V->y - U->y * V->x; return Resultant_Vector; } float operator* (P_3D_Vector V, P_3D_Vector U) { return V.x * U.x + V.y * U.y + V.z * U.z; } Perspective_View::Perspective_View() { Position = View_3D_Vector(0.0f, 0.0f, 0.0f); // Initializing the position of camera View_Direction = View_3D_Vector(0.0f, 0.0f, -1.0f); // Initializing which direction to look at Right_Vector = View_3D_Vector(1.0f, 0.0f, 0.0); // Initializing the rightward direction vector Up_Vector = View_3D_Vector(0.0f, 1.0f, 0.0f); // Initializing the upward direction vector Rotated_X = Rotated_Y = Rotated_Z = 0.0f; // Initial Angle } void Perspective_View::Look(P_3D_Vector Direction) // Translating the camera in all dimensions { Position = Position + Direction; // Incrementing position of camera when translated } void Perspective_View::Rotate_X(GLfloat Angle) // Rotating along the X-axis { Rotated_X += Angle; // Incrementing change in X-axis angle View_Direction = Normalize_3D_Vector(View_Direction * (GLfloat)cos(Angle * PIdiv180) + Up_Vector * (GLfloat)sin(Angle * PIdiv180)); // Updating viewing direction Up_Vector = Cross_Product(&View_Direction, &Right_Vector) * -1; // Updating upward direction vector } void Perspective_View::Rotate_Y(GLfloat Angle) // Rotating along the Y-axis { Rotated_Y += Angle; // Incrementing change in Y-axis angle View_Direction = Normalize_3D_Vector(View_Direction * (GLfloat)cos(Angle * PIdiv180) + Right_Vector * (GLfloat)sin(Angle * PIdiv180)); // Updating viewing direction Right_Vector = Cross_Product(&View_Direction, &Up_Vector); // Updating rightward direction vector } void Perspective_View::Rotate_Z(GLfloat Angle) // Rotating along the Z-axis { Rotated_Z += Angle; // Incrementing change in Z-axis angle Right_Vector = Normalize_3D_Vector(Right_Vector * (GLfloat)cos(Angle * PIdiv180) + Up_Vector * (GLfloat)sin(Angle * PIdiv180)); // Updating rightward direction vector Up_Vector = Cross_Product(&View_Direction, &Right_Vector) * -1; // Updating upward direction vector } void Perspective_View::Render_View(void) // Rendering the camera view { P_3D_Vector View_Point = Position + View_Direction; // Incrementing center with change in movement gluLookAt(Position.x, Position.y, Position.z, View_Point.x, View_Point.y, View_Point.z, Up_Vector.x, Up_Vector.y, Up_Vector.z); // gluLookAt() used here to see the game space } void Perspective_View::Look_Z(GLfloat Distance) // Translating the camera along Z-axis { Position = (View_Direction * -Distance); // Updating position } void Perspective_View::Look_X(GLfloat Distance) // Translating the camera along Z-axis { Position = (Right_Vector * Distance); // Updating position } void Perspective_View::Look_Y(GLfloat Distance) // Translating the camera along Z-axis { Position = (Up_Vector * Distance); // Updating position }`

The Main Window,

MainWindow.cpp:

`#include "Include.h" #include "PerspectiveView.h" int main() { Perspective_View Current_View; // Declaring an object for the Perspective_View class b2Vec2 World_Gravity(0.0f, 9.8f); // Defining gravity b2World *Game_World = new b2World(World_Gravity, true); // Creating the Box2D world b2BodyDef Ground_Body_Def; Ground_Body_Def.type = b2_kinematicBody; // Defining type of body Ground_Body_Def.position.Set(0, 768); // Setting initial position b2Body *Ground_Body = Game_World->CreateBody(&Ground_Body_Def); // Creating body b2PolygonShape Ground_Shape; Ground_Shape.SetAsBox(2000, 69); // Defining body shape b2FixtureDef Ground_Fixture_Def; Ground_Fixture_Def.shape = &Ground_Shape; // Defining body fixtures Ground_Body->CreateFixture(&Ground_Fixture_Def); // Binding fixture to body bool No_Key_Pressed = true; bool Facing_Right = true; while(/*window is running*/) { Game_World->Step(1/60, 8, 3); // Iterating the game world in Box2D if(/*Right Key Pressed*/) { No_Key_Pressed = false; Facing_Right = true; } if(/*Left Key Pressed*/) { No_Key_Pressed = false; Facing_Right = false; } if(No_Key_Pressed) { Current_View.Look_X(0.0); // Don't move } else { if(Facing_Right) { Current_View.Look_X(1.0); // Move Right } else { Current_View.Look_X(-1.0); // Move Left } } No_Key_Pressed = true; Current_View.Render_View(); // Here, we are calling the render function containing the gluLookAt() } return EXIT_SUCCESS; }`

This is the Main Window. It is quite messy. I have omitted the OpenGL drawing and rendering methods. They execute perfectly so I did not include them in the code above. However, the OpenGL camera and Box2D is the problematic part, hence I included only those function calls. Thank you for your patience to go through my code. It is quite long, I know. You need not go through the whole Perspective_View class. I just included it to show what it does. The actual problem is in the MainWindow itself. I hope it is clear and understandable help solve the problem. Please help me on this. Thank you.

]]>Can you post some code (Please remember to format it so we can read it clearly)

Using gluLookAt for 2D is a really bad idea. It's designed for a perspective view.

]]>Well, I used gluLookAt() to create a 3D projection matrix but I am only using the X and Y co-ordinates to move the camera in only two dimensions. Is this the reason why my camera is unable to affect the Box2D bodies? Will using glOrtho() solve this problem? Please tell me this. Thank you.

]]>Box2D just returns a set of 2D body positions, unless you are using the debug renderer, rendering is completely up to you.

gluLookAt creates a 3D view matrix, are you projecting the bodies into a 3D space?

I think that is unlikely.

Try using glOrtho instead of gluLookAt

]]>Hi... I am developing a 2D platformer game in C++ using OpenGL as the graphics library and Box2D as the physics engine. So far, I've been able to implement a camera in OpenGL and have designed a Box2D world with all the required physical attributes. I've even been able to draw the Box2D bodies using OpenGL rendering methods. All of this has been successful so far... However, I am having difficulty with using the OpenGL camera in Box2D. I am using the gluLookAt() function to move the camera. I have a static sprite on screen which appears to move away with respect to the camera when the camera moves in the other direction. Similarly, there is also a Box2D body on screen which is stationary (not static; dynamic body but stationary) and in the same space as the sprite. However, when I try to move the camera, the body does not appear to move away, but instead stays where it initially was with the sprite in the background moving away. I am not understanding how to make the body move away with respect to the camera. I am confused. Are the Box2D physics world and OpenGL visual world not in the same space? Why is my OpenGL gluLookAt() camera not affecting the Box2D body? Please help me with this. Thank you.