| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <html>
- <head>
- <title>Blitz3D Docs</title>
- <link rel=stylesheet href=../css/commands.css type=text/css>
- </head>
- <body>
- <h1>ImagesCollide (image1,x1,y1,frame1,image2,x2,y2,frame2)</h1>
- <h1>Parameters</h1>
- <table>
- <tr>
- <td>
- image1 - first image to test
<br />
- x1 - image1's x location
<br />
- y1 - image1's y location
<br />
- frame1 - image1's frame to test (optional)
<br />
- <br />
- image2 - second image to test
<br />
- x2 - image2's x location
<br />
- y2 - image2's y location
<br />
- frame2 - image2's frame to test (optional)
- </td>
- </tr>
- </table>
- <h1>Description</h1>
- <table>
- <tr>
- <td>
- This is THE command to get pixel-perfect collisions between images. It will not consider transparent pixels during the collision check (basically, only the 'meat' of the image will invoke a collision). This makes it perfect for most situations where you have odd-shaped graphics to text against.
<br />
-
<br />
- The ImagesOverlap command is MUCH faster, however, but can only determine if ANY of the two images have overlapped (this INCLUDES transparent pixels). This method works if you have graphics that completely fill their container and/or you don't plan on needing pinpoint accuracy.
<br />
-
<br />
- As with any collision detection system in Blitz, you will need to know the variable names of the two images, and their X and Y locations at the moment collision checking occurs.
<br />
-
<br />
- The example blatently uses graphics that are much smaller than their container to show you how accurate this command really is. The ImagesOverlap example is identical to this one - and shows how inaccurate the overlapping method can be with graphics of this nature.
- </td>
- </tr>
- </table>
- <h1><a href=../2d_examples/ImagesCollide.bb>Example</a></h1>
- <table>
- <tr>
- <td>
- ; ImagesCollide Example
<br />
-
<br />
- ; Turn on graphics mode
<br />
- Graphics 640,480,16
<br />
-
<br />
- ; Create two new empty graphics to store our circle and box in
<br />
- gfxBox=CreateImage(50,50)
<br />
- gfxCircle=CreateImage(50,50)
<br />
-
<br />
- ; Draw the box image first
<br />
- ; Set drawing operations to point to our new empty graphic
<br />
- SetBuffer ImageBuffer(gfxBox)
<br />
- ; Change drawing color to blue
<br />
- Color 0,0,255
<br />
- ;Draw our box (note that it has a 10 pixel space around it)
<br />
- Rect 10,10,30,30,1
<br />
-
<br />
- ; Repeat for the circle graphic
<br />
- SetBuffer ImageBuffer(gfxCircle)
<br />
- Color 255,0,0
<br />
- ; Note the extra space between the circle and the edge of the graphic
<br />
- Oval 10,10,30,30,1
<br />
-
<br />
- ; Let's not forget to put the drawing buffer back!
<br />
- SetBuffer BackBuffer()
<br />
-
<br />
- ; Locate our box to a random, visible screen location
<br />
- boxX=Rnd(50,610)
<br />
- boxY=Rnd(50,430)
<br />
-
<br />
- ; Repeat the loop until we've had a collision
<br />
- Repeat
<br />
- ; Attach our mouse to the circle to move it
<br />
- circleX=MouseX()
<br />
- circleY=MouseY()
<br />
- ; Standard double buffer technique; clear screen first
<br />
- Cls
<br />
- ; Draw our objects at the designated location
<br />
- DrawImage gfxBox,boxX,boxY
<br />
- DrawImage gfxCircle,circleX,circleY
<br />
- ; Standard double buffer technique; flip after all drawing is done
<br />
- Flip
<br />
- ; We test the locations of our box and circle to see if they have pixel collided
<br />
- Until ImagesCollide (gfxBox,boxX,boxY,0,gfxCircle,circleX,circleY,0)
<br />
-
<br />
- ; Loop is over, we must've collided!
<br />
- Text 0,0, "WE'VE HAD A COLLISION! PRESS A MOUSE BUTTON"
<br />
- ; Can't see the text until we flip ..
<br />
- Flip
<br />
- ; Wait for a mouse click
<br />
- WaitMouse()
<br />
- ; End our graphics mode
<br />
- EndGraphics
- </td>
- </tr>
- </table>
- <br>
- <a target=_top href=../index.htm>Index</a><br>
- <br>
- Click <a href=http://www.blitzbasic.co.nz/b3ddocs/command.php?name=ImagesCollide&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
- </html>
|