# Check whether a game object is inside the boundary

I'm developing a small children game similar to Paperama. Here the player can scale rotate and move the object anywhere in the screen and the player should fit it in the boundary. I'm okay with everything (scale, move, rotate etc..) but I couldn't think of a way how to measure the percentage where the target object is inside the boundary object. Here is a graphical representation of what I want to achieve. I tried comparing vectors and positions but it's so complex and inefficient for symmetrical objects where multiple positions are acceptable to fill the void.

• Are your target objects and your boundary objects always regular, or at least always convex? Do they always share the same shape? You need the percentage of the overlapping between them even when target objects is not entirely inside boundary object or only when it is inside? – MAnd Nov 9 '15 at 17:29
• Also, notice that your image is rather confusing. By it, one could conclude that what you are asking is how detect when target object is inside boundary object and then expand target object to fill the whole boundary object. – MAnd Nov 9 '15 at 17:36
• Pentagon is just a scratch i made in paint. But in the actual game there maybe images of complex objects (eg: an image of a bicycle) – Indika Wijesooriya Nov 10 '15 at 1:40
• And what about my other two clarification questions: do target object and boundary object always share the same shape (i.e. if one is a bicycle the other is as well)? What do you need is the percentage of the overlapping between them? – MAnd Nov 10 '15 at 13:17

What if you did this by lining up specific points instead of the percentage of the object.

What I mean is to match each vertex. For a polygon matching all vertexes with an appropriate vertex to some variable degree of success will read as equal positions. • What exactly do you mean? In some instances a shape can fit if it's rotated any which way such as a circle or square but even if they're more oblong polygons, each vertex would need to match another vertex, right? I think I may be confused about what you mean though haha – Jacob Johnson Nov 9 '15 at 20:10
• Oh sorry, I meant possible small downside. I can imagine something like star or connecting the vertices in wrong order or something like E and 3. Its just it doesnt really care about area and some more complex shapes could surprise. – wondra Nov 9 '15 at 20:51
• This is true. It'd probably be easier this way but there are definitely some snags depending on some variables. It really depends on what OP wants to achieve. If he's sticking to something basic like he explained this might be a solution. However, he may very well have just given a simple response as an example. – Jacob Johnson Nov 9 '15 at 21:11
• What if the object does not have vertices (actually many vertices) ? – Indika Wijesooriya Nov 10 '15 at 1:38
• For a circle it could just mark 2-4 opposite points? This would still require that it is lined up. – Jacob Johnson Nov 10 '15 at 14:24

You could try Boolean polygon intersections, or polygon-polygon clipping. Both can be looked up easily, thus I'm not going to talk about their implementation. IMO polygon-polygon clipping is easier though.

The algorithm is as follows: First compute the area of the target polygon. Next clip (or intersect) the user-handled polygon by the target polygon, that is the target polygon is the clipping polygon, and the other polygon is the clipped polygon. Compute the area of the polygon returned by the above operation. If it is equal to the area of the target polygon, it is a complete fit. The percentage is the above area divided by target polygon area * 100.

I think you're approching this from the wrong angle, you don't really need to check if an object is inside another object. You just need to check if an objects scale, position and rotation matches a set target and average the percentage of each category.

Calculating a score could be something like this quick pseudocode:

• dist% = max(0, DISTANCESCOREBASE - dist(obj.pos,target.pos))/100
• scale% = max(0, 1-abs(target.scale-obj.scale))
• rot% = 1-(totalAngle(target.rotation, obj.rotation))/(PI/2)
• tot% = (dist% + scale% + rot% ) / 3

You'll have to refine the algorithms for the exact scoring-system you want but hopefully this can set you on your way.

There is a struct named "Bounds" in Unity, which allows you to define "areas" in the worldspace, which can later be used for position comparision.

Here are the steps for you to follow:

1. Get the object's bounds from either collider or renderer (they are usually same).

2. Move the center of bounds to the area you want to check.

3. Use Contains boolean to find out if the center of your gameobject is inside that area - using center should be enough for approximate calculations.

Note: If you want absolute results, compare centers of extents and corners one by one with the corresponding point on bounds.

Here is the API referance for Bounds: http://docs.unity3d.com/ScriptReference/Bounds.html