Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Game Programming > pics collision


Reply
 
Thread Tools Display Modes
  #1  
Old 07-20-2003, 10:04 AM
yhaim yhaim is offline
Newcomer
 
Join Date: Jul 2003
Posts: 2
Default 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
Reply With Quote
  #2  
Old 07-20-2003, 12:24 PM
Boris's Avatar
Boris Boris is offline
Junior Contributor
 
Join Date: Jul 2003
Location: Finland
Posts: 238
Default

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
Reply With Quote
  #3  
Old 07-20-2003, 01:29 PM
blindwig's Avatar
blindwig blindwig is offline
Ultimate Contributor

* Expert *
 
Join Date: Jun 2003
Location: New York, NY
Posts: 1,929
Default

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
Reply With Quote
  #4  
Old 07-20-2003, 01:44 PM
Squidge Squidge is offline
Restricted
 
Join Date: Mar 2003
Location: Manchester! England!
Posts: 872
Default

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
Reply With Quote
  #5  
Old 07-22-2003, 07:31 AM
scottjohnston scottjohnston is offline
Newcomer
 
Join Date: May 2003
Posts: 4
Default 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.
Reply With Quote
  #6  
Old 07-22-2003, 11:43 AM
blindwig's Avatar
blindwig blindwig is offline
Ultimate Contributor

* Expert *
 
Join Date: Jun 2003
Location: New York, NY
Posts: 1,929
Default

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
Reply With Quote
  #7  
Old 07-24-2003, 07:05 AM
Boris's Avatar
Boris Boris is offline
Junior Contributor
 
Join Date: Jul 2003
Location: Finland
Posts: 238
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Pixel perfect collision... I really need help Kamochan DirectX 4 07-22-2003 07:40 AM
Unique Filenames for Adding & Deleting Pics gonzo General 6 04-01-2003 11:28 PM
I need some expert help. Twan Game Programming 3 03-01-2003 08:02 PM
Possible solution to collision detection. . . MFCTC Game Programming 3 05-13-2002 08:47 AM
How do I link(match) pics to a table in access, please help! espenskaug Word, PowerPoint, Outlook, and Other Office Products 3 02-03-2002 12:10 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->