• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
chondee

Question about collision detection implementation

7 posts in this topic

Hi,

I am writing a 2d scrolling shooter game in c++/sdl, I have most of the basics already done, including collision detection, however I am wondering whether the way I am checking for collision is good for the long run, or I will have to rewrite the whole thing later.

What I am talking about is the way I currently have it is that during int (main) I call a global function that checks, loops through all the objects that needed to be checked for collision (player-enemy, enemy-enemy, player_bullet-enemy, enemy_bullet-player).

I am wondering if this centralized/separated way is the way to go, or I should do this more OO-y, that every object should have a collision check method for themselves.

Could someone with more experience with games involving collision detection give me some advice?

Thanks in advance!
0

Share this post


Link to post
Share on other sites
The way I do it is that each game object has a list of collision boxes. Collision boxes can be passed any collision box to see if the 2 intersect. This way I can check to see if any 2 given game objects are colliding. I can also do collision pruning and only check collisions between objects of interest this way. The way you describe what you're doing, it sounds like you check collision between everything and everything else, and you definitely don't want to do that.
0

Share this post


Link to post
Share on other sites
That is pretty much the way I am doing it, using collision boxes, and before doing checks whether the collision box sides intersect I first check if they are close at all (that is pruning, right?). I only check between objects of interest too, I don't think I am doing any unnecessary checks.

What my concern is about whether I should keep my collision check as a central global function, or I should equip all objects with the their own collision checks, so everything checks for itself.
0

Share this post


Link to post
Share on other sites
Well that's good then. I also do my pruned collision checks in my main loop. In my base update, I update the players, then check for collisions, and react if necessary. I think that's an ok design.

I don't think each object should have its own specialized collision check. Just having a collision box check for collision with another collision box works well. This keeps the collision checking decoupled from any particular game object, which is useful since it allows for it to be applied more generally.
1

Share this post


Link to post
Share on other sites
Imagine you have 1000+ objects in a level, a not entirely inconceivable possibility. You can't check every object against every other object, because that would take too long. Instead, you should split the scene into axis-aligned cells, and quickly sort objects into the cells based on position. Then at the collision-detection phase, only check the objects in the same cell, and possibly the neighbouring cells, in case the object overlaps a boundary. You can also recycle the cell structure by using it to speed up rendering - quickly determine the visible cells, and then draw only the objects inside or intersecting those cells.
1

Share this post


Link to post
Share on other sites
Collision groups is the way to go. This way you can quite easily check collision only for those cases that really matter and skip the rest. Also I would imagine that you have nested for loops for all the groups. In cases where you have a list of objects that can collide each other(enemy-enemy) the most important optimization is to not check the same collision twice. easiest way to make this not happen is to make the inner for loop start from i+1 where i is the outter for loops iterator.
Also in a 2d sidescroller if you don't have active stuff outside of the screen you can skip all non active objects in the collision detection and you should have nothing to worry about. If you have 10-200 objects on the screen at the same time you don't really need any fancy optimizations like cell divisions and such.
1

Share this post


Link to post
Share on other sites
Thanks for the input.
Objects that get out of the screen get flagged as dead and will be removed before the collision check, but as you thought, I do have some nested loops, that I will look over to avoid checking for the same collision twice, from the point of view of both objects.
0

Share this post


Link to post
Share on other sites
yeah. That is the most you can really optimize in a 2d scroller collision detection without too fancy tricks. couple of years back I was working in a project that had 2000+ objects running at once that needed collision detection with each other. Even with multithreading it took some fancy tricks to get running in realtime. Eventually we managed to get 8000 simultaneous objects to run smoothly on a quad core system even with the path finding and collision avoiding AI running individually for them all by running a slower thread going trough each pair every couple of seconds and making each one a table pointing to all objects nearby and the faster threads just iterating trough these tables. Took months to optimize but it was worth the experience.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0