# How can I tell how much of a "scratch card" has been revealed?

I have developed scratch card effect.I am now working on reward system, so my question is how will I go on and implement logic which will let me know that the object behind scratch card is now visible??

• Depends what you define visible as. Technically fully visible would mean that there isn't a single nontransparent pixel overlapping from the scratch card to the object, but that would be kind of silly. I'm guessing you want it to be defined as visible when there are very few pixels in front of the object, right? Commented Jun 8, 2015 at 15:03

I agree with Superdoggy's answer, but I believe there is a better way to go about this. If you know the area of the scratch card collision box you are wanting to reveal, you could go about it by: Get the Total Pixel amount of the object(width*height). When the mouse is pressed: check the pixels around the mouse in a radius of your choice, and if it is within the area of the object, and the pixel is currently opaque, set to transparent and ++ a 'revealed pixel' counter. To check how much of the scratch card is currently visible, just divide 'Revealed Pixels' by 'Total Pixels.'

Unfortunately I don't know how to program in Unity, so here's some pseudo code!

    Initializer:
set object = {x1, y1, x2, y2}
set radius = (number greater than 0)
set bool object_pixels[object.x2 - object.x1][object.y2 - object.y1] = 1
set percent_visible = (percentage of object to be visible before coninuing, 0 to 1)

On mouse press:
--mouse.x and mouse.y are coordinates of mouse

FOR x, radius * 2 DO

FOR y, radius * 2 DO

--only run if x and y are within circle around mouse
continue
ENDIF

--if mouse + 'for variables' is within object
IF ((x + mouse.x >= object.x1) AND (x + mouse.x < object.x2) AND (y + mouse.y >= object.y1) AND (y + mouse.y < object.y2)) THEN

--if the current pixel is set to 1 (or opaque)
IF (object_pixels[x + mouse.x - object.x1][y + mouse.y - object.y1] == 1) THEN
object_pixels[x + mouse.x][y + mouse.y] = 0
revealed_pixels += 1
ENDIF

ENDIF

ENDFOR

ENDFOR

IF revealed_pixels >= ((object.x2 - object.x1) * (object.y2 - object.y1)) / percent_visible THEN
--This statement runs if over preferred amount of pixels are visible
END

• thanks mch for broad answer, I kind of use similiar approach like you, I set up 6 colliders on both side and middle of img , whn img get reveal the collider gets trigger too ,ASA all collider gets trigger ,Reward gets assign. Commented Jun 8, 2015 at 23:02

I would add up all of the alpha values of the scratch card pixels that are overlapping the object pixels. Then if "A" < some value you can assume that the user has cleared enough pixels that they can see the object.

You can find a really good tutorial on per-pixel collision detection in C# here: Linkylink. It's made for C# with XNA, but the concept isn't too hard to adapt to C# with Unity, I would assume. Simply just modify the code a bit to count alpha values of colliding pixels instead of just checking for collision.

Hope this helps!

• thanks for the link,its very helpful...I will definitly try it but currently need to complete it quickly so went with collider approach i.e., set up 6 colliders on both side and middle of img and whn img get reveal the collider gets trigger too ,ASA all collider gets trigger ,Reward gets assign. Commented Jun 8, 2015 at 23:00