Hit detection for non-rectangles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evilbutterfly
    FFR Player
    • Apr 2003
    • 5784

    #1

    Hit detection for non-rectangles

    So I've got a game in the works, finally. Basically I'm diving headfirst into Java with what I know after a couple semesters and I plan to write as much as I can. I've been doing a lot of planning and not much writing of code (that's how I do things. I plan for a long time and then write it all in a flurry without realizing it), and I've been thinking about hit detection.

    How the hell do you do hit detection on anything but a rectangle? I mean, rectangles are easy. Just
    Code:
    if(thing.x+thing.width > stuff.x && thing.x < stuff.x+stuff.width)
    and then the same deal for y and height, and then you're done. Simple, easy.

    But I don't want everything in my game to be a rectangle or really close to it. Some things (small circles, mainly) work pretty well having a rectangular hitbox, but it'd be nice if I could have a triangular hitbox. I just can't think of a way to make if(between slope and straight line) into actual code.

    Am I missing something really obvious and dumb, or is it really that hard? And I don't even wanna think about doing a circlular hitbox.

    Note: Programming in Java (because it's all I know, pretty much), which probably isn't the best for this. The only game programs we've done in class was Snake, and that's all 1x1 squares hitting each other, which is piss easy.
    So I've gone completely slack-ass and haven't done any work on creating games. =(

    In less-depressing news, I got a job for an online business (which sells non-electronic games, of all things!) which has taught me a lot about marketing online and all that jazz.

    So now I'm on Twitter @NoahWright.
    And I write the blog for their website.

    Plus I do cool programming in-house that you'll never see. =O
  • Afrobean
    Admiral in the Red Army
    • Dec 2003
    • 13262

    #2
    Re: Hit detection for non-rectangles

    I thought everything 2D used rectangles for hit detection. I mean, come on. "hitbox". Boxes are rectangular. What's wrong with using a square box on a circle anyway? I'm quite sure the professionals wouldn't hesitate to do that.

    Comment

    • Yesssss
      FFR Player
      • Jul 2005
      • 2522

      #3
      Re: Hit detection for non-rectangles

      If you wanted to do a circle on circle collision you could test if the sum of the two radiuses is less than the distance between the two centers.

      Circle on rectangle would be kind of complicated, and using any shapes more complicated than that I wouldn't bother with.
      YOU JUST WON THE GAME! CONGRATULATIONS!

      Comment

      • JKPolk
        tool
        • Aug 2003
        • 3737

        #4
        Re: Hit detection for non-rectangles

        I believe that if you use javax.swing.*, there are methods for containers which have the ".contains()" method. If you want to detect a collision, pass some points of one object into the contains method of the other object. If true, voila you have a collision.
        It's been a long time since I've worked with these types of object, but this info should be correct.

        Comment

        • sleeplessdragn
          ~Bang that beat Harder~
          FFR Simfile Author
          FFR Music Producer
          • Jan 2004
          • 2321

          #5
          Re: Hit detection for non-rectangles

          Circular hitbox calculating through the radius is the best way to go I think. I had a lab involving some bouncing balls in a box; it was rather easy to put together actually. If whatever you want to hit a circle crosses the radius or is over the radius, then etc etc.

          Comment

          • evilbutterfly
            FFR Player
            • Apr 2003
            • 5784

            #6
            Re: Hit detection for non-rectangles

            You guys (except Mr. Polk) must have misread what I said. I said it didn't bother me to use squares to represent circles. What I'm concerned with are triangles. With a circle, if a bullet or whatever cuts off the edge it's barely noticeable in a fast-paced game (planning on making a side-scrolling shooter, Gradius-esque). If there is a slope (aka: triangle) in the level and you use a square to represent it, you're making it twice as big. It'd be really easy to fly your ship into the area right above the slope of a triangle expecting to not be hit, die, and be extremely mad.

            And Polk, the way they teach is totally gay and they basically always give us the GUIs. I'm basically planning/writing the back end of the program and will then dive into the GUI and pray for the best. I'm basically just gonna try and read the GUIs they've given us and figure things out as best I can, but if anybody wants to give hints and tips that'd be nice. Like, I don't even know how to start with displaying the things. But you mentioned containers...*looks on google*

            Looks like if I just define a shape (Java has a Shape class built in, and it has a .intersects() class which will do what I need) as the hitbox it'll work. *checks API* There isn't a Triangle class, but there is a Polygon one, so I'll be able to do plenty of stuff. Awesome. =O

            EDIT: I might as well just change the topic title to "oh **** eb is making a game" since I'll undoubtedly come up with all sorts of questions and I doubt Google will always save my ass. XD
            Last edited by evilbutterfly; 01-8-2007, 04:04 PM.
            So I've gone completely slack-ass and haven't done any work on creating games. =(

            In less-depressing news, I got a job for an online business (which sells non-electronic games, of all things!) which has taught me a lot about marketing online and all that jazz.

            So now I'm on Twitter @NoahWright.
            And I write the blog for their website.

            Plus I do cool programming in-house that you'll never see. =O

            Comment

            Working...