How to Check and Set Instance Variable Based on 80% Overlap Area?

0 favourites
  • 7 posts
From the Asset Store
80 pieces of loopable game music, 10 game overs and 10 level complete pieces.
  • How to Calculate and Set Instance Variable Based on 80% Overlap Between Two Objects.

    If object A overlaps object B by more than 80% area-wise, then object A's instance variable overlapping should be set to true; otherwise, it should be set to false.

  • Hi. It all depends on the overhead.

    If you need to calculate 60 times per second, then I would recommend to apply overlapping objects to primitive shapes, for example two circles, it is easy to calculate the area of their contact or rectangles.

    If you need to calculate 30 times per second or less, you can use more accurate methods. I would use the Montecarlo method.

    All these formulas are available online.

    Probably you can use the built-in methods of Construct3 itself, for example, place 10 image points on the object and check how many of them will be above the overlapped object. I think you can still come up with a couple of crutches.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • igortyhon Thank you for your response! Your suggestions are really helpful. If possible, could you please share an example or a small demonstration of how to implement one of these methods, such as using image points for overlap detection? It would greatly help me understand the process better. Thanks again!

  • You could, simply, check the distance between the center of both objects.

  • Here is a crutch method by points, of course the more points the more accurate.

    fex.net/en/s/0ylzezs

  • Here's a few ideas:

    If you want to compare the visual overlap area you can do that by looking at pixels with a drawingCanvas. Basically draw one sprite to the drawingCanvas, snapshot the canvas, and then loop over the pixels and count the number of then with an alpha>50. That will give the Initial area. If the sprite isn't changing sizes save that to a variable, so you don't have to recalculate it every time. Then you paste the second object to the canvas with the "destination out" blend. That will erase the overlapping part from from the first sprite. Then you'd snapshot the canvas, loop over the pixels and count the pixels with an alpha>50. That will give the number of pixels not overlapping. Finally, the calculation will be percentCovered = 1 - (pixels not overlapping)/(initial area).

    Overall, that will take a few frames to complete since it involves a few async operations. Looping over all the pixels can be a bit heavy, so you'd want the canvas to just be big enough to cover both objects, and/or you could just sample some of the pixels like igortyhon suggested.

    Another way is to check the percentage the collision shapes are overlapping. It can be done in one tick but you'd need to use "object overlaps point" to sample stuff. The con is it's only approximate based on how many points you sample.

    You can also access the polygon points and if both objects have convex collision polygons you can calculate the exact area covered by the polygon and use a line clipping algorithm to clip one polygon by the other to get the intersecting polygon. The main complexity here is to clip the polygons and calculate the areas. It's not hard but won't be a one liner.

    If the objects are both unrotated boxes or circles, then you can do something even simpler.

    For example, if both are unrotated boxes you'd calculate the coverage with:

    Sprite1: overlaps Sprite2

    --- percent = (min(sprite1.bboxBottom, sprite2.bboxBottom)-max(sprite1.bboxTop, sprite2.bboxTop))*(min(sprite1.bboxRight, sprite2.bboxRight)-max(sprite1.bboxLeft, sprite2.bboxLeft))/(Sprite1.width*sprite1.height)

    Circles could be a relatively simple option too. Basically come up with the math from the two radii and the distance between the centers.

  • Hi R0J0hound

    thank you so much this worked for me.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)