danieldesira / TurtleQuest

https://turtle-quest.vercel.app/
Creative Commons Attribution 4.0 International
0 stars 0 forks source link

Optimise collision detection for floating objects #27

Closed danieldesira closed 6 months ago

danieldesira commented 6 months ago

Collision detection seems to be fine and fast but I think that it would be ideal to differentiate between vertical and horizontal detection so as to avoid nested loops and hence improve performance.

danieldesira commented 6 months ago

Input from ChatGPT: Your FloatingGuidedObstacle class looks well-structured, handling the initialization of position, movement, and collision detection effectively. Here are a few suggestions and observations:

  1. Code Organization: Consider breaking down the isCollidingWithTurtle method into smaller, more focused functions. This can improve readability and maintainability, making it easier to understand and debug the collision detection logic.
  2. Performance Optimization: The current collision detection approach involves iterating over each pixel within the obstacle's bounding box and checking for collisions with the turtle. While this approach works, it may not be the most efficient, especially if the obstacle or turtle sprites are large. You might consider optimizing this process, such as by using bounding boxes or more efficient collision detection algorithms, depending on the specifics of your game.
  3. Direction Handling: Your current implementation of handling directions (Directions.Left and Directions.Right) seems straightforward and effective. Just ensure that the direction is consistently updated and applied correctly in the swim method.
  4. Dependency Injection: Consider injecting the dependencies (Game, collidedWithTurtleDown, etc.) into the class constructor instead of accessing them directly through the Game.instance singleton. This can make your code more modular and testable, as dependencies can be easily mocked or replaced in unit tests.
  5. Error Handling: Make sure to handle edge cases and errors gracefully, especially in methods like swim, where the obstacle's position could potentially go out of bounds. Ensure that the obstacle's position is constrained within the boundaries of the game area.
  6. Documentation and Comments: Add comments to clarify the purpose of methods and the logic behind them, especially for complex or critical sections of code. This can help other developers (and your future self) understand the code more easily.

Overall, your FloatingGuidedObstacle class appears to be well-implemented and suitable for its purpose. By following the suggestions above and continuously testing and refining your code, you can further improve its quality and maintainability. If you have any specific questions or concerns about any part of the code, feel free to ask!

danieldesira commented 6 months ago
  1. Consider implementing the following algorithm and testing it:
    
    interface BoundingBox {
    minX: number;
    maxX: number;
    minY: number;
    maxY: number;
    }

function checkAABBCollision(boxA: BoundingBox, boxB: BoundingBox): boolean { // Check for non-overlapping conditions along x-axis if (boxA.maxX < boxB.minX || boxA.minX > boxB.maxX) { return false; }

// Check for non-overlapping conditions along y-axis if (boxA.maxY < boxB.minY || boxA.minY > boxB.maxY) { return false; }

// If both conditions are false, bounding boxes are overlapping return true; }

// Example usage: const boxA: BoundingBox = { minX: 0, maxX: 50, minY: 0, maxY: 50 }; const boxB: BoundingBox = { minX: 20, maxX: 70, minY: 20, maxY: 70 };

if (checkAABBCollision(boxA, boxB)) { console.log("Collision detected!"); } else { console.log("No collision detected."); }


2. Document the abstract classes for characters and level.
3. Produce JS doc.
danieldesira commented 6 months ago

Updated:

  1. To check for and add missing documentation.
  2. Finding a tool which generates html or md for the documentation would be nice to have.
danieldesira commented 6 months ago

Regarding documentation generation, Typedoc supports markdown so we can give that a shot before closing this ticket