Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Every time I move the hero on my screen I increment a screenOffsetX and a screenOffsetY. These add 1 if you move right, -1 if left to the sceenOffsetX. Add the screenOffestY +=1 if down, -=1 if up.

I also have a variable for the first time through drawing called drawingForTheFirstTime.

The problem is that I have a level of size 40x40 array. I am drawing a camera view of 20x20. I shift the camera fine as long as I am not near the edges of the map. When I try to go into the upper left of the map, I lose track of the hero.

I dont want the camera to move if the hero is close to the edge of the map. I want him to move independantly of the camera. Otherwise he stays at the same position on the screen while the map moves underneath him. I think it is only my if logic that is broken.

if(!(screenOffsetX != 0 || screenOffestY != 0) && !drawingForTheFirstTime)
{
    return false;
}
else
{
    //move camera
}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

I'm a beginner too, but I just tackled this problem in my own game, so hopefully you will find this helpful:

First off: you have screenOffsetY spelled as screenOffestY, with the e and s reversed. So I'm not sure if that was intentional or not.

Secondly: Since there is only limited source code provided to base my answer on, I'm going to have to generalize it a bit.

In my own personal experience (I'm working with C++ and SDL too for my game), in order to use the scrolling camera effectively, I'm needing to enact constraints on the camera's movements.

The camera moves left If and Only If (IFF), the x-coordinate of the camera is greater than 0. The camera moves right IFF the x-coordinate of the camera, is less than the width of the level/background/screen minus the width of the camera-frame.

Similarly, the camera moves up iFF the y-coordinate of the camera is greater than 0, and the camera only moves down IFF the y-coordinate of the camera is less than the height of the level/background/screen minus the height of the camera-frame.

Obviously you'll need to use your wisdom to apply this to your own particular need, and to figure out if you want to move your camera relative to your screen, your background, your level, or whatever else you want.

Below is the actual code that I'm using to keep my camera within the level bounds. I'm doing my camera relative to my level background which is contiguous, so this may be different from what you need, but hopefully it may help get you on the right path. :3

void Camera::updateCamera(int x, int y)
{
    //centers camera on provided x/y coords
    gameCamera.x = x - gameCamera.w / 2;
    gameCamera.y = y - gameCamera.h /2;

    //keep camera in bounds
    if (levelWidth != 0 && levelHeight != 0)
        {
            if (gameCamera.x < 0) gameCamera.x = 0;
            if (gameCamera.y < 0) gameCamera.y = 0;
            if (gameCamera.x > levelWidth - gameCamera.w)
                gameCamera.x = levelWidth - gameCamera.w;
            if (gameCamera.y > levelHeight - gameCamera.h)
                gameCamera.y = levelHeight - gameCamera.h;
        }
}

EDIT: included alternate camera algorithm below which automatically centers, and keeps itself in bounds in relation to a surface(likely the level) that you pass to it. We pass the rect by const, since you should be passing the rectangle by reference since its an object (otherwise the program will create a copy of the object every time this method runs), and we don't want our code to have the privilege of being able to alter the original rect. Note: It's irrelevant that nothing in our code tries to change anything in the rect, but it's simply good programming practice to ensure that our code doesn't even have PERMISSION to change it.

void Camera::updateCamera(const SDL_Rect *object)
{
    //centers camera on provided x/y coords
    gameCamera.x = (object->x + (object->w / 2)) - (gameCamera.w / 2);
    gameCamera.y = (object->y + (object->h / 2)) - (gameCamera.h /2);

    //keep camera in bounds
    if (levelWidth != 0 && levelHeight != 0)
        {
            if (gameCamera.x < 0) gameCamera.x = 0;
            if (gameCamera.y < 0) gameCamera.y = 0;
            if (gameCamera.x > levelWidth - gameCamera.w)
                gameCamera.x = levelWidth - gameCamera.w;
            if (gameCamera.y > levelHeight - gameCamera.h)
                gameCamera.y = levelHeight - gameCamera.h;
        }
}

Hope this helps!~ ^-^

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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