Images Collide issue?

BlitzMax Forums/BlitzMax Beginners Area/Images Collide issue?

Captain Wicker (crazy hillbilly)(Posted 2012) [#1]
Const ALPHABITS=$ff000000

Graphics 1024,768

image=CreateImage(256,1)
map=LockImage(image)
For i=0 To 255
	WritePixel(map,i,0,ALPHABITS|i)
Next

image1=CreateImage(256,1)
map1=LockImage(image1)
For i=0 To 255
	WritePixel(map1,i,0,ALPHABITS|i)
Next

UnlockImage(image1)
UnlockImage(image)

HideMouse 

Repeat
Cls

DrawImageRect image1,700,600,56,56

DrawImageRect image,0,0,56,56
DrawText "Blue Color Gradient",0,0

If ImagesCollide(image,1,1,0,image1,1,1,0) 
 Notify("Images Collided",serious=False)
EndIf 

Flip
Until AppTerminate() Or KeyDown(key_escape)
End 

Why when I run the program it says images have collided but they haven't?


JazzieB(Posted 2012) [#2]
Because you have supplied the same screen co-ordinates in the ImagesCollide function - it does not matter where they are actually drawn, the test is done on the co-ords you supply.


Captain Wicker (crazy hillbilly)(Posted 2012) [#3]
Thank you JazzieB, That was a careless mistake on my part. ;)


matibee(Posted 2012) [#4]
here's another one..

Notify("Images Collided",serious=False)


You need to start user SuperStrict at the head of your code or you'll have lots of issues like this.

That actually compiles to:

[if local variable "serious" = false]
Notify("Images Collided", True )

[if local variable "serious" = True ]
Notify("Images Collided", False )

Get your head around that one :D


Captain Wicker (crazy hillbilly)(Posted 2012) [#5]
Get your head around that one

lol, I will :P

So why doesn't the program notify me if the two images collide now?
Const ALPHABITS=$ff000000

Graphics 1024,768

image=CreateImage(256,1)
map=LockImage(image)
For i=0 To 255
	WritePixel(map,i,0,ALPHABITS|i)
Next

image1=CreateImage(256,1)
map1=LockImage(image1)
For i=0 To 255
	WritePixel(map1,i,0,ALPHABITS|i)
Next

UnlockImage(image1)
UnlockImage(image)

HideMouse 

Repeat
Cls

DrawImageRect image1,MouseX(),MouseY(),56,56

DrawImageRect image,0,0,56,56
DrawText "Blue Color Gradient",0,0

If ImagesCollide(image,1,1,0,image1,2,2,0) 
 Notify("Images Collided" , serious = False)
ElseIf ImagesCollide(image1 , 2 , 2 , 0 , image , 1 , 1 , 0)
 Notify("Images Collided" , serious = False)
EndIf 

Flip
Until AppTerminate() Or KeyDown(key_escape)
End 


Last edited 2012


Dabhand(Posted 2012) [#6]
DrawImageRect breaks it (EDIT: Badly put... It doesnt break anything, it just misrepresents what your testing):-

If you look:-


Const ALPHABITS=$ff000000

Graphics 1024,768

image=CreateImage(256,1)
map=LockImage(image)
For i=0 To 255
	WritePixel(map,i,0,ALPHABITS|i)
Next

image1=CreateImage(256,1)
map1=LockImage(image1)
For i=0 To 255
	WritePixel(map1,i,0,ALPHABITS|i)
Next

UnlockImage(image1)
UnlockImage(image)

HideMouse 

local mx:Int, my:Int

Repeat
Cls
mx = mousex()
my = mousey()

DrawImageRect image1,mx,my,56,56

DrawImageRect image,0,0,56,56
DrawText "Blue Color Gradient",0,0

If ImagesCollide(image,0,0,0,image1,mx,my,0) 
 	drawtext "images collided",0,100
EndIf 

Flip
Until AppTerminate() Or KeyDown(key_escape)
End 


This is untested, but, what will happen is that if you move the image somewhere around the fixed image, it will pick up the collision, but not pixel perfect (Using DrawImageRect) as your image is an actual line, not the gradient rect you see!

Dabz

Last edited 2012


Dabhand(Posted 2012) [#7]
Sorry about the quick summary above, lol, had to quickly mash some potatoes! :D

Anyway, if you replace DrawImageRect, with plain old DrawImage, minus the last 2 parameters, you'll see what your actual testing! :)

The fix, create a pixmap, with a size 56x56, and draw your gradient rect to that first, load it up as an Image LoadImage(hPixmap), then, draw that image with plain old DrawImage, and then use this handle in ImagesCollide... Done!

Dabz


Captain Wicker (crazy hillbilly)(Posted 2012) [#8]
@Dabz:
How do I create this PixelMap?


Dabhand(Posted 2012) [#9]
Something like...


Graphics 640,480

HideMouse 

Const ALPHABITS=$ff000000
Local mx:Int, my:Int

'Create the gradient line then store it in an image
Local pix:TPixmap = CreatePixmap(256,1, PF_RGBA8888)
For i=0 To 255
	WritePixel(pix,i,0,ALPHABITS|i)
Next

Local gradient:TImage = LoadImage(pix)

'Now, create our gradient rect using the line above
Local image:TImage
Local imagePix:TPixmap = CreatePixmap(57,57,PF_RGBA8888)
Cls
DrawImageRect gradient,0,0,56,56
imagePix = GrabPixmap(0, 0, 56,56)
image = LoadImage(imagePix)

Repeat
Cls

'Nab mouse coords
	mx = MouseX()
	my = MouseY()
	
	'Check collision
	If ImagesCollide(image,mx,my,0,image,20,20,0) Then DrawText "Collided",0,0
	'Draw Fixed
	DrawImage image,20,20
	'Draw at mouse coords
	DrawImage image,mx,my

Flip
Until KeyDown(KEY_ESCAPE)


That!

Dabz