Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Pixel grid vs Hitbox (collision detection)

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Pixel grid vs Hitbox (collision detection)

    I have been working on a program for a little while now. Now that I got my GUI stuff figured out (had no idea what I was doing when I started) I am working on getting the rest of my game together. Right now I am working on the collision detection subroutines, and have basically come down to two ways to do it, and am wondering which would be the better way to go.

    The first way I started was with a pixel object, which many put together would create other objects within the game. The variables are an int[] array to keep track of location in window, and a ton of boolean variables such as isSolid, isPlayer, etc.... Then with each image I was going to assign each pixel for what it is supposed to represent in game. This would lead to a pixel precise collision detection, but then again, it also leads to the program passing around huge arrays of pixel[], the most basic of which would be comprised of 2500 pixels.

    The other idea was to create a hitbox object, which was simply a set of coordinates which represent the size of the object in game. It would work much the same way but it would only have to pass around an array of 4 int variables at the cost of being less precise.

    I would hate to put a lot of work in the pixel based system only to find out later that it bogs down the program too much and have to switch to hitbox anyway. I would like to be more precise if I could.


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pixel grid vs Hitbox (collision detection)

    You didnt say what game you are makeing so it would be a little hard to find out, but if its a shooter then pixel grid(otherwise bullet collision would look a bit wierd.), if its a Mario brothers style game - that doesnt really matter, wichever suits you.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pixel grid vs Hitbox (collision detection)

    If you're asking which one to use, it really depends on the game. Some games can get away with basic collision detection. Others need precision. There are also a ton of other ways to deal with collision detection.

    A note: passing around huge arrays isn't a big deal, since it's a single reference no matter how big the array is. Passing an array of a trillion Objects costs the same as passing an array of 2 ints. It's iterating through the array that gets you (and memory, but who cares about that these days).

    You could also take a hybrid approach: use the cheap hitbox detection, and then only bother with the pixel-by-pixel detection if the cheap method returns true.

    But like I said, it really depends on the specifics of your game, how you're running it, what system you're shooting for, etc.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pixel grid vs Hitbox (collision detection)

    Quote Originally Posted by Alex555 View Post
    You didnt say what game you are makeing so it would be a little hard to find out, but if its a shooter then pixel grid(otherwise bullet collision would look a bit wierd.), if its a Mario brothers style game - that doesnt really matter, wichever suits you.
    I don't want to be pedantic, but I would argue that very few shooters require pixel-by-pixel collision detection. They're more "is the bullet inside the object?" than "which exact piece did I hit?".
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pixel grid vs Hitbox (collision detection)

    Most shooters have headshots =) lol

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pixel grid vs Hitbox (collision detection)

    Quote Originally Posted by Alex555 View Post
    Most shooters have headshots =) lol
    Eh, so that's two hitboxes instead of one. No need for pixel-by-pixel detection. Even if you wanted to test for eyes, nose, and mouth hits, that's still not pixel-by-pixel. Pixel-by-pixel detection is more for stuff like fluids and substances.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Pixel grid vs Hitbox (collision detection)

    Its going to be more of a mario style game. It also sounds like I should be just fine with the pixel based system that I already started building. Thanks guys

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pixel grid vs Hitbox (collision detection)

    Quote Originally Posted by DOLZero View Post
    Its going to be more of a mario style game. It also sounds like I should be just fine with the pixel based system that I already started building. Thanks guys
    A platformer like Mario probably only needs hitboxes or a grid-based detection system. Although passing the arrays of pixels isn't the problem, iterating through them is. It's a ton of overkill to check every pixel of your player and every block, when all you really care about are the outlines of each, and you only care about the blocks near the player.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 02:12 PM
  2. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  3. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  4. Collision detection - whats wrong with my code?
    By Javaz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 22nd, 2011, 08:20 PM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM