Camera and platform Update

posted in BKP Game journal
Published January 07, 2019
PC
Advertisement

Hello,

It's been a while. I was busy again with other stuff but I managed to work on the game engine (less on the game itself) during this time. The new features are not visible for most of them, it is more about code optimization and organization. But now we have a better 2D camera system that I will present right now.

The first nice thing is how to use the camera. I just need to initialize, choose among some functions and call the camera update function it in the game loop.


void bkp_graphics_2DinitCamera(BKP_Rect dim, BKP_Rect world,int camera_function); //Initialize
int bkp_graphics_camera2D(BKP_Dynamics2D P); 							//Call in main loop to follow the scene

void bkp_graphics_2DsetCameraDim(BKP_Rect dim); 			//obsolete
void bkp_graphics_2DsetCameraWorld( BKP_Rect world);		//world limits (up,left,down,right)
void bkp_graphics_2DsetCameraAuto(float auto_speed);		//obsolete use camera2DsetFunc() instead                   
void bkp_graphics_2DsetCameraSpeed(BKP_Vec2 speed);         //set a speed other than default
void bkp_graphics_2DsetCameraPanic(float up, float down);   //set the panic lines for vertical scroll IMPORTANT

void bkp_graphics_2DtoggleCamera(void);                   //Freeze the camera to the current position or unfreeze

void bkp_graphics_camera2DsetFunc(int function);			//select a camera function 

Basically, there are only 2 mandatory functions, others are used to change the default values for the camera function, focus or speed. There are more than listed here. Yes, it is possible to change the camera function during the game. Here is an example of how to use it:


BKP_Rect world = setMapPlatform(); 


bkp_graphics_2DinitCamera(bkp_rect(g_scr.w / 4, g_scr.h * 2 / 5, 0.0f, 0.0f), world, BKP_CAMERAFUNC_SMOOTHSTD);
bkp_graphics_2DsetCameraPanic(150,50);
bkp_graphics_camera2DsetFunc(BKP_CAMERAFUNC_FOCUS); 

while(Player->input->Cancel == 0)                                                                                                                                                                                                                                            
{
        bkp_input_capture(Player->input);

        manage_player();
        bkp_graphics_camera2D(Player->dyn);

        Ugp_draw();
        _update_fps_counter( fps); //print on the screen FPS and Memory usage
        _update_memUsage(mem);

}

1/get the world dimension from the level layout.

2/ select smoothstd function for scrolling
3/set the up panic at 150 pixels under the top of the screen and 50 over the bottom of the screen
4/we change the current function to FOCUS to tell the camera to scroll to the target instead of just show the player in the centre (look at the beginning of the 2nd video to see the scrolling animation to find the player). The camera will switch  automatically to the function passed in 2DinitCamera as soon as the target is locked.
By using BKP_CAMERAFUNC_FIXEDSETFOCUS,  we will set the camera without scrolling the prior animation to find the player.

I organized the code in the way that it is easy to add a new camera function without modification in the game loop. let's have a look. Actually the bkp_graphics_camera2D() is that simple:


int bkp_graphics_camera2D(BKP_Dynamics2D P)                                                                                                                                                                                                                                    
{
    int ret = camera2D(&stc_2D->camera, P);
    if(BKP_TRUE == ret)
    {
        bkp_graphics_2DsetCameraSpeed(stc_2D->camera.delta);
        bkp_graphics_2DTranslateView(stc_2D->camera.speed); // manual
    }
    return ret;
}

int camera2D(BKP_Camera * S, BKP_Dynamics2D P)
{
    if(S->on == BKP_FALSE)				//set or unset by toggle()
        return BKP_FALSE;

    return TBL_Camera[S->func](S, P);   //all functions are in this table
}

Notice that I pass a BKP_Dynamics2D structure to the camera instead of anything else not general, all objects I want to get focused by the camera should have a BKP_Dynamics2D structure. That's it, Player, NPC, or whatever object so far has that. it is a simple structure with position, dimension, ...

So, next time I want to add a new camera I just have to create the function and set a new entry in the enum of camera functions et voila, nothing to change, just add.

LOCKON camera

 

 

 It is just a basic camera where the player is centred everytime it is possible. For horizontal movements it is ok but when it comes to jumps it starts to hurt our eyes. I did it at the beginning just to be able to test the collisions and travel around the map.  This camera function is not really usable to play a platform game. 

 

STDsmooth camera

 

This function solves the problem with vertical scrolling and smoothly scroll. when changing the direction we can notice the camera scroll to give us some room in the direction the player is facing. On vertical movement, the Camera scrolls only the player is on a platform or if he is going to high (on the video the orange line)

 

here let see the differences between the two functions on vertical movements.(sorry for the gif, gimp decided to annoy me) . You can see on the left gif the player jumping without the camera moving while on the right gif it moves everytime the player move up

focusy_optimzed_.gif.2dba55b9b15d27dd7c12a8bbce3e8c0c.giflocky_optimzed_.gif.03e8fdcdef48f941ce6ba0525574c30b.gif

The lockon camera is used in Soul of Mask, but I keep it here because this is the choice I made for autoscrolling. I made a function auto scroll before which is obsolete (or not, maybe I will delete it maybe not) and replace by the Lockon camera. I just ask the camera to follow a target different than the player. Here on this video, you will see a green square moving around. The camera will use this green scare as a target, that's it I have an auto scroll now:

 

Coming up next:

 I will stop the game engine and go back to the game development again, the next time we should have the first image of the game.

Thanks for reading. Any technical questions ? don't hesitate. I decided to release the engine part on Github and only keep the game part proprietary.

 


 

 

 

 

Previous Entry Scroll Mem and Text
1 likes 2 comments

Comments

Rutin

Welcome back, looking forward to seeing more progress on your game. :) 

January 07, 2019 02:48 PM
lilington

yep, see you soon with more progress.

January 08, 2019 11:43 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement