Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39502
In Process

askthedev.com Latest Questions

Asked: May 6, 20252025-05-06T04:14:20+05:30 2025-05-06T04:14:20+05:30

Should I use BSP for all collision detection in my 2D space shooter, or are there better methods to consider?

anonymous user

I’m diving into the world of game development as a freshman CS major, and I’ve been working on a 2D space shooter using SDL2 in C. While I’ve managed to implement some basic features, collision detection is something that’s been on my mind a lot lately. I initially used AABB (Axis-Aligned Bounding Box) collision detection, which was straightforward, but I want to take things up a notch for scalability and style as my game grows.

I’ve heard a lot about using BSP trees for collision detection, and they seem promising, particularly for dividing space in a way that can handle lots of objects efficiently. However, I’ve also come across opinions suggesting that using BSP for all types of collision detection—whether between moving and static objects or between moving objects—isn’t always the best approach. Some say it can complicate things unnecessarily.

My game features a scrolling play area filled with static structures for cover, and enemies that spawn from random directions, shooting projectiles at the player. Given the dynamics of both static and moving objects in my game, I’m wondering: Should I stick with BSP for all collision detection, or would it make more sense to have different methods for moving to static and moving to moving collisions? Is there a better approach or maybe something that’s more widely used in the industry?

Also, if you’re willing to share, I’d love to know how you’d implement some of these techniques using SDL and C. I’m eager to learn! Any insights or pointers on this would be greatly appreciated. I’m excited to see what solutions or methods others have successfully used in their own projects!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-05-06T04:14:22+05:30Added an answer on May 6, 2025 at 4:14 am

      In your scenario where the game environment involves dynamic moving entities alongside static structures, BSP trees might not be the optimal solution for all collision detection cases, given their primary strength lies in static scene partitioning. BSP trees work efficiently for quickly querying which objects are relevant in collision checks, particularly static structures; however, applying them uniformly for moving vs. moving collision detection is unnecessarily complex and can negatively impact performance. A widely-adopted approach in the industry is to leverage spatial partitioning structures like Quadtrees (for 2D games) for efficient collision queries on static or semi-static objects, while separately employing relatively simpler but highly performant methods such as Axis-Aligned Bounding Boxes (AABBs), bounding circles, or even narrow-phase checks (pixel-perfect collision or SAT) for moving-to-moving object collisions.

      To implement these ideas practically in your game with SDL2 and C, you could maintain a simple Quadtree structure to partition static obstacles and enemy spawn zones, thus lowering the collision check costs drastically when querying projectile and ship positions. Moving objects, whose positions update each frame, typically benefit from simplified geometric collision checks such as circles or bounding boxes for initial broad-phase collision detection, followed by exact checks as needed. This hybrid methodology keeps your collision logic efficient, scalable, and agile, aligning more closely with industry-standard practices for 2D game development.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-05-06T04:14:22+05:30Added an answer on May 6, 2025 at 4:14 am

      Collision Detection in Your 2D Space Shooter

      Hey there! So, it sounds like you’re diving deep into collision detection, and that’s super exciting! AABB is a great starting place, but as you mentioned, as your game grows, you might want to explore a bit more.

      BSP trees can definitely seem cool for dividing space, but they’re not always the magic solution for every situation. They work well for static environments where you can preprocess the layout, but they can be overkill for dynamic elements, especially for moving objects. For your case, having a mix might be a solid approach!

      Consider keeping the AABB for simple bounding box checks, especially for projectiles hitting static objects. Then, for moving-to-moving collisions, you might want to look at something like Sweep and Prune or Circle Collision checks, which can often be simpler and faster than applying BSP trees.

      As for the implementation in SDL and C, here are a couple of ideas:

      • AABB Collision Detection: You’re likely already doing something like this:
        if (player.x < enemy.x + enemy.width &&
            player.x + player.width > enemy.x &&
            player.y < enemy.y + enemy.height &&
            player.y + player.height > enemy.y) {
            // Collision detected!
        }
      • Circle Collision Detection: If you decide to switch it up, here’s a simple way:
        float distX = player.x + player.width / 2 - (enemy.x + enemy.width / 2);
        float distY = player.y + player.height / 2 - (enemy.y + enemy.height / 2);
        float distance = sqrt(distX * distX + distY * distY);
        if (distance < (player.radius + enemy.radius)) {
            // Collision detected!
        }
      • Preprocessing Static Objects: If your static objects are more complex, maybe consider using a grid-based approach to quickly check what nearby objects each entity should collide with, rather than checking against everything.

      Ultimately, it’s about finding the balance that works best for your game’s needs. Experiment and see what feels good! Happy coding!

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.