Page 1 of 1

Obstacles in game

Posted: Sun Dec 20, 2020 4:37 pm
by konrmat171
Hi everyone
I'm new here and I'm supposed to write a flappy birds game or temple run game. I'm supposed to be responsible for the piece of code that generates obstacles and moves them towards the hero and the background. Is there anyone here who can help me with this task because I don't even know how to start?

Re: Obstacles in game

Posted: Sun Dec 20, 2020 7:33 pm
by pgimeno
Hi, welcome to the forums. Do you have any background in Lua or in any other programming language?

Re: Obstacles in game

Posted: Sun Dec 20, 2020 8:28 pm
by konrmat171
Yes i made simple paralax system with two img (one for background and another to ground). Now I try to create two types of obstacles (for example, jump barrels and crouch posts).

Re: Obstacles in game

Posted: Mon Dec 21, 2020 11:58 am
by pgimeno
OK, then I suggest you use axis-aligned rectangles as the collision shape, because these are the simplest to check against. For a Dinosaur Game type of game, you only need collision detection, not collision response, which simplifies things for you.

Every time you need to create a new obstacle, you need to add the corresponding collision rectangle.

There are several ways to check for collision; the simplest one is to use the Separating Axis Theorem, which when applied to convex polygons (like rectangles), says that two polygons overlap only if none of their edges leaves one polygon on one side of the edge and the other polygon on the other side. For axis-aligned rectangles, that condition translates to this code: https://love2d.org/wiki/BoundingBox.lua

Every time you remove an obstacle (because it goes off the screen), you also remove the collision rectangle.

See if that helps. If not, feel free to ask for clarification you need.

Re: Obstacles in game

Posted: Thu Dec 24, 2020 2:41 pm
by konrmat171
Thank you very much, it will be of great help in the future, but I am currently having trouble drawing random obstacle. I don't know how to write an algorithm that creates a obstacle that goes in my direction and crate new obstacle every few seconds.

Re: Obstacles in game

Posted: Fri Dec 25, 2020 12:46 pm
by RNavega
konrmat171 wrote: Thu Dec 24, 2020 2:41 pm I am currently having trouble drawing random obstacle. I don't know how to write an algorithm that creates a obstacle that goes in my direction and crate new obstacle every few seconds.
Hi. What have you tried already, can you show some code?

The things you're asking are very simple, so maybe it's better to follow a "flappybird clone" tutorial instead until you get more experience. Here's a good one, using LÖVE: https://simplegametutorials.github.io/love/bird/

EDIT: When following tutorials for LÖVE, note that they might be using an older version, so a function name or usage might be different. If you find these differences then you need to go to the library documentation and find the updated function and how to use it.

EDIT 2: you can find more LÖVE tutorials in here: https://love2d.org/wiki/Category:Tutorials

Re: Obstacles in game

Posted: Fri Dec 25, 2020 12:55 pm
by RNavega
Also, if you imagine yourself making more complex games in the future, it'd be interesting to invest some time in making simpler games first that will give you an easier challenge in learning different techniques, design patterns and such.

There are two cool articles talking about this, not specifically about LÖVE, just in general no matter what language/libraries you're using: