4 Fixes in CollisionExample#3
Open
Nikaoto wants to merge 2 commits intomiko:masterfrom
Nikaoto:collision-example
Open
4 Fixes in CollisionExample#3Nikaoto wants to merge 2 commits intomiko:masterfrom Nikaoto:collision-example
Nikaoto wants to merge 2 commits intomiko:masterfrom
Nikaoto:collision-example
Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed 4 things:
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 launchThe 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.
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.
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.