Collision

reepnorp
01-16-2005, 05:58 PM
With this code, the collision only seems to work sometimes. Anyone know why?


If intShipOneLeft >= intShipTwoLeft And intShipOneLeft <= intShipTwoRight Then
'If the left variables are either the same, or the first ship's is higher; and the
'first ships left variable is either smaller than, or equal to the second' right
blnLeftBetween = True 'The first ship's left side is hitting the second's right
blnRightBetweenTwo = True 'The second ship's right side is hitting the first's left
Else: blnLeftBetween = False 'Otherwise they aren't
blnRightBetweenTwo = False
End If
If intShipOneRight >= intShipTwoRight And intShipOneRight <= intShipTwoRight Then
'If the right variables are either the same, or the first ship's is higher; and the
'first ships right variable is either smaller than, or equal to the second's left
blnRightBetween = True 'The first ship's right side is hitting the second's left
blnLeftBetweenTwo = True 'The second ship's left side is hitting the first's right
Else: blnRightBetween = False 'Otherwise they aren't
blnLeftBetweenTwo = False
End If
If intShipOneTop >= intShipTwoTop And intShipOneTop <= intShipTwoBottom Then
'If the top variables are either the same, or the first ship's is higher; and the
'first ships top variable is either smaller than, or equal to the second's bottom
blnTopBetween = True 'The first ship's top is hitting the second's bottom
blnBottomBetweenTwo = True 'The second ship's bottom is hitting the first's top
Else: blnTopBetween = False 'Otherwise they aren't
blnBottomBetweenTwo = False
End If
If intShipOneBottom >= intShipTwoBottom And intShipOneBottom <= intShipTwoBottom Then
'If the bottom variables are either the same, or the first ship's is higher; and
'the first ships bottom variable is either smaller than, or equal to the second's top
blnBottomBetween = True 'The first ship's bottom is hitting the second's top
blnTopBetweenTwo = True 'The second ship's top is hitting the first's bottom
Else: blnBottomBetween = False 'Otherwise they aren't
blnTopBetweenTwo = False
End If

webbone
01-16-2005, 06:28 PM
Your second If statement only is 'true' when the right edges are the same. Remember that you need to be checking for:

L2 <= L1 <= R2

L2 <= R1 <= R2

If either of the above are true then you are 'horizontally coherent'. Similar rules apply for Up/Down checks.

Notice that the above equations are checking a single position (left OR right edge) of item 1 with BOTH edges (left AND RIGHT) of item 2.

Also remember that in order for a collision to occur you need to have both 'horizontally coherent' and 'vertically coherent' to be TRUE.

reepnorp
01-16-2005, 07:43 PM
So how would I change it to make it work? I can sort of see whats wrong, but I just can't figure out the logic right now.

webbone
01-16-2005, 08:37 PM
Well your second If statement is the first problem you've got:


'this is your original, which is wrong
If intShipOneRight >= intShipTwoRight And intShipOneRight <= intShipTwoRight Then


'this is what it needs to be, based on the example concept I posted before
If intShipOneRight >= intShipTwoLeft And intShipOneRight <= intShipTwoRight Then


The same sort of change would apply to your 4th IF statement as well.

reepnorp
01-17-2005, 04:53 AM
Okay, I tried that. Now it seems to work, unless object one is running into the second from either the bottom or right. Any ideas why? Also, it only works for the top if the leftmost bottom corner is overlapping the top of the second, and same conditions but for the right side.

webbone
01-17-2005, 08:22 PM
Okay, I tried that. Now it seems to work, unless object one is running into the second from either the bottom or right. Any ideas why? Also, it only works for the top if the leftmost bottom corner is overlapping the top of the second, and same conditions but for the right side.
You'll need to repost your adjusted code otherwise any guesses would be just that...

Also include your code that interprets your Boolean values that the IF blocks are creating - you might have a similar issue there as you did in the IF blocks.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum