Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Object Camera and World Map Problem

This thread is locked; no one can reply to it. rss feed Print
Object Camera and World Map Problem
Nortski
Member #15,933
April 2015

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 :)

Chris Katko
Member #1,881
January 2002
avatar

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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Nortski
Member #15,933
April 2015

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 :)

Chris Katko
Member #1,881
January 2002
avatar

Nortski said:

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. :)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Nortski
Member #15,933
April 2015

Yeh everything is fine. Just caught a throat infection that prevented my free eating or drinking ;D 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!

Go to: