Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Convert image to a number of squares?

  1. #1

    Convert image to a number of squares?

    I'm working on a 2D game using sprites. Instead of making exact collision masks for each sprite, I just want to assign a number of rectangles to each sprite, so that if the object that I'm checking for collision is within any of these squares, it's colliding with my sprite. I've tried drawing the squares on top of my sprite image manually and then inputing the coordinates of each square into my code, but drawing these squares takes a lot of time, so I was wondering if there's a program that can convert an image into X number of squares (all of them within the edges of the sprite), or a plugin/script for GIMP that can do that?

    (Hope this makes sense!)

    EDIT: Here's an example:

    -->
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  2. #2

    Re: Convert image to a number of squares?

    I dont know if it is worth the trouble.

    Alternatively you can look at this:
    It is also possible to use the stencil buffer for collision checking:
    http://kometbomb.net/2007/07/11/hard...ion-in-opengl/
    and
    http://kometbomb.net/2008/07/23/coll...queries-redux/
    http://3das.noeska.com - create adventure games without programming

  3. #3

    Re: Convert image to a number of squares?

    The thing is, I can't really use screen drawing functions for my collision detection routine, as a lot of these collisions will happen off-screen. That's why I want to go with the collision boxes idea, as everything needed for checking for collisions will then be hardcoded into the game logic and won't require any screen drawing or checking of textures and sprites. Plus, it'll be a lot faster.

    I'll just continue doing the box drawing manually, I guess...
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  4. #4
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    Re: Convert image to a number of squares?

    I would suggest a real good google. I've tried to write a simple progam to do this, but it has some flaws and it certainly won't work properly with concave outlines like the example sprite you provided. Essentially though what I did was calculate the outline of the sprite and then using some very basic statistical analysis to calculate the weight of each column and row, I created a set of quads which contained the sprite (the next stage was to be calculating whether each quad contained some of the sprite.

    There are a whole host of articles on the web about bounding boxes and sprite based collision detection. Most of them appear to use a containing bounding box that encompasses the whole sprite and then do pixel based checks if you get a match on bounding boxes. Of course, you could adopt a two stage bounding approach... one box defines definite hits and another defines possible hits.

    You've not stated what the underlying API is (or whether you're using some sort of framework) I think, depending on the underlying API, it can also be done by the API and I know it can be done by some of the frameworks.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  5. #5

    Re: Convert image to a number of squares?

    I've already googled this a lot, but wasn't able to find a program or plugin to help me. I know about different methods for collision checking, so I wasn't looking for alternative methods, as the one I've decided on is best suited for my game. Anyway, I did a quick test and discovered that the boxes don't have to be all that precise - the effect is actually best if the boxes don't follow the edges of the image 100%, as this gives the effect of the object being 3D, since all bullets won't impact right on the edge but sometimes further in. It works perfectly. Almost seems to be pixel perfect collision, but a lot faster and not as clinically precise on the edges as pixel perfect collision would be. So, doing the boxes manually won't be quite as big a chore, since they don't need to be all that perfect.

    (I'm using SDL and OpenGL, by the way.)
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  6. #6

    Re: Convert image to a number of squares?

    why not use polygon collision detection?

    but maybe we can come up with an algorithm do do that
    From brazil (:

    Pascal pownz!

  7. #7

    Re: Convert image to a number of squares?

    I've been racking my brain trying to come up with an algorithm to calculate these boxes, but I'm drawing a blank. Any ideas are welcome.
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  8. #8

    Re: Convert image to a number of squares?

    octree's or something like that?
    Try to subdevide the boundbox into smaller cubes and check each time if there are pixels inside. After x steps you have some boxes left with either pixels inside and ones without.
    http://3das.noeska.com - create adventure games without programming

  9. #9

    Re: Convert image to a number of squares?

    What if your sprite is triangle shaped?

  10. #10

    Re: Convert image to a number of squares?

    The idea is to calculate the squares that approximate the shape, whatever it is.

    Quote Originally Posted by noeska
    Try to subdevide the boundbox into smaller cubes and check each time if there are pixels inside. After x steps you have some boxes left with either pixels inside and ones without.
    I'll give this a try. I'll probably try it with all pixels inside the square required to have an alpha value above 0.5, or something similar.
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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