I'm trying to implement a camera that follows a game object as it moves around the world map, i.e. a car driving around a race track.
I think I have the camera set up correctly on to the object, but when the object moves it doesn't appear to be moving around the map. It's as if the map is also locked on to the game object.
I have the world map and initial camera variables set as:
// the world size int worldWidth = 10000; int worldHeight = 10000; // the camera's position int cameraX = 0; int cameraY = 0;
I have initialise and draw functions for the background as:
void InitTrack(Track &testTrack, ALLEGRO_BITMAP *raceTrack) { testTrack.x = 0; testTrack.y = 0; testTrack.width = 10000; testTrack.height = 10000; testTrack.image = raceTrack; } void DrawTrack(Track &testTrack) { al_draw_bitmap(testTrack.image, testTrack.x, testTrack.y, 0); }
I translate the object's world co-ordinates to screen co-ordinates at the top of my event timer:
cameraX = object.x - WIDTH / 2; cameraY = object.y - HEIGHT / 2;
And I call the draw background function just above my draw object function is my game loop.
I would appreciate any suggestions
You're not using cameraX and cameraY anywhere.
Do this:
al_draw_bitmap(testTrack.image, testTrack.x - cameraX, testTrack.y - cameraY, 0);
Remember, really think about what's going on. As you move the camera to the right, cameraX increases. So if you're moving to the right, all the objects move to the left. So, when drawing, you draw them at their position, minus the camera offset.
Sorry for the late reply, spent the last 2 days in hospital world's smallest violin play's in the distant background
Ah right yeh that works. I wrongly assumed that if the background was static then as long as the camera was following the object it would look like the object it travelling across it.
Thanks
Sorry for the late reply, spent the last 2 days in hospital world's smallest violin play's in the distant background
Hope everything went well for you.
Yeh everything is fine. Just caught a throat infection that prevented my free eating or drinking So they had to keep me in and pump me full of fluids and morphine.
Wans't all bad though, had my own ward with Sky Sports, didn't want to leave!