Image collision when scaling

BlitzMax Forums/BlitzMax Beginners Area/Image collision when scaling

_JIM(Posted 2008) [#1]
Hi!

I'm having a little problem trying to see if the mouse is over an image. The image is a PNG with alpha and I'd like pixel collisions (bounding box kind of defeats the purpose of the transparent PNG).

What I used is a 1x1 image placed at the mouse cursor and check collisions between that and the transparent image. This works just fine when I'm at 100% scale and upwards, but if I scale things down, there's no more collision detected.

While writing this post, I just thought that maybe that 1x1 image gets scaled to 0x0 or something and therefore no collision.

Is there any better way to detect those collisions, or should I try to keep that image at 1x1 scale at all times?


tonyg(Posted 2008) [#2]
Would *really* help if you provided some code.
Anyway, I got the same issue but would fix it by doing a setscale before the colliderect:



_JIM(Posted 2008) [#3]
Thanks for the answer, but I kind of found the solution.

It is indeed my 1x1 pixel image that causes the problems. Some basic code should look like this:

SuperStrict

Graphics 800, 600

Global img1:TImage = LoadImage("leaf.png")
Global Pixel:TImage = LoadImage("Data\Gfx\dot2.png")

SetBlend ALPHABLEND

Global Scale:Float = 1.0

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
	
	Cls
	
	If (KeyHit(KEY_SPACE) ) 
		If (Scale > 0.99) 
			Scale = 0.5
		Else
			Scale = 1.0
		EndIf
	EndIf
	
	SetScale Scale, Scale
	
	DrawImage img1,100,100
	
	If (ImagesCollide(img1 , 100 , 100 , 0 , Pixel , MouseX() , MouseY() , 0) ) 
		DrawText "Collision", 0, 0
	EndIf
	
	Flip
	
Wend
End


Sorry I can't provide the images, so if you're too lazy just forget it :)

I've found the fix though: ImagesCollide2!

Guess this was the purpose for that function. I can draw the first image scaled to any size, but keep my 1x1 image at 100% scale.

Back to coding now...


tonyg(Posted 2008) [#4]
Hmmm! ImagesCollide2 does under the covers what my code does. Using ImagesCollide2 won't be as flexible in scope but, as I don't know your scope, I'll leave that for you.


_JIM(Posted 2008) [#5]
I was using ImagesCollide before, so it was a matter of adding a "2" and changing parameters. Thanks anyway :o)


MGE(Posted 2008) [#6]
I've never used the collision system, has anyone done any tests with hundreds of sprites moving around, colliding with each other and/or a background?


_JIM(Posted 2008) [#7]
Hmm, never thought f benchmarking it. I'm using it for a level editor, so I'm really not that concerned about it. I can tell you that it runs fine with around 1000 alpha sprites checking collision with that 1x1 image on a Q6600 (what I have at work).

I could optimize stuff by checking if they're on screen, then with bounding box, and then collision, but I don't feel the need right now.

I will probably try a benchmark soon though.


ImaginaryHuman(Posted 2008) [#8]
I made a simple little game and used imagescollide() for collision detection, it had maybe up to 30-50 objects about 100x100 pixels on the screen at the most, compared against the player object. Was fine on a 2GHz dual core iMac ATI X1600 128mb. I expect it could handle more than that but at some point it's going to become apparent that it is doing a lot of work to find a collision.