Skip to content

4 Fixes in CollisionExample#3

Open
Nikaoto wants to merge 2 commits intomiko:masterfrom
Nikaoto:collision-example
Open

4 Fixes in CollisionExample#3
Nikaoto wants to merge 2 commits intomiko:masterfrom
Nikaoto:collision-example

Conversation

@Nikaoto
Copy link
Copy Markdown

@Nikaoto Nikaoto commented Oct 18, 2017

Fixed 4 things:

  1. Random obstacle generation
    Previously, every time the game was launched generated obstacles would have the same positions. Adding math.randomseed(os.time()) before obstacle generation fixed this. Now they have unique locations upon every launch
  2. Player/Cursor movement
    The player was only allowed to move in one direction. Fixed that by summing the dx and dy so the cursor can now move horizontally. Also, added Cursor.speed and made the movement based on deltatime (dt). This is good for two reasons: speed can be changed whenever needed and faster processors will not move the player faster when using dt (inversely with slower processors). In addition, this is the standard way of getting movement in games.
  3. Collision detection during movement
    The collision was checked in this way: Cursor's current location is saved, cursor is moved, if cursor is colliding then revert the location to the saved location. Improved this by adding nextX and nextY. They are the next X and Y coordinates of the Cursor in the next frame. We check if the Cursor WILL collide in the next frame. If it doesn't collide, then we move. This saves us from having to move the player back and forth, also it's lighter on the system.
  4. Readability
    Since this is Love2d-examples, a lot of people will be reading the code to learn about the framework so I made it more readable. I also added comments to explain what each section of the code does.

… different ones after every launch; improve movement (can also be horizontal now); improve collision calculation - previously, the Cursor was moved and then checked if was colliding, now we calculate the next X and Y and move the cursor if it will not collide, this saves time and is lighter on the system; added comments to explain the code better; add the standard way of handling movement in games
@Nikaoto Nikaoto changed the title 4 Fixes 4 Fixes in CollisionExample Oct 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant