Collisions between 2 of the same type

BlitzPlus Forums/BlitzPlus Programming/Collisions between 2 of the same type

Kyler(Posted 2007) [#1]
Can somebody please write me a sample code showing how check a collision between 2 objects of the SAME type? Because this doesn't work...

If imagescollide(im,a\x,a\y,0,im,a\x,a\y,0)


Ked(Posted 2007) [#2]
I'm not 100% sure but I think you would do this:
For a.apple=each apple
For a2.apple=each apple
     If ImagesCollide(a\img,a\x,a\y,0,a2\img,a2\x,a2\y)
          ;dsfjdslvjdsfkljdsgds;
     Endif
next
next

That's how I would do it anyway.


Kyler(Posted 2007) [#3]
ok, say i wanted to check the collision between more than just two objects, say 100. then how would i do that?


Matty(Posted 2007) [#4]
Ked's code will work with more than 2 objects, in fact it works for all objects currently in memory, however it does not discount two objects that are one and the same object.

Here is another way of doing it:

For Apple.AppleObject=each AppleObject
     For OtherApple.AppleObject=each AppleObject
            If Handle(Apple)<>Handle(OtherApple) then 
                       If ImagesCollide(Apple\Image,Apple\X,Apple\Y,Apple\Frame,OtherApple\Image,OtherApple\X,OtherApple\Y,OtherApple\Frame) then
                              ;do whatever you want now that you've detected a collision
                      endif 
            endif 
     next
next



This is the easiest way of doing things, and if you are only comparing 100 objects against all 100 other objects every frame (roughly 10000 imagescollide checks) then it may be quick enough for what you want.


Kyler(Posted 2007) [#5]
will this code work if i wanted to make a bunch of particles, all the same type, to move around and bump into eachother?


Matty(Posted 2007) [#6]
Don't see why not.


Kyler(Posted 2007) [#7]
What does the command "Handle" do? It,s not in the command reference.


Matty(Posted 2007) [#8]
Handle & its related partner Object are two commands not documented as official blitz commands.

I will try to explain by giving you an example, there are many uses for this command the main one being though the ability to go directly to a particular type instance without having to loop through them all.



Type AppleObject

Field Ripeness
Field Taste
Field Color
Field Name$

end type


Apple.AppleObject=new AppleObject
Apple\NAme="jonathan"

AppleHandle=Handle(Apple)
;The Handle(Apple) command returns an integer which holds a kind of pointer to this particular type instance.


SecondApple.AppleObject=new AppleObject
SecondApple\Name="granny smith"


TheFirstApple.AppleObject=Object.AppleObject(AppleHandle)
;the Object command when used as above will return either the relevant type instance as it does here, or null if it does not exist.


Print TheFirstApple\Name;should print "jonathan"



The way I used it in the code earlier (a few posts up) allows us to check if we are looking at the same type instance in each loop - if the handle returned is the same for both then we are looking at the same type instance.

It may take some time for this command's usefulness to become apparent, and you may need to play with it a little so don't worry if you don't get it just yet.


Kyler(Posted 2007) [#9]
Ok, I have one more question. I know i should already know this, but I don't. When I create a type, I put in something like,

" appleobject.appleobject = new appleobject"

I don't know why I put it. That's just how I do it. Could somebody explain what this command means? I usually see it done like this,

"apple.appleobject = new appleobject"

What's the difference?


Matty(Posted 2007) [#10]
Not too much difference at all -

as an example, when you declare a variable as a string you do so by adding $ to the end of the variable name, when you declare a floating point variable you add #. In the case of a custom type you add .TYPENAME when declaring the variable.

There is nothing wrong with doing it your way but it can be a little confusing.

Examples:

A.Appleobject=new AppleObject

YetAnotherInstance.AppleObject=new AppleObject

CDEF.AppleObject=new AppleObject



All of those would create a new instance of a type object, the first being called A, the second being called YetAnotherInstance, and the third being called CDEF. The name you give to the type instance really does not matter.

You don't have to name a new type instance as the name of the type itself


Kyler(Posted 2007) [#11]
Thanks Matty!


Zauron(Posted 2008) [#12]
FYI, the Handle function is great and useful for some advanced tricks, but it is completely unnecessary in this example. You can compare two Type instances to see if they are the same one or not without using the Handle command.

In other words, this code:

For Apple.AppleObject=each AppleObject
    For OtherApple.AppleObject=each AppleObject
        If Apple <> OtherApple then 
            If ImagesCollide(Apple\Image,Apple\X,Apple\Y,Apple\Frame,OtherApple\Image,OtherApple\X,OtherApple\Y,OtherApple\Frame) then
                ;do whatever you want now that you've detected a collision
            endif 
        endif 
    next
next


will work exactly the same as the code Matty first posted (notice the lack of the Handle function calls).

Note that this actually does make sure it is the exact same or different object by comparing their memory addresses internally, it does not compare the CONTENTS of the objects.

For example, this code:

Type TestType
    Field x, y
End Type

a.TestType = New TestType
b.TestType = New TestType

a\x = 10
b\x = 10
a\y = 10
b\y = 10

If a = b Then
    Print "The same!"
Else
    Print "Not the same!"
EndIf


will print out "Not the same!" even though the contents of the two objects are the same. Using = or <> between two Type instances will only check if they are the exact same instance of the object, it doesn't bother to look at the data inside them. If you wanted to see if their values are the same, you'd have to manually check x and y of each one.


Kyler(Posted 2008) [#13]
this code:

For Apple.AppleObject=each AppleObject
For OtherApple.AppleObject=each AppleObject
If Apple <> OtherApple then
If ImagesCollide(Apple\Image,Apple\X,Apple\Y,Apple\Frame,OtherApple\Image,OtherApple\X,OtherApple\Y,OtherApple\Frame) then
;do whatever you want now that you've detected a collision
endif
endif
next
next

(sorry, dont know how to make one of those code boxes)

It only works if I want 2 instances to collide with eachother...But say I want 100s! without doing this...

b1.ball = new ball
b2.ball = new ball
b3.ball = new ball
b4.ball = new ball
b5.ball = new ball
b6.ball = new ball
b7.ball = new ball
b8.ball = new ball
b9.ball = new ball
etc....


Thats not what I want...