2d collision and image structure

This is a discussion on 2d collision and image structure within the Game Programming forums, part of the General Programming Boards category; I decided to start developing a small 2d platform type game because I haven't ever worked with or created a ...

  1. #1
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357

    2d collision and image structure

    I decided to start developing a small 2d platform type game because I haven't ever worked with or created a graphical game.
    Don't know if you will need this information but I am using opengl and devil.

    I don't know of a good way of performing collision detection with the environment which allows strange shapes and slopes without using per-pixel collision. Can anyone offer any insight into the subject?
    For object-object collision I'm thinking of using ellipses or squares which are defined seperately from the actual image.

    My second question is on how I should store animation information, especially when different frames of the same object seem like they should have different widths/heights (if that should even happen). I have some ideas but I have no idea on what the general/intelligent/flexible/etc. 'standard' way of storing an animation is.

    Any help or relative links/articles are greatly appreciately.

  2. #2
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    I really never thought about animation, but this is what i just think of:
    Code:
    int runAnimate=0;
    
    glLoadIdentity();
    runAnimate++;
    if(runAnimate >=2)    //how many image in the animated picture
        runAnimate = 0;   //reset the animation
    if(runAnimate == 0)
        glBindTexture(GL_TEXTURE_2D, texture[0]);
    else
        if(runAnimate == 1)
            glBindTexture(GL_TEXTURE_2D, texture[1]);
           //and so on with the third, fourth, ....
    
       glBegin(GL_QUADS);
            //Draw the box with glTexCoord2d();
       glEnd();
    I don't know if this'll help any but that the simplest way to do it (i think)
    Hello, testing testing. Everthing is running perfectly...for now

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Animation is simple if your resource cache and structures are set up correctly.

    My approach is this:

    1. Each texture resides in a global list.
    2. Each texture has an ID which is just it's index in the list.
    3. A manager class handles the textures and all access to and from them.

    In my approach you do not ever deal specifically with the texture. You simply do this:

    This sets the texture in Direct3D - port to GL as you see fit.

    Code:
    void Object::Render(float fTimeDelta)
    {
      ...
      ...
      Device->SetTexture(0,CTextureManager->GetTexture(m_dwCurrentID));
      ...
      ...
    }
    So the texture is not part of the object at all, it still resides in the global list. Now since animations are just a series of textures or images displayed over a certain time interval you can now create a class that does all of this.

    Animation can now be done by doing this:

    1. Create an AnimSequence class that handles animations
    2. Animations are nothing more than a set of stored texture IDs.
    3. Each frame of animation has a time value that tells the class how long to display the frame.
    4. If the animation has played fully (is on the last frame), if it's set to loop, set the animation index to 0 to retrieve the first texture ID and start the process over again.

    So an animation sequence will have this:

    Code:
    std::vector<DWORD *> IDs
    std::vector<float *> FrameTimes;
    You can use a map if you'd like, but I use two vectors to keep them separate.
    If you set your render up to retrieve the current texture from the texture manager based on the current frame ID then you never physically switch images. You simply request a different texture ID and bam....the texture manager returns it and thus the texture is set.

    It works quite well actually.

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    thanks, that helps quite a bit.

    For the file, should each frame be the same size? Or should I describe each frame's size in another section of the file. Also for different animations of the same object should I use different files, or just describe the positions (and length) of each animation?

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    I decided on a structure for my animations, but I still am a little fuzzy on how I should implement the level environment. If there is some standard way of doing it (which allows at least moderately complex slopes) I would love to know, if not, here is what I am currently thinking.

    I am thinking of making a bunch of vertices and then using line intersection tests, doing that I can't think of a reason I would need to run more than 4 tests. To figure out which vertices to test I could have two arrays of pointers, one sorted by X and one sorted by Y, and then keep track of the vertice to the left and top of the player's position, updating every time the player moves.

    Anyone have feedback, possible problems, ideas, insight, etc.?

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    For collision detection you could use the masking algorithm, and since it is animated, no matter what texture you use, the masking will take care of the collision detection for you :d
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    For the sprites I think using bounding boxes and ellipses smaller than the actual width and height of the images will be realistic enough and I don't need the extra information a masking collision detection method provides me. Although it may come in handy for environmental objects later on in the project, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL Collision Handling
    By Neo1 in forum Game Programming
    Replies: 2
    Last Post: 04-02-2008, 02:04 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  4. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 05:17 PM
  5. Collision detection algorithm
    By Hannwaas in forum Game Programming
    Replies: 5
    Last Post: 11-30-2001, 12:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21