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]
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.
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.
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.
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. ]
When you draw you need to subtract the camera position to properly position your objects on screen.
screenx = ball.x - Cameraposition[0]; screeny = ball.y - Cameraposition[1];
Hey edgar!
Could you explain that a bit, please. Because what you say in words would help me more to understand this ..
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.
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);
Derp. Sorry. Going away now.
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.