Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

images

Status
Not open for further replies.

gladiator5

Programmer
Joined
Jul 11, 2003
Messages
4
Location
US
How do I have a timer make an image invissable when another image is in image1's width range. I want it so when image2 is in image1's width range it dissapears. Bassicly I want a image to disappear when it touches another image anywhere. No this is not a homework assignment.
 
Perhaps you can explain the background to this question to help us provide the most relevant solution.
 
i think gladiator5 is seeking a collision detection algo. am i right???
 
I often use Regions for collision detection
 
For all your API stuff try:

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
johnwm, why you post the reference of API help in this thread?I am still thinking....

for gladiator5: a code to do this.

If image1.Left + image1.Width >= image2.Left _
And image1.Left <= image2.Left + image2.Width _
And image1.Top <= image2.Top + image2.Height _
And image1.Top <= image2.Top + image2.Height _
Then
image1.Visible = False
Else
image1.Visible = True
End If

trying to get more simplifed code.
 
gladiator, if you plan to use picture boxes to hold your images, then Rectangle API functions will be very useful to you. You will perform the following steps to detect a collision.
1. Get the window rectangles of the two PBs.
2. Intersect the two rectangles.
3. If the resultant rectangle is not empty then the two PBs collide.

Here is a sample program to demonstrate this.
1. Start a new project.
2. Place a picture box on the form and set its Index = 0.
3. Place a timer on the form and set its Interval = 100.
4. Place the following code in the form.
___

Private Declare Function GetWindowRect Lib &quot;user32&quot; (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function IntersectRect Lib &quot;user32&quot; (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
Private Declare Function IsRectEmpty Lib &quot;user32&quot; (lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Load()
Load Picture1(1)
Picture1(0).Move 1000, 1000, 1000, 1000
Picture1(1).Move 2500, 1000, 1000, 1000
Picture1(1).Visible = True
WindowState = vbMaximized
End Sub
Private Sub Picture1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
With Picture1(Index)
.CurrentX = X
.CurrentY = Y
.ZOrder
End With
End Sub
Private Sub Picture1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
With Picture1(Index)
.Move .Left + X - .CurrentX, .Top + Y - .CurrentY
End With
End If
End Sub
Private Sub Timer1_Timer()
Dim R0 As RECT, R1 As RECT, R As RECT
Dim Collision As Boolean
GetWindowRect Picture1(0).hwnd, R0
GetWindowRect Picture1(1).hwnd, R1
IntersectRect R, R0, R1
Collision = IsRectEmpty(R) = 0
If Collision Then
Picture1(0).BackColor = vbRed
Picture1(1).BackColor = vbRed
Else
Picture1(0).BackColor = vbGreen
Picture1(1).BackColor = vbGreen
End If
End Sub
___

Run the program, you will see two green boxes (PBs) on your form. Drag them using the mouse around the form. As they cross the border of each other, they will turn red indicating a collision. As they are moved apart they will again turn green.

Read the code in Sub Timer1_Timer carefully. This is where the collision detection is being performed.
 
Yep, essentially the same as my (unposted) Region solution, which uses CombineRegion to get the same sort of result as IntersectRect. I prefer Regions because they can be more-or-less any shape you like.
 
In fact someone dug out a reference to my old Regions code, so here it is the link: Thread222-334861
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top