# How do I make an infinitely scrolling background?

I am trying to make a game where a torpedos keep coming from random directions and the player has to maneuver a submarine to escape them.

I am currently offsetting the background to give it an illusion that the submarine is moving i.e. if the submarine moves forward, I move the background (quad) backwards. This is primarily being done as I want it to be an infinite space. The position of the submarine does not change, the player just controls the rotation of the submarine and the background moves in the opposite direction. The background is set to repeat to give it an infinite space illusion.

The torpedo gets spawned at a random distance and moves towards the submarine. Since, the position of the submarine does not change, the torpedo will always hit the submarine. How do I structure this game, specifically the background repeat so that I can actually move the submarine in absolute dimensions and still maintain the infinite space illusion?

• I suggest you change the title of the question to make it more relevant with what you are actually asking. Feb 9, 2017 at 10:06
• @realUser404, please make note of the title we have gone for. I noted an earlier edit suggestion on your behalf, but grammatically, it made even less sense than the original title. Feel free to make these suggestions in the future, just remember that grammar is quality. Feb 9, 2017 at 18:42
• @Gnemlock I noted your edit, but I can see where the misunderstanding between us comes. I believe the OP has solved the "infinite space" problem, his problem comes from the movement of all the objects around him. Problem which imho is due to the fact that he chose to move only the background instead of moving the player and then calculating the other objects from the player's position, cf my answer. Feb 10, 2017 at 10:21

If this is as complex as your game gonna be, you could move the torpedo with the background when the player presses a direction key.

i.e., if the player moves up, you don't actually move the player but you move the background AND the torpedo down.

• Thanks. That seems to be an option. I believe that it is going to be a bit difficult to manage that, considering that the torpedo moves in curves. Is there a better way to structure this game? Feb 9, 2017 at 8:51
• It's actually not that hard. Make the torpedo moves with the given trajectory toward its target. Whenever the player uses a direction key, just update the X or Y coordinate of the torpedo accordingly, as well as the X or Y coordinate of the target.
– user87553
Feb 9, 2017 at 8:54
• @palmi Vector substitution means that it should make no difference if you move an object or move everything else in the opposite direction. Feb 9, 2017 at 12:42

Make your torpedoes a child-object of the background quad. They will become bound to the background's transform and will move as the background moves (it won't matter what your torpedoes are doing, it should look fine). It will be as if you're moving everything BUT the submarine.

Although, making the submarine move and just stretching or tiling a background would be the more efficient way of making this. This wouldn't really mean infinite space but it would mean a HUGE space where the player can't really reach the edge even if they tried to (not the best way of doing it though).

The best way would be to just spawn new backgrounds as the player moves and delete the ones that the player gets too far away from. Something like this only in four directions instead of just one. I'm afraid you'll need to think of ways to do that on your own.

• Might want to mention precision errors when position vectors grow very large, solved by occasionally resetting the player's position to the origin (as well as everything else so that it is seamless to the player). Feb 9, 2017 at 11:19
• Thank you for the suggestion and link. Making it a child object is something I did not think of. Feb 9, 2017 at 16:27

What is usually done with parallax scrolling is draw the background according to the player's position.

This means that instead of doing :

if (moveRight)
background.x -= 50;


as I suppose you are doing right now, you should do :

if (moveRight)
player.x += 50;

drawBackground(player.x, player.y);


Then if you want to keep the submarine in the center of the screen, just apply an offset to all the objects you are drawing.

There are several articles as this one treating about parallax scrolling and video-games : Parallax scrolling

• @Gnemlock What do you want me to add? As already said, instead of moving the background when pressing directionnal keys a better solution is to update the player's position and draw the background according to the player's position. The link is just some additionnal resource for the OP. Feb 9, 2017 at 10:20
• Fair enough. I still think this answer is severely lacking, I just thought it had room for improvement. Draw the background according to the player's position tells us very little. What do you mean by player position? Do we make sure the background is always drawn with the player in a particular position? What position? How does that account for animation? etc. etc. Feb 9, 2017 at 10:42