Interesting problem with collision

BlitzMax Forums/BlitzMax Beginners Area/Interesting problem with collision

Pavlov(Posted 2005) [#1]
Object A collides with object B. I believe I'm detected the collision the correct way and am removing object B from its list but would also like to remove object A from its list. Any ideas?

' go through all the attacking missiles looking for a collided image
For Local A:AttackingMissile = EachIn AttackingMissilelst
CollideImage(A.anImage, A.x, A.y, 0, 0, 1)
Next

' attacking missile collides with a city
For Local B:Cities = EachIn CitiesLst

If CollideImage(B.anImage, B.x, B.y, 0, 1, 0)
PlaySound nukeSnd
AddExplosion(B.x, B.y, 1)
B.target = 0
End If
Next


taxlerendiosk(Posted 2005) [#2]
You're not using the extra argument that lets you pass an object to the collision function that you then retrieve in an Object array when something else collides with it. (Remember you'll have to downcast it.)


Pavlov(Posted 2005) [#3]
Object A keeps coming back as null. Feels like I've run through the gauntlet of what it could possibly be. Any suggestions would be greatful. Thanks.

' go through all the attacking missiles looking for a collided image
For Local A:AttackingMissile = EachIn AttackingMissileLst
CollideImage(A.anImage, A.x, A.y, 0, 0, 1, A)
Next

' attacking missile collides with a city
For Local B:Cities = EachIn CitiesLst

Local collided:Object[] = CollideImage(B.anImage, B.x, B.y, 0, 1, 0, Null)

If collided
For Local i = 0 To Len(Collided) - 1
Local A:AttackingMissile = AttackingMissile(Collided[i])
If a = Null
Print "it's null null"
End If
AttackingMissileLst.remove(A)
Next
PlaySound nukeSnd
AddExplosion(B.x, B.y, 1)
B.target = 0
End If
Next


taxlerendiosk(Posted 2005) [#4]
Hmm... sorry, as far as I can tell that should work. By the way, if the cities remain stationary and the missiles are moving around the screen, it might make more sense to draw the cities to a collision layer not the missiles, and compare the missiles to it. Then you only need to clear and redraw the city layer when a city is destroyed rather than every frame, which I think could make it faster.

However, I might have no idea what I'm talking about.


Pavlov(Posted 2005) [#5]
I fixed it with some help from a program called rockout.bmx that came with blitzmax. I put the following code in the attacking missiles update method and it worked like a charm. Life is good. Do you think I found a bug based on the source code listed above?

For Local City:Cities = EachIn CitiesLst

Local ox:Int = x - width / 2
Local oy:Int = y - height / 2

Local ogx:Int = City.x - City.width / 2
Local ogy:Int = City.y - City.width / 2

' Check collision...

If OverLap (ox, oy, ox + width, oy + height, ogx, ogy, ogx + City.width, ogy + City.height)
AddExplosion(City.x, City.y, 1)
PlaySound nukeSnd
City.target = 0
AttackingMissileLst.remove(Self)
End If
Next