Results 1 to 5 of 5

Thread: Alright. I've asked this question before, Collision Detection.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Angry

    I have been trying to get collison detection in my programs, and each time I think I have it I don't. So, if one of you knows how to achieve this with an object that moves around a screen and bumps into other objects in the middle of the room, please tell me. Or, if someone has a program that already does this, I would equally appreciate it. Please help me, this is the only thing standing in the way of me, and making a game.

    End.
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    Collision Detection.

    Fun!

    well first you have to keep track of all your objects on the screen. Most likely you have a structure (Type) which represents an object on the screen. so, all you have to do is get the object in question and compare it's coord's with all other objects.

    this is on the fly so it's not going to be 100% accurate nor is it the most eff. way.

    Code:
    Public Type BALL_OBJECT
      X  as Long    '' center
      Y  As Long    '' center
      DX as Long    '' movement on the X axis
      DY As Long    '' movement on the Y axis
      R  as Long    '' radius
      D  as Boolean '' is the object displayed
      N  as Long    '' object number
    End Type
    
    Global GBO(0 To 99) as BALL_OBJECT
    
    Public Function DetectCollision(bo as BALL_OBJECT) as Boolean
      
      Dim lLoop as Long
      Dim dist  as Long
      
      For lLoop = 0 To Ubound(GBO)
        '' check for not the same object # and the object is being displayed
        If (GBO(lLoop).N <> bo.N) And (GBO(lLoop).D) Then
          
    
          '' get the est. distance
          dist = (abs(bo.X - GBO(lLoop).X) + _
                  abs(bo.Y - GBO(lLoop).Y)) - 
                 (Int((abs(bo.X - GBO(lLoop).X) + _
                  abs(bo.Y - GBO(lLoop).Y))/2)
          
          '' check for a collision
          If (dist <= (GBO(lLoop).R + bo.R)) Then
            DetectCollision = True
            Exit Function
          End If
        End If
      Next lLoop
      
    End Function
    remember, this may not work correctly but it's a step in the right direction.

    Good Luck
    -Shickadance

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    See. I've already taken that step...

    Code:
    For counter = 1 To 8
        If collider.Top >= wall(counter).Top And collider.Top <= wall(counter).Top + wall(counter).Height Or collider.Top + collider.Height >= wall(counter).Top And collider.Top + collider.Height <= wall(counter).Top + wall(counter).Height Then
            If collider.Left >= wall(counter).Left And collider.Left <= wall(counter).Left + wall(counter).Width Or collider.Left + collider.Width >= wall(counter).Left And collider.Left + collider.Width <= wall(counter).Left + wall(counter).Width Then
                collide = True
            End If
        End If
    Next
    This is what I have come up with. It checks for all of the objects on the screen, which are in a control array. But, when the picture of my character hits the top of an object (when their tops are the same) he will go right through it. I'm currently checking for something though. The key to this may lie in whether or not he's bigger than the object on the screen. A certain occurance my code doesn't cover..
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Oh my god.

    I figured it out.

    Ok. There are 3 types of 2D collison.

    1.) The user's top is greater than the top of an object, but less than it's bottom. We have a hit.

    2.) The user's bottom is less than the object's bottom, but greater than it's top. More collison.

    3 (the one that screwed me up.) The user's top is less than the objects top, and the user's bottom is greater than the object's bottom, in other words, the user is bigger than the object itself. Which was the problem I had.

    , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , !!!!!!!!!!!!!!!!!
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

  5. #5
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    Congrats!!

    heh, hey if you highlight those smiley faces at the bottom of your message some look mad and some look said. wierd

    -Shickadance

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