Results 1 to 10 of 10

Thread: Collision Detection And Objects.....

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    22

    Collision Detection And Objects.....

    ok i am tring to write a multi-collision detection function that will check through all objects in an array......heres the source code.....

    __________________________________________________
    Class Object "Player" with collision detection method.....
    __________________________________________________

    Private fall As Boolean
    Private DestRectPlayer As RECT

    Public Property Get fallo() As Boolean
    fall = fall
    End Property

    Public Property Let DestinationRect(NewValue As RECT)
    DestinationRect = NewValue
    End Property

    Public Property Get DestinationRect() As RECT
    DestinationRect = DestRectPlayer
    End Property


    Public Sub MultiCollision(ObjectArray As Object, PartOfArray As Boolean, NumberInArray As Integer, TotalNumberInArray As Integer)

    If fall = True Then
    Exit Sub
    End If

    Dim TempRect As RECT

    If PartOfArray = True Then

    For i = 1 To (NumberInArray - 1)
    fall = IntersectRect(TempRect, DestRectPlayer, ObjectArray(i).DestinationRect)
    If fall = True Then
    Exit Sub
    End If
    Next i

    For i = (NumberInArray + 1) To TotalNumberInArray
    fall = IntersectRect(TempRect, DestRectPlayer, ObjectArray(i).DestinationRect)
    If fall = True Then
    Exit Sub
    End If
    Next i

    Else

    For i = 1 To TotalNumberInArray
    fall = IntersectRect(TempRect, DestRectPlayer, ObjectArray(i).DestinationRect)
    If fall = True Then
    Exit Sub
    End If
    Next i

    end if

    End Sub

    ______________________________________
    Form Calling It....
    ______________________________________

    Dim objPlayers(1 To 10) As New Class1

    Private Sub Form_Load()

    With objPlayers(1).DestinationRect
    .Right = 30
    .Bottom = 30
    End With

    With objPlayers(4).DestinationRect
    .Left = 15
    .Top = 15
    .Bottom = 45
    .Right = 45
    End With

    Call objPlayers(1).MultiCollision(objPlayers, True, 1, 10)

    End Sub

    ___________________________________________________
    Module With Inserect API....
    __________________________________________________

    public Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long

    _______________

    when ever i run it i get a "byref argument type miss matched" error highlighting the "objplayers(1)" when calling it....now to me that sounds like i have spelt .DestinationRect wrong in the function...but i havent......i have been trying to figure this out for 2 days......any help please?

    Thanks in advance....
    Last edited by JSG; Sep 24th, 2001 at 10:42 PM.
    Two Wrongs May Not Make A Right, But Three Rights Make A Left....

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    You must put ObjectArray() As Object I believe...
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    22
    ok, i am making progress

    i get another error though...."type mismatch:array or user defined type expected" highlighting, again, objplayers when calling it.....now i thought that it was an array (declared "Public objPlayers(1 To 10) As New Class1" in module....) i tried calling it with objplayers() but that doesent work either.....

  4. #4
    Zaei
    Guest
    I only looked through your code for a bit, but I want to try a bit of function re-writing. Here we go.

    Code:
    Private fall As Boolean 
    Private DestRectPlayer As RECT 
    
    Public Property Get fallo() As Boolean 
    fallo = fall 
    End Property
    Otherwise your Propery Get wont do anything.

    You are trying to detect collisions between all objects, correct?
    Code:
    For i = 0 to UBound(ObjectArray)
        For j = 0 to UBound(ObjectArray)
            if not (i = j) then ' we dont want to check the object against itself
                'IntersectRect stuff goes here
            end if
        Next j
    Next i
    This will check each object against all other objects.

    The last thing is, You detect collisions, but how are you going to handle them? You need to think about that.

    Z.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    22
    thanks alot for the re-write.....i never thought of doing in that way! but i am still having trouble passing an array to the function...keep getting "type mismatch:array or user defined type expected" error......is there a trick or something that i am missing?
    Two Wrongs May Not Make A Right, But Three Rights Make A Left....

  6. #6
    Zaei
    Guest
    Post some current code so we can see what you are doing. Format it with [ code] and [ /code] tags (without the spaces).

    Z.

  7. #7
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    yay, use [code] and [/code] or use my code-formatting utility (see signature)

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    22
    ok, first i would just like to say thanks for helping me out

    i wrote this little example to show you what i mean......

    Class Module ("class1")
    ________________________

    Code:
     Private test As Integer ' the local variable of property Ptest
    Public Property Let Ptest(ByVal vData As Integer) 
        test = vData
    End Property
    
    
    Public Property Get Ptest() As Integer
        Ptest = test
    End Property
    
    Public Sub MsgBoxTest(object()  As Object, TotalNumberInArray As Integer) 
    
        For i = 1 To TotalNumberInArray
            MsgBox object(i).Ptest
        Next i
    End Sub
    ________________________

    Form From Where i am calling Objects ("form1")
    ________________________
    Code:
    Private test(1 To 5) As New Class1 
    
    Private Sub Form_Load()
    test(1).Ptest = 1 
    test(2).Ptest = 2
    test(3).Ptest = 3
    test(4).Ptest = 4
    test(5).Ptest = 5
    
    test(1).MsgBoxTest test , 5
    
    End Sub
    _________________________

    when i run it i get a "Type Mismatch: Array or User Defined Type Expected" error....with it highlighting test (in red...) now i thought i declared it as an array? (blue) and i added those bracets when declaring the function (teal).

    thanks for your help.
    Last edited by JSG; Sep 26th, 2001 at 08:56 AM.
    Two Wrongs May Not Make A Right, But Three Rights Make A Left....

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    parameter object() As Object should be object() As Class1, it won't be smart enough to cast down all class1 in array to object, in case you want other arrays than class1 arrays to be passed as well declare the parameter variant, but this will be a greater peformance hog
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    22
    thanks alot!
    Two Wrongs May Not Make A Right, But Three Rights Make A Left....

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