Results 1 to 6 of 6

Thread: Collision

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Collision

    Let's say I have a tile looks like this:
    Collision-tileu.png
    The pink colour is transparent.

    How can I check whether or not the user is touching the tile? It's okay for the user to touch the transparent area but not the actual tile (the boxes).
    I have tried doing this by checking the colour at the player's pixel but my game started to lag. or maybe because I've had like 3 loops to check the tile next to the player and looping through each of the collidable tile IDs and checking whether or not it is colliding.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Collision

    What is a tile? What does it mean for a user to touch a tile?

    Perhaps part of the tile's state could be position and orientation (think "Tetris") in which case part of its behaviour could be to say whether a given point is "within" the tile. (java.awt.Shape instances implement, in a well defined way, a notion of "insideness".) If there are only a few (hundreds, say) tiles I would go for iterating through the whole lot to decide which, if any, contained some given position. If there were rather more tiles (millions, say) I would think about putting them in some sort of data structure that would allow quick access to just those tiles that were possible candidates for including a given point.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Collision

    Quote Originally Posted by pbrockway2 View Post
    What is a tile? What does it mean for a user to touch a tile?

    Perhaps part of the tile's state could be position and orientation (think "Tetris") in which case part of its behaviour could be to say whether a given point is "within" the tile. (java.awt.Shape instances implement, in a well defined way, a notion of "insideness".) If there are only a few (hundreds, say) tiles I would go for iterating through the whole lot to decide which, if any, contained some given position. If there were rather more tiles (millions, say) I would think about putting them in some sort of data structure that would allow quick access to just those tiles that were possible candidates for including a given point.
    A tile is just a Image with x and y coordinates.
    You can move the player around the screen and when you do that player's x and y coordinates change.
    When the user enters the image I want it to stop. Kind of like in tetris, you can go into the shape.
    If I would use shapes will I need to convert the Image into a Shape?
    Or is there an easier way of doing it?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Collision

    So if I understand you both player and tile are small graphics and you want to detect when they overlap (including touch).

    I'm no expert in any of this, but the first thing I would observe is that what makes this tricky is that not all points in the images "count": ie it is only the non transparanet parts that matter. The first thing I would do is determine the "bounding" boxes for the player and all the tiles, and only check for a collision when the bounding boxes intersect. This won't involve much calculation.

    Next it may be that the specific shapes have a geometry that make it relatively easy to detect if a point is inside the shape. Your example shape is like this: the "L" shape has a simple geometry that allows you to tell whether a given test point is inside just based on the coordinates of the 6 corners.

    Only as a very last resort would I compare the location of non transparent pixels in the two images pixel by pixel.

    -----

    It might be worth reading up about the Shape interface. There is no simple image->shape conversion - basically the distinction is between raster and vector graphics. But it might be possible for you to represent the elements as both an image and a shape: the latter to facilitate collision detection.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Collision

    Have a look at gamedev.net.
    There must be a number of articles there on collision detection with sprites, which is what you're talking about.

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Collision

    Quote Originally Posted by pbrockway2 View Post
    So if I understand you both player and tile are small graphics and you want to detect when they overlap (including touch).

    I'm no expert in any of this, but the first thing I would observe is that what makes this tricky is that not all points in the images "count": ie it is only the non transparanet parts that matter. The first thing I would do is determine the "bounding" boxes for the player and all the tiles, and only check for a collision when the bounding boxes intersect. This won't involve much calculation.

    Next it may be that the specific shapes have a geometry that make it relatively easy to detect if a point is inside the shape. Your example shape is like this: the "L" shape has a simple geometry that allows you to tell whether a given test point is inside just based on the coordinates of the 6 corners.

    Only as a very last resort would I compare the location of non transparent pixels in the two images pixel by pixel.

    -----

    It might be worth reading up about the Shape interface. There is no simple image->shape conversion - basically the distinction is between raster and vector graphics. But it might be possible for you to represent the elements as both an image and a shape: the latter to facilitate collision detection.
    Ugh. This is confusing. I'm sure my programming knowledge is too bad for this.
    I have looked on gamedev.net, but I couldn't find anything.

Similar Threads

  1. Collision in j2me
    By Ashish_jain in forum Java 2D
    Replies: 8
    Last Post: 06-24-2011, 03:58 PM
  2. 2D - Collision messed up
    By überfuzz in forum New To Java
    Replies: 0
    Last Post: 06-22-2011, 09:20 PM
  3. Collision in GUI 2d Game help
    By heysayjump in forum New To Java
    Replies: 7
    Last Post: 03-14-2011, 02:01 AM
  4. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 12:38 AM
  5. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 09:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •