ImagesCollide

BlitzMax Forums/BlitzMax Beginners Area/ImagesCollide

(tu) ENAY(Posted 2005) [#1]
Argh. Why can't I figure out how to use ImagesCollide?

Ok so it's :-

ImagesCollide(TImage1,x,y,frame1,TImage2,x,y,frame2)

I don't get why you need you need frame1 and frame2.

In Regular Blitz i just use (Image1,x,y,Image2,x,y) and everything works. If I don't specify a frame then I get errors and when I fill in 0 for the 2 frames I just don't get any results at all. Nothing happens in game at all.
What am I doing wrong?

HELP! :)


tonyg(Posted 2005) [#2]
In regular blitz you'd get an error 'not enough parameters' if you didn't specify a frame with imagescollide.
This works in Bmax...
Graphics 640,480
image1:TImage=CreateImage(128,128)
image2:Timage=CreateImage(128,128)
SetColor 255,0,0
DrawRect 0,0,128,128
GrabImage(image1,0,0)
Cls
SetColor 0,0,255
DrawOval (0,0,128,128)
GrabImage(image2,0,0)
SetColor 255,255,255
While Not KeyHit(key_escape)
  Cls
  DrawImage image1,100,100
  DrawImage image2,MouseX(),MouseY()
  If ImagesCollide(image1,100,100,0,image2,MouseX(),MouseY(),0) DrawText "Collided",0,0
  Flip
Wend
End

Can you show some code?
<edited to include generated images>


Panno(Posted 2005) [#3]
hi ;
yap this works but i must check allframes from a image if i dont know which frame is plotted ...
hmm need a better (faster way) ...


skidracer(Posted 2005) [#4]
For a fast version, will hopefully document soon, there is some source here:

http://www.blitzbasic.com/logs/userlog.php?user=5&log=491

It is not the simplest method to do fast collisions which would be to call CollideImage with every DrawImage to build a collision map of the scene while you're rendering it. The game's update loop can then collide everything quickly with the collision maps created in the previous flip.


tonyg(Posted 2005) [#5]
I might be missing something here.
Why would you have to check all frames from an image?
If you're displaying an image with multiple frames then you'd have to use something like a 'curframe' variable to display the frame you want... wouldn't you? This is the frame you check the collisions with.
As in BB, for multiple enemies, couldn't you check collisions for all enemies on a list by looping?
Having said that I haven't really checked collision layers much yet.


RiK(Posted 2005) [#6]
This *really* needs documenting properly.

I'm pretty sure I know what it's doing and I've got it working in a simple example but aproperly documented example would be a great help.


LarsG(Posted 2005) [#7]
I agree... more documentation on the subject (if possible, some illustrations would be nice too) is needed...


GfK(Posted 2006) [#8]
Realise this is an old thread, but I'm damned if I can get ImagesCollide to return anything other than 0.

The documentation appears to be as useless as it was eight months ago when this thread was started. :/

	If ImagesCollide(imgMouse,MouseX(),MouseY(),0,tiles,xstart,ystart,0)
		DrawText "Collided!",0,0
	EndIf


Why doesn't this work???


Grey Alien(Posted 2006) [#9]
It looks like it should work accoring to the docs. I haven't used that one yet; been using CollideImage instead which DOES work. Try making a mini app and post an example for us to test. Or just force a collision by changing your mouseX and Y and startX and Y to 0 and making 100% sure there is some graphics at the same position in your tiles image.


GfK(Posted 2006) [#10]
I found CollideImage but the documentation doesn't make any sense. What's a collision layer??? How do I use the Writemask and Collisionmask parameters? Come on, who wrote this nonsense??

I'm starting to despise Blitzmax. :/


Dreamora(Posted 2006) [#11]
CollideImage writes the image to a layer (you have 16 of them). To which layer an image is written, is defined through the writemask.

If you want to check if an image collides with anything, you do that basing on the layers as well. But this time you have to write the needed layer flag to the collision mask.

Quite simple :-)

Don't forget to call resetcollisions after you check all collisions.


(tu) ENAY(Posted 2006) [#12]
Heheh, man those were the days, 8 months ago too!
I never did quite figure why ImagesCollide didn't work.

In the end I gave up and since then have been using my own bounding box and circle collision code that I wrote ever since. If that's making you pull your hair out then you should have a tinker with drawrect and then try to resist not making a '4 line inside a function' MyDrawRect function ;)

> I'm starting to despise Blitzmax. :/

Don't despair Dave! It sorta gets better later, I've had 12 months of crazy Blitzmax things to deal with.

Besides, just wait until you start putting sound in your game.....


tonyg(Posted 2006) [#13]
@Gfk, Does that example I posted with ImagesCollide work?
If it does then post your code and somebody will see what's happening.
There are lots of good examples on how collideimage is used hidden away. Shame some of them couldn't have been used in the doc.
e.g.
Collideimage1
Wiki Collideimage entry
Collideimage wrapper
Extra doc which doesn't seem to have made it


Grey Alien(Posted 2006) [#14]
The thing with the CollideImage is to look very carefully at the flags in the help example. The first call "Writes" to layer 1 and the second call "Checks" the specified object against layer 1. It's quite cool in a way actually.


GfK(Posted 2006) [#15]
Well, I think the original problem was something to do with the image I was using. I re-exported it from Photoshop, and everything works fine now.

I've since bumped into a mouse lag problem, which after a dig through the forums I've managed to work around (GrabPixmap), but that REALLY needs fixed. :/

I still maintain that the documentation is hopelessly vague in places, though.


Grey Alien(Posted 2006) [#16]
Gfk: This is the best mouse fix problem, by Indiepath, add straight after Flip:

Function FlushGraphics:Int()
	?Win32
		If TD3D7Max2DDriver(_max2dDriver)
			Local sdesc:DDSurfaceDesc2 = New DDSurfaceDesc2
			sdesc.dwSize = SizeOf(sdesc)
			Local res:Int = PrimaryDevice.backbuffer.Lock(Null,sdesc,DDLOCK_WAIT|DDLOCK_READONLY,Null)
			PrimaryDevice.backbuffer.unlock(Null)
			Return res
		EndIf
	?
End Function



GfK(Posted 2006) [#17]
Thanks - beaker suggested exactly that fix at about 2am this morning but I didn't get chance to try it til just now.

Hope this gets fixed 'officially' in the next update anyway. :)


(tu) ENAY(Posted 2006) [#18]
Me too :)


Grey Alien(Posted 2006) [#19]
Me Three and a few more things too like the vertical tearing in Windowed Mode and the sound channel bugs.


(tu) ENAY(Posted 2006) [#20]
and proper streaming music audio.


Robert Cummings(Posted 2006) [#21]
How about audio that doesn't crackle on 98?


Grey Alien(Posted 2006) [#22]
I was actually considering start a thread call High Priority Bugs that we could post proper big known bugs that really affect us all as opposed to tiny bugs that aren't much of an issue right now...


Dreamora(Posted 2006) [#23]
A proper media library would already be good enough ... loading times like hell for OGG is a serious problem.


Grey Alien(Posted 2006) [#24]
I've started a "Major Requests" thread http://www.blitzbasic.com/Community/posts.php?topic=59005

Be cool if people added stuff there, like streaming oggs.


Vin78(Posted 2012) [#25]
I had this problem. But have solved it now.

I don't know why it doesn't just let you specify:

If Sprite Collision 1,2
Else
Endif

But for some reason were defining Images and not sprites. Pain in the Arse.

If anyone is still confused it is thus:

If ImagesCollide(YOUR_IMAGE_A_NAME,YOUR_IMAGE_A_XPOS,YOUR_IMAGE_A_YPOS,0,YOUR_IMAGE_B_NAME,YOUR_IMAGE_B_XPOS+WIDTH OF IMAGE,YOUR_IMAGE_B_YPOS+HEIGHT OF IMAGE,0)
DrawText "Collided!",0,0
EndIf

YOUR_IMAGE_A_NAME
YOUR_IMAGE_A_XPOS=THE X POSITION ON THE SCREEN OF YOUR_IMAGE
YOUR_IMAGE_A_YPOS=THE Y POSITION ON THE SCREEN OF YOUR_IMAGE
0
THEN
YOUR_IMAGE_B_NAME
YOUR_IMAGE_B'S X POSITION+WIDTH OF YOUR IMAGE
YOUR_IMAGE_B'S Y POSITION+HEIGHT OF YOUR IMAGE

Remember that the computer doesn't know you've got a sprite at the Mousex() and Mousey() positions!

And that it is merely checking if the two have overlapped WITHIN THE BOX
you have created with YOUR_IMAGE_A_XPOS,YOUR_IMAGE_A_YPOS and
YOUR_IMAGE_B'S X POSITION+WIDTH OF IMAGE B
YOUR_IMAGE_B'S Y POSITION+WIDTH OF IMAGE B

Cheers
V