1
\$\begingroup\$

I'm trying to get a game on the google play store before I'm 40 (long held dream to publish a game since the days of the speccy), I have just over a month to go so it's not looking good! Foolishly I decided to use SDL2 and Box2d to write the game, both technologies I have little experience in & to compound my misery I'm a noob at C++ so please bear with me if I've made a common error.

Basically I've managed to get a simple physics simulation up and running and have sorted applying my SDL textures to the box2d collision boxes, that all seems to be fine and dandy, where I've come a cropper is my player moves along a ground entity on the X axis,

I've managed to implement a scrolling background but it doesn't keep pace with the player, in which I mean the player moves across the screen instead of remaining central as I'd like him to and the background scrolls behind him. I've probably completely mis-interpreted how to manage my requirements and after days of searching for reference code I'm now at a bit of a loss.

Hopefully some kind soul can help me realise my dream! I've posted what code I think is relevant but if theres something missing I'll add it.

Player movements

// Get the position and angle of the player
playerPos = player.pl_body->GetPosition();
playerDeg = player.pl_body->GetAngle();
playerVel = player.pl_body->GetLinearVelocity();

float velChange = desiredVel - playerVel.x;
float impulse = player.pl_body->GetMass() * velChange; //disregard time factor
player.pl_body->ApplyLinearImpulse( b2Vec2(impulse,0), player.pl_body->GetWorldCenter(), true );

SDL2 rendering

//Render current frame using clips
SDL_Rect* playerCurrentClip = &playerClips[ plAnimFrame / 2 ];

// Render player texture to x & Y of player entity
gPlayerTexture.render (playerPos.x - player.PLAYER_WIDTH/2, playerPos.y - player.PLAYER_WIDTH/2, playerCurrentClip, playerDeg, NULL, flipType);

Camera setup

// Setup a surface for the camera area
SDL_Rect camera = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
// Render background texture
gBackgroundTexture.render (0, 0, &camera, NULL, NULL, flipType);

Camera scrolling

//Center the camera over the player
camera.x = (playerPos.x + player.PLAYER_WIDTH / 2 ) - SCREEN_WIDTH / 2;


 //Keep the camera in bounds
 if( camera.x < 0 )
 {
     camera.x = 0;
 }
 if( camera.x > LEVEL_WIDTH - camera.w )
 {
     camera.x = LEVEL_WIDTH - camera.w;
 }
\$\endgroup\$
0
\$\begingroup\$

From the code you posted:

// Render player texture to x & Y of player entity
gPlayerTexture.render (playerPos.x - player.PLAYER_WIDTH/2, playerPos.y - player.PLAYER_WIDTH/2, playerCurrentClip, playerDeg, NULL, flipType);

It appears you are not subtracting the camera.x from the player position.

// Render player texture to x & Y of player entity
gPlayerTexture.render (playerPos.x - player.PLAYER_WIDTH/2 -(( camera.x )), playerPos.y - player.PLAYER_WIDTH/2, playerCurrentClip, playerDeg, NULL, flipType);
\$\endgroup\$
  • \$\begingroup\$ Keep at it. It has nothing to do with a specific age. It is just all about learning way to increase productivity. People find a million excuses for procrastination, hell I'm procrastinating right now. :) \$\endgroup\$ – wolfdawn Apr 22 '14 at 5:11
  • \$\begingroup\$ Hi Arthur :D Many thanks for the tip, I'll have a test later this evening and let you know how I get on. Cheers for the kind words also! \$\endgroup\$ – Funk247 Apr 22 '14 at 11:08
  • \$\begingroup\$ Awesome! It's working! Thank you so much for the help! I hav'nt been this chuffed since my son was born! \$\endgroup\$ – Funk247 Apr 22 '14 at 20:27
  • \$\begingroup\$ I am glad and I learned a new auto-antonym. ;) You are welcome. SDL, box2d and C++ are all challenging. \$\endgroup\$ – wolfdawn Apr 23 '14 at 9:19
  • \$\begingroup\$ lol, now I know what an auto-antonym is so we've both learnt something new :D \$\endgroup\$ – Funk247 Apr 23 '14 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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