Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision Detection in "more advanced" 2D Scenario  (Read 3391 times)

0 Members and 1 Guest are viewing this topic.

Skuller74

  • Newbie
  • *
  • Posts: 3
    • View Profile
Collision Detection in "more advanced" 2D Scenario
« on: March 10, 2012, 08:02:32 am »
So far I have coded a simple Pong clone, and a Tetris clone. Collision detection was very simple in these. Pong: If ball has negative velocity, check collision on left paddle, if ball has positive velocity, check for collision on right paddle. With tetris, I set the board up as an array of 0s (empty), 1s (occupied), and 2s (board boundary), checked board space in the direction in which the piece was about to move, and did stuff based on the result. This may not have been the best way but I wanted to try something different.

Now I am moving on to Sprite-based bounding-box collision detection. I haven't actually started the project yet; however, I am planning for a Pac-Man clone or Pac-Man-esque game if I decide to change it up.

I plan on displaying the board as a series of sprites assigned to different numerical values and load it from an arbitrary file, say "maze1.map", which contains a 2D array of the previously stated numbers based on the shape of the maze piece (vertical, horizontal, corners, and end pieces). I'd like to eventually extend this to a map editor, but may save this for a future project.

:arrow:!! START HERE IF YOU DON'T CARE ABOUT THE OTHER BULLSHIT !! :: Anyways, I'm wondering how to process collision detection with so many "collidable" sprites on the board at one time (on a game such as pacman, using collidable sprites for maze pieces and precise bounding-box collision). Do I check EVERY sprite on the board for collision every time the frame updates? Or do I divide the board into quadrants, change which quadrant I'm testing based on the area of the character (and ghosts), and only check that quadrant/subquadrants?

I don't need code help or anything with this, just theory on how to handle collision detection.

Thanks :D

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Collision Detection in "more advanced" 2D Scenario
« Reply #1 on: March 10, 2012, 08:20:42 am »
For Pacman, checking every object for collisions against every other object should be fine.

For more complex scenes, dividing the world up is definitely the way to go. Quadtrees are probably the right thing to suggest at this point. Though a simpler system with a fixed number of buckets can still be hugely beneficial if that's all that is required.

Skuller74

  • Newbie
  • *
  • Posts: 3
    • View Profile
Collision Detection in "more advanced" 2D Scenario
« Reply #2 on: March 10, 2012, 09:07:59 am »
Okay awesome, thanks for the quick reply. I have one more quick question in regards to this.

I feel like this is the right way but I'd like to double check. Should I store all of the sprites using a vector, and then use a for loop to cycle through each one, checking for collision each iteration? I've never used a vector, but I know the general idea of how to implement one.

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Collision Detection in "more advanced" 2D Scenario
« Reply #3 on: March 10, 2012, 12:39:32 pm »
Quote from: "Skuller74"
I've never used a vector, but I know the general idea of how to implement one.


Use the implementation in Standard Template Library.  :wink:
http://www.cplusplus.com/reference/stl/vector/

 

anything