0
\$\begingroup\$

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?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I suggest you change the title of the question to make it more relevant with what you are actually asking. \$\endgroup\$ Feb 9, 2017 at 10:06
  • \$\begingroup\$ @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. \$\endgroup\$
    – Gnemlock
    Feb 9, 2017 at 18:42
  • \$\begingroup\$ @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. \$\endgroup\$ Feb 10, 2017 at 10:21

3 Answers 3

1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$
    – palmi
    Feb 9, 2017 at 8:51
  • \$\begingroup\$ 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. \$\endgroup\$
    – user87553
    Feb 9, 2017 at 8:54
  • \$\begingroup\$ @palmi Vector substitution means that it should make no difference if you move an object or move everything else in the opposite direction. \$\endgroup\$
    – Philipp
    Feb 9, 2017 at 12:42
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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). \$\endgroup\$
    – zcabjro
    Feb 9, 2017 at 11:19
  • \$\begingroup\$ Thank you for the suggestion and link. Making it a child object is something I did not think of. \$\endgroup\$
    – palmi
    Feb 9, 2017 at 16:27
-1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ @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. \$\endgroup\$ Feb 9, 2017 at 10:20
  • \$\begingroup\$ 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. \$\endgroup\$
    – Gnemlock
    Feb 9, 2017 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .