Vb6 Tutorial- Rectangle Collision Detection

  • Uploaded by: Timothy Morris
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Vb6 Tutorial- Rectangle Collision Detection as PDF for free.

More details

  • Words: 724
  • Pages: 4
Vb6 Tutorial: Rectangular Collision Detection, by Timothy Morris. This tutorial will teach you how to use collision detection in Visual Basic 6. To find out if two rectangles have collided, first we need to check 4 things: 1. 2. 3. 4.

If the right side of rectangle 1 is greater than the left side of rectangle 2 If the left side of rectangle 1 is less than the right side of rectangle 2 If the bottom side of rectangle 1 is greater than the top side of rectangle 2 If the top side of rectangle 1 is less than the bottom side of rectangle 2

In this example I will use two shapes as my rectangles. To find the right side of a rectangle, we need to add the width of the rectangle to the left value of the rectangle. For example: Right = Shape1.Left + Shape1.Width We also use this method to find the bottom of the rectangle: Bottom = Shape1.Top + Shape1.Height Now we need to do the math. First, find if the right of shape 1 is greater than the left side of shape 2: If (Shape1.Left + Shape1.Width) > Shape2.Left... Now, if the left of shape 1 is less than the right of shape 2: If Shape1.Left < (Shape2.Left + Shape2.Width)... Now, use the same method for top and bottom sides: If (Shape1.Top + Shape1.Height) > Shape2.Top... If Shape1.Top < (Shape2.Top + Shape2.Height)... Put all this together and we get: If (Shape1.Left + Shape1.Width) > Shape2.Left AND Shape1.Left < (Shape2.Left + Shape2.Width) AND (Shape1.Top + Shape1.Height) > Shape2.Top AND Shape1.Top < (Shape2.Top + Shape2.Height) then... COLLISION!

Another way to do this is to call the API IntersectRect. We do this by first creating a type called RECT. This is done by starting your code with: Option Explicit Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type This creates the type called RECT. RECT has properties Left, Right, Top and Bottom. You can create a new RECT by typing: Dim as RECT For example: Dim rect1 as RECT To change the properties of rect1, for example, the top property, we type: rect1.top = 5 We then add below: Private Declare Function IntersectRect Lib "user32" Alias "IntersectRect" _ (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long

This declares the Function IntersectRect so we can use it later. Before we call the function IntersectRect, we first need to create a variable of variable type Boolean. A Boolean can be used ONLY to store the value TRUE of FALSE. To do this, type:

Dim as Boolean

Then, we type:

Collision = IntersectRect (, , )

IntersectRect works by taking the properties of and and comparing them to find the intersection. It does all the math for you. It then returns the intersection as a RECT into . It also returns if there was an intersection as a TRUE or FALSE value into RectCollision.

An example is:

Dim Rect1 as RECT Rect1.left = 5 Rect1.right = 10 Rect1.top = 5 Rect1.bottom = 10 Dim Rect2 as RECT Rect2.left = 7 Rect2.right = 12 Rect2.top = 7 Rect2.bottom = 12 Dim Collision as Boolean Dim Rect3 as RECT Collision = IntersectRect (Rect3, Rect1, Rect2)

Intersect will work out the intersection between Rect1 and Rect2 and returns it in Rect3. Rect3 will become the rectangle where it was intersected. So Rect3 will have: Left – 7 Top – 7 Right – 10 Bottom – 10 Also, Collision will become TRUE because there was an intersection. To sum it all up, I will show you how to use this to find an intersection between two shapes:

Dim Rect1 as RECT Dim Rect2 as RECT Dim Rect3 as RECT

‘Also Note that you can write this as Dim Rect1, Rect2, Rect3 as RECT Rect1.Left = Shape1.Left Rect1.Top = Shape1.Top Rect1.Right = Shape1.Left + Shape1.Width Rect1.Bottom = Shape1.Top + Shape1.Height Rect2.Left = Shape2.Left Rect2.Top = Shape2.Top Rect2.Right = Shape2.Left + Shape2.Width Rect2.Bottom = Shape2.Top + Shape2.Height Dim Collision as Boolean Collision = IntersectRect(Rect3, Rect1, Rect2)

If there is an intersection, Collision will be TRUE, else it will be FALSE. In this case, because there was an intersection, Collision will be TRUE. Both theses ways can be used to find Rectangle Collision Detection. Use whichever you find easiest.

Related Documents

Collision Detection
November 2019 22
Precise Collision Detection
November 2019 14
Vb6
October 2019 20
Vb6
November 2019 14

More Documents from ""