I am making a game in which a cube travels along a maze with the motive of crossing the maze safely. I have two problems in this.

  1. The cube needs to have a smooth movement like it is traveling on a frictionless surface. So could someone help me achieve this. I need to have this done in a event callback function
  2. I need to move the camera along with the cube. So could someone advice me a good tutorial about camera positions along with an object?

Here are some simple steps you can take just to get something up & running quickly, and not to be too ambitious:

(1) For a frictionless effect, it sounds like your cube just needs to maintain a velocity vector (vx,vy,vz). Whenever the user presses a directional key, add or subtract some small fixed amount to this velocity vector.

This way, on each frame, whether or not the user is doing anything, you will move the cube by (velocity * timeElapsed), or even just by (velocity * someConstant). The cube will continue sliding along its current trajectory as though frictionless -- until it collides with a maze wall, that is... but that's collision detection, a whole other topic.

(2) I'm guessing you have the cube's position represented somewhere in your code. Well, on each frame take that position and add some fixed distance from it -- this will give you your camera's position for that frame. I suggest to you, initially, just to make your camera look down at the cube (and maze) from some fixed height. For example:

cameraPosition = cubePosition + VECTOR(0.0, CAMERA_HEIGHT, 0.0);

assuming the +y direction represents "up" in your world... otherwise, add CAMERA_HEIGHT to the z-coordinate or subtract it from y, or whatever works for you.

Then if you're doing old-skool OpenGL and not shader language:

        cameraPosition.x, cameraPosition.y, cameraPosition.z,
        cubePosition.x, cubePosition.y, cubePosition.z,
        0.0, 1.0, 0.0);  // <-- consult the gluLookAt() documentation

    // ... all your other glRotate()/glTranslate() and rendering
    // code for each frame follows here

Because the camera's position is cubePosition plus [some fixed distance], the camera will "follow" the cube around wherever it moves. In fact, you may find that it follows the cube too rigidly and will want to make the camera more loosely tethered -- but initially, keep it simple and just get something working.

In fact, all of this is very rigid and can be played with to get dynamic camera motion, dynamic angles, "tracks" and paths the camera could follow smoothly -- but all of that gets very complicated very quickly. Again, I implore you: initially, just to get things working, keep it simple and just have your camera perched a fixed distance above your cube, looking down onto the cube & maze. Once you get that much in place, feel free to play with it and design something more elaborate. (Just make sure you really sit down and design it, and not just cobble things together, otherwise you'll drown yourself in code clutter.)

| improve this answer | |
  1. For achieving this effect you only have to update the position of the cube every time you press one of the movement keys. Here is everything you need to know about it.

  2. You have to understand that after setting the identity to the modelview, every vertex that is multiplied by that will be in the eye/camera position. Adding a -Z translate will allow you to see the object because the camera by default is looking towards -Z. All its left to do is updating the model position to the camera position and then do the -Z translate. This would be done pretty much like in 1. I'd recommend to understand some basic concepts before implementing anything, here's a good place to start.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.