Help needed to find the area

Hi,
I have been trying to get a grasp of things by setting really small tasks for my self.. so i set up on trying to do something really basic i created a game which will be over and displays a game over message, when a box is dragged into a predefined position as below .....

Image

Now i have tried but have no clue as to how do i find out that as and when the green box over laps the grey box or lies with in it, kindly suggest a any link or tutorial where i can find a solution to such a problem .....

Note - this is what i tried - i noted down the x and y coordinates of the grey box , they were x=118, y=50 in my case , now i tried to code in the time line to add an event listener on enter frame that when the coordinates of green box also become same as that of grey it shows a game over message , but i was not able to access the properties of green box from action page as it has been converted in to an component and i could give it an instance name .....

Regards
Jin
First of all - do not write code using the time line ( exept maybe things like 'stop();' or 'this.cacheAsBitmap = true;' ) ; the best practice is to write a bunch of .as files that contain all the code you want to use.
Second - components can have instance names which can be accessed like any other MovieClip - I do not understand why you can't access the coordinates of your green box.
@viqtor- thanks i was able to give instance name to the box 🙂 , trying to access the topright , topleft, bottomleft and bottom right coordinates right will revert back if i need any help , thanks ...
I have been able to find the coordinates using the below methods -


var topLeft:Point = area1.localToGlobal(new Point(0, 0));
var bottomRight:Point = area1.localToGlobal(new Point(width, height));


but my issue now is that the green box is continuously moving how to i find its coordinates while it still is moving ...

when i test it in flash it gives the same values in trace ...


JIn
UPDATE

i have been able to find the the top left and bottom right coordinates using the below code, it changes with change in box's position on stage 🙂
/////////////////////////////
import flash.utils.*;
box1.addEventListener(Event.ENTER_FRAME,myFunction);
var x1:Number;
var y1:Number;
var x2:Number;
var y2:Number;
var x3:Number;
var y3:Number;
var x4:Number;
var y4:Number;


var topLeftStage1:Point = box1.localToGlobal(new Point(0, 0));
var bottomRightStage1:Point = box1.localToGlobal(new Point(width, height));


x3=topLeftStage1.x;
y3=topLeftStage1.y;

x4=bottomRightStage1.x;
y4=bottomRightStage1.y;

function myFunction(event:Event) {
var topLeftStage1:Point = box1.localToGlobal(new Point(0, 0));
var bottomRightStage1:Point = box1.localToGlobal(new Point(width, height));
x3=topLeftStage1.x;
y3=topLeftStage1.y;

x4=bottomRightStage1.x;
y4=bottomRightStage1.y;

trace(x3,y3);
trace(x4,y4);
}


///////////////////////////////////////////////////////////

but the problem is when the user rotates the box the coordinates changes to negative in many cases , so if the user flips the box and puts it in the grey area, the coordinate will be negative and will not match the positive x,y of grey area(but the condition of box being inside grey area will become true), how do i over come this .....

Kindly suggest

Jin
Depending on what you want, you could look into the wck ContactList
There is an example in the WCK demo: BoxMan.as

That will give you when two objects come in contact.

If you are only using boxes (and not rectangles), perhaps you could check to see if the center points are near each other.
Image

If you look at the image above , i use the code mentioned above to find the top left and bottom right of the grey box and now as i can get the x range of the grey box , i can now try and find if the green box lies between the 2 x's it should be good, but in my case this is what is happening -

grey top left(321,122)

grey bottom right(778,502)

now green box's top left(341,328) , 334,328),(347,328),(365,328)

green's bottom right (700,654),(-45,784),(-109,-51),(745,-128)


Now as you can see i have mentioned 4 value for (x,y) of top left and bottom right....as these are the values that i get when i flip the green box which the user can also do as the user can just pick up the box and flip it inside the world....

here you see that the x value becomes negative in some cases, so even if i find out that the 2 objects are touching each other how do i find that they are overlapping each other and one is inside the other , here the green being inside the grey or exactly overlapping it ....

help will be highly appreciated ...

Jin
Update

Found the solution 🙂

area1.getRect(this).containsRect(box1.getRect(this));

this will detect if all the 4 corners of green box are inside or match the 4 corners of grey box no matter if they are negative or positive....

Regards
Jin
I was wondering if the containsRect also works for rotated rectangles but it doesn't seem to. Seems like it checks the boundary rectangle, or maybe I'm missing something.
It works fine if the rectangle to be checked is non rotated.

If both rectangles rotate then another way might be to:
-take the four 'local' corners
-check if those are inside the other rectangles (local > global coordinate check).

I saw methods (haven't used) that can do that so you don't have to do it manually.
@Istvan - thanks i will try it as indeed i might need to move both of them ....

Original thread: https://box2d.org/forum/viewtopic.php?t=8728