Skip to main content
GameDev.net gamedev.net
🔒 Locked

How can I make a function to this code???

Started by -Fruz- Jan 13, 2002 at 5:45 PM 9 replies 350+ views
Original Post
-Fruz-
-Fruz-
I made this little code to c if two object''s collided... ---------------------------------------------------------------- Private Sub Timer1_Timer() If O1.Left >= O2.Left - O1.Width And O1.Left <= O2.Left + O2.Width And O1.Top >= O2.Top - O1.Height And O1.Top <= O2.Top + O2.Height Then Label1.Caption = "Crash" Else Label1.Caption = "" End If End Sub ---------------------------------------------------------------- How can I make a function so I can just say like: If Crash object1, Object2 By using the crash code i made? Plz help me! JFK
hewhay
hewhay
create a function with return type bool. Change your if then..
to
  
if (condition)
{
// your code

}
[else [if (otherConditioncondition)]
{
}]
[else
{
}
]
// the code between [] is optional


and change your AND to &&, your or to ||
humanity will always be slaved by its ignorance
Oluseyi
Oluseyi
Assuming you have defined an Object (with the properties Left, Top, Right and Bottom) type:

Private Function Crash(O1 As Object, O2 As Object) As Boolean
If O1.Left >= O2.Left - O1.Width \
And O1.Left <= O2.Left + O2.Width \
And O1.Top >= O2.Top - O1.Height \
And O1.Top <= O2.Top + O2.Height \
Then Crash = True
Else
Crash = False
End If
End Function


[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Anozireth
Anozireth
I''m a bit rusty on VB but I think this is how you''d do it.


Function Crash(O1 as Object, O2 as Object) as Boolean
If O1.Left >= O2.Left - O1.Width And O1.Left <= O2.Left + O2.Width And O1.Top >= O2.Top - O1.Height And O1.Top <= O2.Top + O2.Height Then
Crash = True
Else
Crash = False
End If
End Function





quote:
Original post by -Fruz-
I made this little code to c if two object''s collided...
----------------------------------------------------------------
Private Sub Timer1_Timer()
If O1.Left >= O2.Left - O1.Width And O1.Left <= O2.Left + O2.Width And O1.Top >= O2.Top - O1.Height And O1.Top <= O2.Top + O2.Height Then
Label1.Caption = "Crash"
Else
Label1.Caption = ""
End If
End Sub
----------------------------------------------------------------
How can I make a function so I can just say like:

If Crash object1, Object2

By using the crash code i made?
Plz help me!

JFK


Oluseyi
Oluseyi
quote:
Original post by hewhay
create a function with return type bool. Change your if then..
to <snip> and change your AND to &&, your or to ||

It's rather fascinating that you didn't notice he wasn't programming in a C-derivative.

[Edit:] Formatting.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!


Edited by - Oluseyi on January 13, 2002 7:17:09 PM
-Fruz-
-Fruz-
Thanx guys!
U have been very helpfull!
btw... Is it a simpler n faster way to detect object collision then this?
(I''m doing this in VB)
-Fruz-
-Fruz-
Oops... I forgot something! I wan''t this code to work with different pictures... C? So if i write for example this:
--------------------
if crash bullet, car
--------------------
It detects a collision with the bullet and the car, and if i write this:
--------------------
if crash car, car2
--------------------
It detects a collision with 2 cars... U guys understand?
Oluseyi
Oluseyi
quote:
Original post by -Fruz-
Oops... I forgot something! I wan''t this code to work with different pictures... C?

a.) Stop using single letters as substitutes for words. It is neither "cool" nor does it save you keystrokes to type "C" instead of "see", "2" instead of "to"/"too", etc. Furthermore, it confuses the situation here since "C" is the name of a programming language.

b.) To answer your question, you need to define a data structure that represents any movable object - the Object parameter type in responses above. This Object type would need to have the properties Left, Right, Top and Bottom. It would also have some form of Picture property, which would be the image you would see.

c.) The syntax would be more If Crash(Car1, Car2) Then ... The parentheses are required in VB (a very regimented language per code formatting).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
-Fruz-
-Fruz-
This is what i ended up with (To test it):
----------------------------------------------------------------
Function Crash(O1 As Object, O2 As Object) As Boolean
If O1.Left >= O2.Left - O1.Width _
And O1.Left <= O2.Left + O2.Width _
And O1.Top >= O2.Top - O1.Height _
And O1.Top <= O2.Top + O2.Height Then
Crash = True
Else
Crash = False
End If
End Function

Private Sub Timer1_Timer()
If Crash(object1, object2) = True Then
Label1.Caption = "Crash"
Else
Label1.Caption = ""
End If
End Sub
----------------------------------------------------------------
I''m really happy with this solution because it''s the easiest way (in my idea). If not then you tell me if there is any simpler and faster way to detect collision, cause I can''t imagine there is any...

Thanx for your help people!

JFK
hewhay
hewhay
quote:

It''s rather fascinating that you didn''t notice he wasn''t programming in a C-derivative.


My mistake, for a moment i understood that he wanted to change his code to a c function. next time i should read more carefully
quote:

hmm what do the square brackets mean?? i didnt know yuo could do that in c++


you cant, i was using a common syntax for optional code
humanity will always be slaved by its ignorance

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.