OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: h- on 07 Aug 2009, 18:37:02

Title: How to check if object inside defined area
Post by: h- on 07 Aug 2009, 18:37:02
I want to know whether an object is inside a undefined sized rectangular area.

For example four road cones are set to form the 4 corners of the area.
I want to know if a soldier enters that area. How would I do that?

Triggers can't be used, I can only use positions, as in positions of the cones and position of the soldier..


I'm a complete and utter math retard so please explain this like to a four-year-old   :-[
Title: Re: How to check if object inside defined area
Post by: JamesF1 on 07 Aug 2009, 21:06:30
Not really thinking straight today (have my head in some annoying script right now...) - so this will be messy.

For something like this:
Code: [Select]
[[coneTL, coneBR], mySoldier] execVM "checkSoldierInCones.sqf";Where TL = top left, and BR = bottom right.  You don't need any more to get the dimensions of the rectangle/square.

Try:
Code: (checkSoldierInCones.sqf) [Select]
_minx = (getPos ((_this select 0) select 0)) select 0; // X-coord of top-left cone
_miny = (getPos ((_this select 0) select 0)) select 1; // Y-coord of top-left cone
_maxx = (getPos ((_this select 0) select 1)) select 0; // X-coord of bottom-right cone
_maxy = (getPos ((_this select 0) select 1)) select 1; // Y-coord of bottom-right cone

_spos = getPos (_this select 1); // Soldier position
_sx = _soldier select 0;
_sy = _soldier select 1;

// Wait until soldier is in the area.
waitUntil { (_sx>_minx) && (_sx<_maxx) && (_sy>_miny) && (_sy<_maxy) };

hint format["%1 is in the area!", _soldier];

Of course, this probably isn't the easiest way to do it... but it is the simplest for 'explanatory purposes'.  You could add a sleep to the waitUntil to do checking less frequently.  However, using "&&" means that it'll just fail at the first failed condition, anyway... so it's hardly going to put any real burden on you :)
Title: Re: How to check if object inside defined area
Post by: Luke on 07 Aug 2009, 21:18:25
Triggers can't be used, I can only use positions.

What do you mean? Theoretically, it can be done with with triggers, you just need to get the center of the area and half of each length of the trigger.

Alternative to what you are saying, that is,
it can be done with triggers, you just don't want to use them, that is a little different,
though, you could do something like what James said.

Hope that helps.

Luke
Title: Re: How to check if object inside defined area
Post by: h- on 07 Aug 2009, 21:33:19
Quote
What do you mean? Theoretically, it can be done with with triggers
The example I gave is only an example, the purposes this will be used won't work with triggers.. :P

@JamesF1
Thanx, I'll give that a try.. :)
Title: Re: How to check if object inside defined area
Post by: h- on 11 Aug 2009, 09:35:33
@JamesF1
Hmm, that doesn't seem to work, 1 to 3 of the 4 checks return false no matter whether I'm inside or outside the "coned area".  :(

Which ones are true and which ones are false depends on which side of the "coned area" I am..
Title: Re: How to check if object inside defined area
Post by: JamesF1 on 11 Aug 2009, 13:09:51
It appears I made a number of errors in the script above... wow...  Here's a version I've just tested working.

Code: [Select]
_minx = (getPos ((_this select 0) select 0)) select 0; // X-coord of top-left cone
_miny = (getPos ((_this select 0) select 1)) select 1; // Y-coord of bottom-right cone
_maxx = (getPos ((_this select 0) select 1)) select 0; // X-coord of bottom-right cone
_maxy = (getPos ((_this select 0) select 0)) select 1; // Y-coord of top-left cone

_soldier = _this select 1; // Soldier position

// Wait until soldier is in the area.
while {true} do {
    scopeName "loop";
    _sx = (getPos _soldier) select 0;
    _sy = (getPos _soldier) select 1;

    if( (_sx>_minx) && (_sx<_maxx) && (_sy>_miny) && (_sy<_maxy) ) exitWith {
        hint format["%1 is in the area!", _soldier];
    };
};

While I'm at it, I'll just mention what was probably obvious, this will only work for square areas on a square angle (e.g. 0, 90, 180, etc.) - other angles need a more complex approach.
Title: Re: How to check if object inside defined area
Post by: h- on 11 Aug 2009, 21:34:22
Thanks, that works fine.. :)

Glad you mentioned that angle stuff, due to my math retardness I always forget that those play a part in these things too..  :confused:

I would indeed need this to work in any angle (the area itself stays rectangle, only the orientation changes).
Title: Re: How to check if object inside defined area
Post by: JamesF1 on 12 Aug 2009, 12:30:22
Alas, my own maths isn't spectacular enough to make this work on an angle without some significant time spent on it... Unfortunately, I don't have a significant amount of time free at the moment :(  I'll see if I can do something about it, but I really can't make any promises.
Title: Re: How to check if object inside defined area
Post by: Luke on 13 Aug 2009, 16:46:33
I think there might be a way using trig(onomotry),

but this complex math would be why I suggested using trig(ger)s.  :P

h- I'm sure it can be done, I don't know how... But I'm sure it can be done.  :confused:

Luke
Title: Re: How to check if object inside defined area
Post by: haroon1992 on 19 Aug 2009, 16:16:29
Quote
    if( (_sx>_minx) && (_sx<_maxx) && (_sy>_miny) && (_sy<_maxy) ) exitWith {
        hint format["%1 is in the area!", _soldier];

Ur.....isn't that
hint format ["%1 is in the area!",name _soldier]  ?

Or am i wrong????
Title: Re: How to check if object inside defined area
Post by: JamesF1 on 19 Aug 2009, 16:26:40
Nope, using _soldier by itself will yield the name when using a format statement ;)
Title: Re: How to check if object inside defined area
Post by: haroon1992 on 19 Aug 2009, 16:45:33
Ohh..................
At least i got a source of knowledge from you....
Thanks for that....
Haroon1992.................