How can I center my screen around a moving object (using C++)?
Actually, the moving object say, vehicle is moving on a route in a map.
How can I recenter the graph around it on the screen ?

Even, if someone could suggest solution for below it would be helpful:
Suppose I have a particle on the screen which moves randomly(or specified by the user) one unit in any direction. Let I have a rectangular box surrounding it. How can I always make the point to be at the center of the box irrespective of where it is moving ?

8 Years
Discussion Span
Last Post by emilo35

First, let's assume the center of your screen has the coordinate (0, 0) for simplicity. Then, you would always want to draw whatever object you are following at the coordinate (0, 0), placing it in the middle of the screen. Then you would want to draw everything else at their respective positions relative to the position of the object you are following.

I usually solve this by creating a camera class, keeping track of where the camera is pointed. Assume you have a grid of height 100 and width 100, and some objects are scattered around its surface. If, let's say, your camera points at (47, 93) you would like to centre that particular point of your grid to (0, 0) (in order for it to appear at the centre of your screen).

Before continuing, let's visualize this with some pseudo:

camera.Xpos -> 47
camera.Ypos -> 93

The camera also happens to look at a car, that is positioned exactly at those coordinates:

car.Xpos -> 47
car.Ypos -> 93

The camera is just an abstract object, and you will never draw it to the screen. The car however, will be drawn, and since your camera is looking directly at it, you will want to draw it at the centre of the screen. How do you do this? Well, let all your visual objects inherit a visualRepresentation class, which contains a method draw, which takes the camera as a parameter ( draw(const Camera & camera) ). The draw method should look something like this:

bool draw(const Camera & camera)
    return (someGrapicsEngine->draw (myPicture, myX - camera.getX(), myY - camera.getY ()));

If all your visual objects are drawn this way, it will appear like it's the camera that's moving around. Now, in order to be able to follow a specific object, I invented the method pointCameraAtMe (Camera & camera) that should be implemented to the visualRepresentation class that, in turn, is inherited by all objects. pointCameraAtMe sets the cameras X- and Y-position pointers to point at the current objects X- and Y-position.

Emil Olofsson

Edited by emilo35: n/a


Thanks emilo35

I have a query, doesn't the method above makes the transition a bit jerked when the car is moving.

Secondly, if I have to zoom in and out of the frame according to the speed of the car. Consider that i was viewing the map and car in zoom mode then suddenly I have to zoom out. How can the transition be made smooth in that case with the car still in center if possible ?


Well, that depends on what you mean with jerked. Since the camera refers directly to the coordinates of the car, the car would allways appear to be exactly at the center of the view. Something you might want to play around with aswell is not making the cameras coordinates point directly at the cars coordinates, but instead setting them as destination coordinates for the camera. Then you could make the camera move towards its destination cordinates. By making it accelerate a bit slower than the car, you would get the effect of a helicopter filming the car, always trying to catch up with its movements.

From what I understand from your questions, your (0, 0) position of your display is not centered, but rather orientated to one of the corners (maybe top-left or bottom left?), aren't they? I think it's easier working with a centered (0, 0) position, so I suggest that you make a function that calculates where the center of your display is (the center position will always be at the center, whether you zoom or not). If your (0, 0) is at the top left, the center of your display would always be at X = visualWidth / 2, Y = visualHeight / 2 . your visual -Width and -Height has to be calculated to some standard width/height (preferably at zoom 0) and then taking your zoom amount into account when determining how much that fit to your screen at your current zoom.

Emil Olofsson

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.