Results 1 to 9 of 9

Thread: Collision detection with stuff in background image

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Collision detection with stuff in background image

    Hi,

    I'm just starting out writing a game with VB6 and bascally it is a character walking around 2D on a background image in all 4 directions. I was to do collision detection for objects such as a tree, building, etc. But these items are included in the background rather than added on top as objects. I probably assume they should be objects instead right?

    I read a tutorial on how to scroll the background which is awsome but how to I scroll the objects also? Just move them with the background? And only bother doing this with ones that would be within range of the screen to see rather than ones all over the background image?

    If I left objects painted into the background, I was gonna use some sort of array to detect where the objects would be. But thats more calculation when I can use collision detection with objects probably. Maybe I just answered my own question then!

    Thanks

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Collision detection with stuff in background image

    Okay, if I use objects on top of the background image, how is the best method to detect collision? Do i have to check every single object I put on the background? Or try to filter doing it only with objects that are on the visual screen somehow?

    Thanks

  3. #3
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: Collision detection with stuff in background image

    Quote Originally Posted by WarrenW
    Okay, if I use objects on top of the background image, how is the best method to detect collision? Do i have to check every single object I put on the background? Or try to filter doing it only with objects that are on the visual screen somehow?

    Thanks
    i have some adivices:
    for you do a scrolling game, you can use 2 pictureboxes:
    one of these pictureboxes is for show the game;
    and the other is for moving(here is the game)(for scrolling the game).
    other advice:
    use(for example) an image array, because they are transparent without images for "walls".
    wheres a nice function for detect collision:

    Code:
    Public Function Collision(Object1 As Variant, Object2 As Variant) As Boolean
        If (Object1.Left + Object1.Width >= Object2.Left _
            And Object1.Left <= Object2.Left + Object2.Width) _
            And (Object1.Top + Object1.Height >= Object2.Top _ 
            And Object1.Top <= Object2.Top + Object2.Height) Then
            Collision = True
        Else
            Collision = False
        End If
    End Function
    important: test these function and tell me if is ok.
    i hope help you
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Collision detection with stuff in background image

    Thanks for the reply. I'll try out your code for the collision detection part. My main concern now is knowing which objects to check for collision. If there are many objects, I hate to loop through all of them for each movement.

    I was thinking that I would use a 2 dimensional array and any object would be placed at its point in the array. This way when I go to move I can just check the array places ahead of time and only in my area to check for collision. I'm hoping this would speed up everything.

    I like your idea of using empty images that are then transparent. I could draw out my whole background and then place images over them. Perfect!

    Thanks

    Warren

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Collision detection with stuff in background image

    Thread moved from 'Game Demos' sub-forum

  6. #6
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Collision detection with stuff in background image

    Choosing which objects to check for collision is something that has plagued game developers for a long time.

    "Brute Force" is the term for simply applying collision detection to everything at once. It works fine on small games, but for anything large you can't afford to run collision detection on every object: it just gets too processor intensive.

    The idea therefore is to use some simple calculations to scythe off those objects that have no chance of collision. Although there are all sorts of methods to do this (Quadtree's are fun ), a much simpler method is a distance check. Work out the size of the object, and only apply collisions if the distance to the player is smaller than this value. With a rectangular picture box, it's fairly simple: The distance to the furthermost point on the pBox, as well as the distance to the player, can be worked out with Pythagoras's theorm.

    Size of pBox = SquareRoot((Width/2)^2 + (Height/2)^2)
    Distance to player = SquareRoot(Abs(Player.X-pBox.X) + Abs(Player.Y-pBox.Y))

    Only if Size>Distance should you perform the more complex collision checks.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Collision detection with stuff in background image

    Seeing as you are talking about collision with background objects you can detect this easier than sprite hits sprite.
    A typical tiled background will store information about the tiles in an array. I've tried something before which uses a long array, the first two bytes refer to the background tile, the last two optionally refer to a foreground masked tile which is drawn on top of the background (eg background = grass, foreground=tree). It's very quick to determine which tile(s) the sprite is over, and equally quick to see if the tile in question is an obstacle or not, (Value > 32767 then it's an obstacle)

    Regarding Sprite hits Sprite detection as Quasar said checking all sprites against all sprites can waste time. For example checking 50 sprites requires 1225 comparisons (49+48+47+...+3+2+1). I've not come across Quadtrees before, but it seems to be a form of zoning. If i divided my game area into say 10 zones and then take note of which sprite is in which zone (some might be in more than one) I can then tackle the collision detection zone by zone, 1225 comparisons could become something like 150 (10 zones of 6 sprites each). Another point regarding Pythagoras's theorm, square Roots are relatively slow to compute it's much quicker to square a number. If your using Pythagoras for a distance comparison you can avoid using square root if you compare it to a squared distance.

  8. #8
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Collision detection with stuff in background image

    Thanks for the comment regarding Sqrt, Milk: I'll be using it.

    Quadtrees are a form of zoning where the entire map is broken into 4 zones, then each of those zones is broken into 4, then each of those... etc. The furth you sink into the quadtree, the smaller the nodes get and the more detail you can see on them.

    The whole idea is that if a parent zone doesn't need to be checked for collisions, none of its childnodes will need to be checked. This means that rather than checking each zone, you only check those zones which are relevant.

    They are often used in 3D terrain LOD: if a zone isn't detailed enough, break it apart and get a more detailed section.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  9. #9
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Collision detection with stuff in background image

    For my final assessment in my game dev course, I created a Collision class which was attached to each and every object.

    Each object had an update method, which would set a "has moved" flag, thereby calling the collision class which took care of everything involved.

    Also to cut down CPU cycles at certain points in the game, I checked collisions against the closest objects first, then the largest, then worked my way down in size.

    Also, if applicable, only check for collisions between objects on the screen, which will cut down your cycles significantly.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

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



Click Here to Expand Forum to Full Width