 |

07-20-2003, 10:04 AM
|
|
Newcomer
|
|
Join Date: Jul 2003
Posts: 2
|
|
pics collision
|
i have 2 pics in my form.
now tell me what wrong with this code.
sometimes the pics bounch each other and sometimes not.
when i detect collision each pic is moving to other direction.
i detect 4 side collision (left.right,top, bottom)
sometimes its work and sometimes not.
heres the code:
' right side collision
ElseIf img1.Top + img1.Height >= img2.Top And img1.Top <= _
img2.Top + img2.Height And img1.Left + img1.Width >= _
img2.Left And img1.Left + img1.Width <= img2.Left + 30 Then
direction = 2
xMotion = -20
direction2 = 1
xMotion2 = 20
MsgBox "right"
' left side collision
ElseIf img1.Top + img1.Height >= img2.Top And img1.Top <= _
img2.Top + img2.Height And img1.Left <= img2.Left + img2. _
Width And img1.Left >= img2.Left + img2.Width - 30 Then
direction = 1
xMotion = 20
direction2 = 2
xMotion2 = -20
MsgBox "left"
'Top collision
ElseIf img1.Left + img1.Width >= img2.Left And img1.Left <= _
img2.Left + img2.Width And img1.Top + img1.Height >= img2.Top _
And img1.Top + img1.Height <= img2.Top + 30 Then
direction = 4
yMotion = -20
direction2 = 3
yMotion2 = 20
MsgBox "top"
'down collision
ElseIf img1.Left + img1.Width >= img2.Left And img1.Left <= _
img2.Left + img2.Width And img1.Top <= img2.Top + img2. _
Height And img1.Top >= img2.Top + img2.Height - 30 Then
direction = 3
yMotion = 20
direction2 = 4
yMotion2 = -20
MsgBox "down"
End If
|
|

07-20-2003, 12:24 PM
|
 |
Junior Contributor
|
|
Join Date: Jul 2003
Location: Finland
Posts: 238
|
|
Hi,
Well, I don't quite understand all of your code (everything you want it to
do). You have a pretty good way to check for collisions The above code is very good, but i (think) i found some errors:
'right side collision
img2.Left And img1.Left + img1.Width <= img2.Left + 30 Then
I think you forgot the img2.left + img2.width, since it's the right side
this is also on the other sides
To determine the way the img's should bounce is difficult, atleast for me. I'm not very good at physics, and this sounds like a big problem  It depends on what you want the game to do
|
__________________
To boldly go where every other programmer has gone before
|

07-20-2003, 01:29 PM
|
 |
Ultimate Contributor
* Expert *
|
|
Join Date: Jun 2003
Location: New York, NY
Posts: 1,929
|
|
The easiest way I've found to do collision detection between 2 image boxes is this:
check all the points of the first image to see if they are inside the bounds of the second image.
check all the points of the second image to see if they are inside the bounds of the first image.
Write a RectangularCollision function to do this so you don't have to re-write it everytime.
Now you should be able to do something like this:
Code:
if RectangularCollision(img1.left, img1.top, img1.width, img1.height, _
img2.left, img2.top, img2.width, img2.height) then _
msgbox "Objects are currently collided"
'Check to see if img1 can be moved left/right
if RectangularCollision(img1.left[b] + MovementX[/b], img1.top, img1.width, img1.height, _
img2.left, img2.top, img2.width, img2.height) then _
msgbox "Objects will collide"
'Check to see if img1 can be moved up/down
if RectangularCollision(img1.left, img1.top[b] + MovementY[/b], img1.width, img1.height, _
img2.left, img2.top, img2.width, img2.height) then _
msgbox "Objects will collide"
|
__________________
"Fortunately, I live in the United States of America, where we are gradually coming to understand that nothing we do is ever our fault, especially if it is really stupid." - Dave Barry
|

07-20-2003, 01:44 PM
|
|
Restricted
|
|
Join Date: Mar 2003
Location: Manchester! England!
Posts: 872
|
|
why dont you just use the intersect rect api, its so easy!
firstly call the API
Code:
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function IntersectRect Lib "user32" Alias "IntersectRect" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
then you need to give the shapes the rect.
the tuturiol is www.rookscape.com/vbgaming
anymore help just ask
|
|

07-22-2003, 07:31 AM
|
|
Newcomer
|
|
Join Date: May 2003
Posts: 4
|
|
Collision
|
Just a minor point, but I take it when the objects collide they reverse direction.
This is great for nice large objects, but something to watch out for is if the object is capable of moving at a speed of over a quarter of its size then it is possible for two objects travel in opposite directions to jump past the half way point and hence throw collisions against the wrong side - causing them to do very strange things.
|
|

07-22-2003, 11:43 AM
|
 |
Ultimate Contributor
* Expert *
|
|
Join Date: Jun 2003
Location: New York, NY
Posts: 1,929
|
|
Quote: Originally Posted by scottjohnston Just a minor point, but I take it when the objects collide they reverse direction.
This is great for nice large objects, but something to watch out for is if the object is capable of moving at a speed of over a quarter of its size then it is possible for two objects travel in opposite directions to jump past the half way point and hence throw collisions against the wrong side - causing them to do very strange things.
What I would do is first off, I try never to move an object move than half of it's size at a time, and secondly if you move it a great distance, you need to check several collision points along it's path rather than just checking the destination for collision.
|
__________________
"Fortunately, I live in the United States of America, where we are gradually coming to understand that nothing we do is ever our fault, especially if it is really stupid." - Dave Barry
|

07-24-2003, 07:05 AM
|
 |
Junior Contributor
|
|
Join Date: Jul 2003
Location: Finland
Posts: 238
|
|
|
Hmmm... how about using a "tester"?
I always check my collisions by having a tester
walk in the direction you want to go, for example
dim tX, ty 'integers....
ty = playery: tx = playerx
ty = ty + 1 'walk down
'check for collision
if collision = false then
playerx = tx: playery = ty
end if
|
__________________
To boldly go where every other programmer has gone before
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|