2D side scroll game: Background is not working right.

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.


Additionally! I generated obstacles from x=800 which also move out of bound along with my line.

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

Need expert inputs to tackle this problem.

[Update]
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]
Last edited on
closed account (3qX21hU5)
I am not sure I am understanding your problem exactly. Could you post a screenshot or two of what is happening?

I am not getting what you mean by you want the line to stay there instead of going out of bounds...

If you could clear this up I would be glad to help you. Also what library are you used to do this project?
updated with screenshots!
closed account (3qX21hU5)
Ok I believe I understand what you are saying now. Basically the line segment is ending at 800 pixels and you want it to continue going on father then that.

Note: If you let me know what library you are using I might be able to provide you with more information that pertains specifically to that library.

In order to fully understand what is happening and why you should know what the screen coordinate system is and what the world coordinate system is.

Basically in your game the screen coordinate system is what you see in your game window. It is 800 x 400 pixels and that is your screen coordinate space.

What is displayed on your screen coordinate system is determined where the camera is at in the world coordinate space. The world coordinate space goes way beyond the left, right, top and bottom of your game window and you can have objects anywhere in that space.

Basically think of your game window as a rectangle that is only seeing a certain part of the game world. When your ball moves it moves the rectangle (Camera) along with it.

So lets say you move your camera +800 pixels along the X axis and +200 pixels along the Y axis . This would mean that the top left corner of your rectangle (Camera) is now positioned at (800, 200) in world coordinates

Here is a diagram to help visualize this.


Before the move
World
(0, 0) is in the corner

______________________  World X Axis
|        |
|        |
|        |
|        |
|________|                                    
|                    
|                    
|                    
|       
|
|
|
World Y Axis




World
(0, 0) is in the corner

______________________  World X Axis
|
|       ___________
|       |          |
|       |          |
|       |          |
|       |          |
|       |__________|
|
|
|
World Y Axis



The little box is what your screen is showing of the world coordinate system. So by moving your camera you are moving around in the world coordinate system and whenever something goes outside of your rectangle it will no longer be drawn (But it will still be there). The same goes the other way around.

So now that I got my crappy explanation of the different coordinate systems out of the way lets get to the fix ;p.

The fix is actually quite easy to do. All you need to do is change the size of your line segment so it is more then 800 pixel long on the X axis. Since the world coordinate system basically goes on for infinity you can make it as long as you want (Though I wouldn't make it more then what you need).

So if you change it so that your line segment is 5200 (Or however long you want it) pixels long on the X axis instead of 800 you should be fine.

Though be cautious since I am not sure what library you are using I am not sure if they do culling while drawing (Basically what that means is they only draw things that are on screen) so you might want to look into that sometime later and see how to do it.

Anyways for the short answer to your solution if you skipped reading my wall of text all you need to do is change the size of your line segment to more then 800 pixels ;p.

Hope this helped a bit and hope it is somewhat comprehensible.
Last edited on
Topic archived. No new replies allowed.