Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 2D side scrolling platformer: Facing problems with background/flat surface.

This thread is locked; no one can reply to it. rss feed Print
2D side scrolling platformer: Facing problems with background/flat surface.
CodeOne
Member #15,442
December 2013

Hey guys,

I am working on a 2D side scroller game. Where a ball is moving continuously towards right . I used a camera to follow the ball as soon as ball reaches the center of screen. I created a horizontal line to look like the surface on which the ball is rolling.

line runs from x=0 to x=800 (i.e screen width)

NOTE: (0,0) coordinate is at top left corner of display.

Problem: My display is 800 x 400 . Camera follows the ball, but soon the ball crosses x=800 and starts moving in black background. I want that line surface to stay there instead of going out of bound.

Actually, its not just about the line segment anymore. The value of x=800 is very crucial because i draw random obstacles (the enemy TRIANGLES) from that point. So if the ball passes that 800th pixel , generated obstacles from x=800 also move off the screen along with my line. So the ball wont be facing any obstacles (where is the fun in that?). We dont want that to happen.

I did a little digging on internet, but nothing wraps around my head.

What should be done here? This is just my first c++ game project using ALLEGRO 5.0.10, so i might be skipping things that can solve the issue.

Need expert inputs to tackle this problem.

Hope these screenshots manage to show, what the real problem is:

[img]http://s22.postimg.org/erpdq7n3x/image.jpg[/img][/url]
[img]http://s22.postimg.org/mbigs9gal/image.jpg[/img][/url]
[img]http://s29.postimg.org/to2z1wh83/image.jpg[/img][/url]

l j
Member #10,584
January 2009
avatar

It's simple, don't move the platform where you are on at all, draw it in the same position and disregard the camera position completely.

CodeOne
Member #15,442
December 2013

Is that possible, i mean...the updatecamera position moves the entire screen. Which is why platform goes off the screen. The platform is a line segment drawn from (0,330) to (800,330). The only statement in drawground() is al_draw_line so i dont know what to change here.

Can you elaborate a little ? I may be acting dumb here.

l j
Member #10,584
January 2009
avatar

Are you using transformations to scroll the screen?

In that case you can draw a line segment from (x, 330) to (800 + x, 330). You could also first draw the line segment from (0, 330) to (800, 330), then apply the transformation and finally draw the rest.

CodeOne
Member #15,442
December 2013

:-/ Okay! It still aint working. Have a look at this ...
This might help you understand what I am doing wrong here. ::)
[This is incomplete code, I will mail you complete header and source if needed. ]

#SelectExpand
1 2#include <allegro5\allegro.h> 3#include <allegro5\allegro_primitives.h> 4#include "Head.h" 5 6// *****GLOBALS********************************************* 7const int WIDTH = 800; 8const int HEIGHT = 400; 9 10 11enum KEYS{LEFT,RIGHT, SPACE}; 12bool keys[3] = {false,false,false}; 13const int PI = 3.1428; 14 15const int BALL_RADIUS = 20; 16const int BALL_WIDTH = BALL_RADIUS *2; 17const int BALL_HEIGHT = BALL_RADIUS *2; 18float VELOCITY = 5; 19//float GRAVITY = 0.4f; 20 21 22 23 24void Initball(Rball &ball); 25void Drawball(Rball &ball); 26 27void Initground(Ground &gnd); 28void Drawground(Ground &gnd); 29void MoveballR(Rball &ball); 30 31 32 33void Cameraupdate(float *Cameraposition,Rball &ball,int BALL_WIDTH,int BALL_HEIGHT); 34 35 36 37int main(void) 38{ 39 // ****** Primitive variable******************************************** 40 bool done = false; 41 bool redraw = true; 42 const int FPS = 60; 43 float Cameraposition[2] = {0,0}; 44 45 46 // *******Object variables********************************************** 47 Rball ball; 48 Ground gnd; 49 Triangle triangle[NUM_TRIANGLE]; 50 51 52 53 // *******Allegro variables********************************************* 54 ALLEGRO_DISPLAY *display = NULL; 55 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 56 ALLEGRO_TIMER *timer = NULL; 57 ALLEGRO_TRANSFORM camera; 58 59 // *******Initialization************************************************* 60 if(!al_init()) 61 return -1; 62 63 64 display = al_create_display(WIDTH,HEIGHT); 65 if(!display) 66 return -1; 67 68 69 al_init_primitives_addon(); 70 al_install_keyboard(); 71 72 event_queue = al_create_event_queue(); 73 timer = al_create_timer(1.0/FPS); 74 75 srand(time(NULL)); 76 Initball(ball); 77 Initground(gnd); 78 Inittriangle(triangle , NUM_TRIANGLE); 79 80 81 al_register_event_source(event_queue,al_get_keyboard_event_source()); 82 al_register_event_source(event_queue,al_get_timer_event_source(timer)); 83 al_register_event_source(event_queue,al_get_display_event_source(display)); 84 85 al_start_timer(timer); 86 while(!done) 87 { 88 89 90 ALLEGRO_EVENT ev; 91 al_wait_for_event(event_queue, &ev); 92 if(ev.type == ALLEGRO_EVENT_TIMER) 93 { 94 redraw = true; 95 MoveballR(ball); 96 97 //***CAMERA*** 98 99 Cameraupdate(Cameraposition,ball,40,40); 100 al_identity_transform(&camera); 101 al_translate_transform(&camera,-Cameraposition[0],-Cameraposition[1]); 102 al_use_transform(&camera); 103 104 105 } 106 107 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 108 { 109 done = true; 110 } 111 112 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 113 { 114 switch(ev.keyboard.keycode) 115 { 116 117 case ALLEGRO_KEY_ESCAPE: 118 done = true; 119 break; 120 case ALLEGRO_KEY_SPACE: 121 keys[SPACE] = true; 122 break; 123 } 124 125 } 126 127 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 128 { 129 switch(ev.keyboard.keycode) 130 { 131 132 case ALLEGRO_KEY_ESCAPE: 133 done = true; 134 break; 135 case ALLEGRO_KEY_SPACE: 136 keys[SPACE] = false; 137 break; 138 } 139 140 } 141 142 Drawball(ball); 143 Drawground(gnd); 144 al_flip_display(); 145 al_clear_to_color(al_map_rgb(0,0,0)); 146 147 148 } 149 150 al_destroy_timer(timer); 151 al_destroy_display(display); 152 al_destroy_event_queue(event_queue); 153 return 0; 154} 155 156 157void Initball(Rball &ball) 158{ 159 ball.x = 50; 160 ball.y = 305; 161 ball.ID = BALL; 162 ball.lives = 3; 163 ball.speed = VELOCITY; 164 ball.rotation = 0; 165 ball.boundx = 18; 166 ball.boundy = 18; 167 ball.score = 0; 168} 169 170void Drawball(Rball &ball) 171{ 172 al_draw_filled_circle(ball.x +20, ball.y ,BALL_RADIUS, al_map_rgb(255, 0, 0)); 173} 174 175void Initground(Ground &gnd) 176{ 177 gnd.x = 0; 178 gnd.y = 330; 179 gnd.ID = GROUND; 180 //gnd.boundx = ; 181 gnd.boundy = 330; 182} 183 184void Drawground(Ground &gnd) 185{ 186 al_draw_line(gnd.x,gnd.y,WIDTH,gnd.y,al_map_rgb(0,0,255),10); 187 al_draw_filled_rectangle(gnd.x,335,WIDTH,HEIGHT,al_map_rgb(127,127,127)); 188} 189 190void MoveballR(Rball &ball) 191{ 192 193 ball.x += VELOCITY; 194 //ball.rotation += (float) ball.speed /C_BALL *360; 195} 196 197void Cameraupdate(float *Cameraposition,Rball &ball,int BALL_WIDTH,int BALL_HEIGHT) 198{ 199 Cameraposition[0] = ( ball.x + BALL_WIDTH/2) -(WIDTH/2); 200 Cameraposition[1] = ( ball.y + BALL_HEIGHT/2)-(330); 201 202 if(Cameraposition[0]<0) 203 Cameraposition[0]=0; 204 if(Cameraposition[1]<0) 205 Cameraposition[1]=0; 206 207}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

CodeOne
Member #15,442
December 2013

Hey edgar!

Could you explain that a bit, please. Because what you say in words would help me more to understand this ..

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

l j
Member #10,584
January 2009
avatar

Draw your ball at its coordinates minus the camera coordinates. Along with the rest of your objects. Draw them at their current position minus the camera position to get the position on screen of the ball / object.

He is using transformations to achieve the same already.

anyway.

// On draw
al_identity_transform(&camera);
al_translate_transform(&camera,-Cameraposition[0],-Cameraposition[1]);
al_use_transform(&camera);
// Draw everything affected by the camera
Drawball(ball);
// Reset camera
al_identity_transform(&camera);
al_use_transform(&camera);
// Draw everything not affected by the camera
Drawground(gnd);

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

CodeOne
Member #15,442
December 2013

Hey taron,

I got it working by doing this modification.

void Drawground(Ground &gnd,float *Cameraposition)
{
al_draw_line(gnd.x+Cameraposition[0],gnd.y+Cameraposition[1],800+Cameraposition[0],gnd.y+Cameraposition[1],al_map_rgb(0,0,255),10);

al_draw_filled_rectangle(gnd.x+Cameraposition[0],335+Cameraposition[1],WIDTH+Cameraposition[0],HEIGHT+Cameraposition[1],al_map_rgb(127,127,127));
}

However, your suggestion looks more efficient.So I will try that too.

Time to complete what I started. Looking forward to your guidance if I crash somewhere else . Thanks for being there Buddy. :D

Go to: