Love2d-samples icon indicating copy to clipboard operation
Love2d-samples copied to clipboard

4 Fixes in CollisionExample

Open Nikaoto opened this issue 7 years ago • 0 comments

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.

Nikaoto avatar Oct 18 '17 18:10 Nikaoto