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 6 of 6

Thread: Need help with collision in a game!

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with collision in a game!

    So, I'm making a platform-style game, and I'm not sure where to start with collision programming. I use four variables for the player: an x and y for where it is drawn, and another set for the opposite corner of the sprite. In a sense, it looks kind of like this:

    xPosLeft = x;
    xPosRight = x+Player.width;
    xPosUp = y;
    xPosDown = y+Player.height;

    For every element or other character in the game, I plan to use the same types of variables to define their bounding box. I suppose I could easily create a rectangle around the sprite, and use the build in collision methods, but based on the nature of the game, I need to know whether the player is landing on something, hitting the bottom of something when jumping, or colliding with something to the left or right.

    Edit: An example of something I would need to be able to display: A character is walking left and passes through (in front) of a tree. He collides with a wall and stops because he cant pass it. He jumps on top of the wall. Even higher up there is a platform. He jumps through the bottom of the platform, and lands safely on top. Note: I know how to define these types of events, I'm more looking for a way to define the collisions from all angles based on the variables I use for the four corners of a sprite.
    I'd like to try to avoid putting everything into a collision array if possible, because the idea seems too abstract to me, and because there will be a lot of elements of various sizes. This is for a school project, so I'd appreciate any and all responses with haste!
    Last edited by Skyhigh32; May 16th, 2011 at 08:35 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with collision in a game!

    Looks like an ambitious project.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with collision in a game!

    It is. It's a final project for school. Any ideas?

  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: Need help with collision in a game!

    ...I don't see a question.
    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with collision in a game!

    Any ideas?
    Just standard stuff. Break the project up into small parts. Work out techniques with small programs and then merge them into the final program.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with collision in a game!

    If you're sticking in the 2d world, a simple grid partition works really well. Divide you're game world into a 2d grid with buckets at each grid box for holding items which are at least partially in that grid box. The size of the buckets is highly application specific, experiment with a combination of small, medium, and large bucket sizes to determine what will work best for you (or create a heuristic for having your game generate bucket sizes for you). Note that it's easiest if all grid boxes are the same size and evenly spaced.

    Then place all your world items into the grid (items which overlap multiple buckets are placed in all of those buckets they lie at least partially in). Once you've got the buckets filled, simply use spacial indexing to translate which buckets a particular item of interest (for example, a player) is in, then search those buckets for items and check collision detection between those items and your original item of interest. Once you've found a collision (or possibly multiple collisions, you might be able to handle each collision independently), you can handle specific states here.

    For example, if you're character is beneath a platform and you want him to jump up through the platform and land on it, you can detect that you're character has collided with the platform, but he still has an upward velocity so the collision is ignored. However, on the way down his velocity will be directed downwards, and the collision can be registered as landing on the platform. No vertical velocity component (i.e. standing/running on the platform) can probably be handled as if he was landing on the platform.

    Alternatively, you could try using a hierarchical system (KD-tree, quad tree, etc.). These systems generalize to higher dimensions really well, though for most 2D collision detection I think they're overkill.

Similar Threads

  1. Collision Detecting
    By iams3b in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 01:48 AM
  2. Collision Check Error
    By Josh Yaxley in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 12th, 2010, 02:12 PM
  3. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM
  4. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM
  5. bounding box collision
    By beechy34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2010, 08:58 AM