Imagerectcollide

BlitzPlus Forums/BlitzPlus Programming/Imagerectcollide

Paulo(Posted 2004) [#1]
Hi, new user here looking for a little explanation on the topic command. Ive basically made my own buttons and want them to be clickable. This works, but Im having problems with the numbers on the imagerectcollide command.. also not sure what I should use in the image reference, since im using the standard mouse pointer, not a cursor image.

Suppose I had a button image drawn here like this...

DrawImage mybutton,20,447 ;Image dminension is 34x24

And I want to test the collision with a plain mouse pointer (with no image) what would I write after the Imagerectcollide command to get the exact detection? Im having real problems here because every time I try this, my detection area seems too big and not referenced from the correct place, I got it to work by trial and error, but its not pixel perfect..

I thought it should be this for the above DRAW line...

mx = mouseX()
my = mouseY()

If ImageRectCollide(WHAT TO PUT HERE?,mx,my,0,20,447,34,24)

and would the numbers be correct? Because to get it to work I had to write this..

If ImageRectCollide (mybutton,mx,my,0,24,455,54,10)

Those numbers seem way out to me, but what am I missing?

Furthermore, "mybutton" is not the mouse image, its the image being used for the button, so how come this works? Im happy I can get it to work, but I would rather know why its not working as I expect it to. Ive followed the command help, but the example is a bit different, any insight anyone can offer would be very welcome. Thanks for reading.


Binary_Moon(Posted 2004) [#2]
There are many times when you need to see if an image has collided with (or is touching) a specific rectangular area of the screen. This command performs pixel perfect accurate collision detection between the image of your choice and a specified rectangle on the screen.



The ImageRectCollide command checks to see if an image collides with a rectangle. You want to see if there is a point (mx,my) inside a rect.

Basically you're using the wrong command. All you have to do is check to see if the mouse is within a specific bounding box.

Function within(x,y,w,h)

	;--------------------------------------------------	
	;check to see if the mouse is within specified rect
	;--------------------------------------------------

	If MouseX()>x And MouseX()<x+w
		If MouseY()>y And MouseY()<y+h
			Return True
		EndIf
	EndIf

	Return False
	
End Function


is what I use.

In your example you would use

within(20,447,34,24)



Paulo(Posted 2004) [#3]
Thanks so much. :) That makes perfect sense.


Murilo(Posted 2004) [#4]
I use the following equation, where pintX and pintY identify the point you're checking and intX1, intY1, intX2, intY2 are the bounding co-ordinates.

((((pintX - intX1) Xor (pintX - intX2)) And ((pintY - intY1) Xor (pintY - intY2))) And $80000000)


TeaVirus(Posted 2004) [#5]
If you need more than just rectangular buttons, you could use Skidracer's InsidePoly function. I'm using this for rotated and odd shaped buttons and find that it's very accurate and fast.

http://www.blitzbasic.com/codearcs/codearcs.php?code=45


Paulo(Posted 2004) [#6]
nice one thanks, I hadnt spotted that one.